Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder logictrap's Avatar
    Join Date
    Apr 2008
    Posts
    155
    Thanks
    11
    Thanked 3 Times in 3 Posts

    Looking for PHP code to build a calendar from a MySQL DB

    I have a database of events that I want to display in traditional calendar format (ie a table for each month with columns for days of week and cell for each day) In each cell I want to display the event titles for that day as links to a detail page for the event.

    My DB has columns for:

    Start Date
    End Date
    Event Title
    Event Details

    Can anyone point me to some open source or freeware code to handle this?

    Thanks

    Which came first - the chicken or the egg? The egg... [ticket closed]
    If a tree falls... does it make a sound? Yes.............. [ticket closed]

  • #2
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    http://zoklet.net/demos/cabin/index.php?page=calendar

    I think that's exactly what you're looking for. Each one of those calendars was printed with a function I made called print_cal which takes a month and year as parameters.
    PHP Code:
    function print_cal($month,$year) {
            
    db_connect();
            
    $cal_names = array('Jan''Feb''Mar''Apr''May''Jun''Jul''Aug''Sep''Oct''Nov''Dec');
            
    $cal_last date('t',mktime(0,0,0,$month,1,$year)) ;
            
    $cal_start date('w',mktime(0,0,0,$month,1,$year)) + 1;
            
    $calendar = array();
            
    $start_date mktime(23,59,59,$month,0,$year);
            
    $end_date mktime(0,0,0,$month+1,1,$year);
            
    // Confusing query, but it's just selecting all of events with start or end times inside of
            // the month we're targeting.
            
    $query "SELECT * FROM `calendar` WHERE ((`start_time`>'$start_date' AND `start_time`<'$end_date' ) OR (`end_time`>'$start_date' AND `end_time`<'$end_date'))";
            
    $result mysql_query($query);
            while(
    $row mysql_fetch_assoc($result)) {
                    
    $temp_id $row['index'];
                    
    $temp_start $row['start_time'];
                    
    $temp_end $row['end_time'];
                    
    $temp_who $row['who'];
                    while(
    $temp_start<=$temp_end && $temp_start<=mktime(23,59,59,$month,cal_days_in_month(CAL_GREGORIAN,$month,$year),$year))
                    {
                            
    $temp_date date("d",$temp_start);
                            
    $calendar[$temp_date] = $temp_who;
                            
    $temp_start mktime(23,59,59,$month,$temp_date+1,$year);
                    }
            }
            print 
    "<br/>";
            print 
    '<b>'.$cal_names[$month-1].", $year</b>";
            print 
    '<table width="150" border="0" id="old_table"><tr>';
            print 
    '<tr><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>';
            if (
    $cal_start != 0) {
                    print 
    "<td class='calendar' colspan=". ($cal_start-1) .">&nbsp</td>";
            }
            
    $cal_pos=$cal_start;
            for (
    $j=1;$j<=$cal_last;$j++)
            {
                    
    $j;
                    if (
    $j<10)
                    {
                            
    $j '0'.$j;
                    }
                    if(isset(
    $calendar[$j])) {
                            print 
    "<td width=20 class=\"".$calendar[$j]."\"><div class=\"tip\"onmouseover=\"tooltip('$calendar[$j]');\" onmouseout=\"exit();\">$j</div></td>";
                    } else {
                            print 
    "<td width=\"20\">$j</td>";
                    }
                    
    $cal_pos++;
                    if (
    $cal_pos == 0)
                    {
                            print 
    "</tr><tr>";
                            
    $cal_pos=1;
                    }
            }
            print 
    "</tr></table>";
            
    db_disconnect();

    The code's not too clean since I hijacked the code from another calendar I made (without the functionality of the database).

  • #3
    Regular Coder logictrap's Avatar
    Join Date
    Apr 2008
    Posts
    155
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Thanks - that's a nice looking calendar.

    I need to have it display a cell for each day with the events for that day listed in the cell.

    Which came first - the chicken or the egg? The egg... [ticket closed]
    If a tree falls... does it make a sound? Yes.............. [ticket closed]

  • #4
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    If you were using my code to do that, you would have to store a (reference to) the information you wanted to display inside of your $calendar array (you'd change this in that while loop). You'll see that I store the name of each of the visitors in $calendar and then echo it in the for loop below. You could store whatever information you wanted here (including other columns gathered from your SQL query).


  •  

    Posting Permissions

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