SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello all, I put together a PHP program that will generate a calendar much like the one here on sitepoint. I have a table called "events" in my mysql database with the fields id, description, dateofevent.

    Right now I'm trying to figure out a way to minimize the number of queries to extract and display the events for each day in the calendar.

    For example, I thought of querying the database for all events in the month of may, of course all the records would be restored in the "results" array. I'm assuming at this point I would have to have a nested while statment. The inner to scan and display all events for a particuar day and the outter while to increment through all the days. This seems like it would take up a lot of cpu time.

    Could someone that has done this type of thing suggest a more optimized way to do this? I'm not too keen on hashes could something like this be a little better?

    If not, is there a spiffy way to split the results array of the query into a 2 dimensional array to reduce the number of comparisions on the date field?

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way I have done it is to basically grab all the records for the month and store their day number in an array. For instance if you have date field in your table you could do


    Code:
    select DAYOFMONTH(datefield) as hotday, restof fields WHERE something = '$something';

    Now when you loop through the result set simply put all the days in an array let's call it $hotdays

    PHP Code:
    while($row mysql_fetch_array($result)) {
    $hotdays[] = $row["hotday"];


    Okay so now you can use in_array() to see if the current day for the calendar needs to be a hotlink. If its in the hotdays array then you there is an event for that day.

    PHP Code:
    if(in_array($curday$hotdays)) {
    print 
    "<a href=\"somepage.php?day=$curday\">$curday</a>';
    }
    else {
    print 
    $curday;

    Is that what you are after?

    I built a calendar class to do this type of thing and you can add the db lookup without too much trouble. Let me know if you want the exact code to use the db lookup with this class.

    http://www.geekarea.org/go.kryogen/a...ion=show/id=20
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, thats it. Didn't think of filtering out all days for the month in the sql query with DAYOFMONTH(), first. THanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •