SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calendar/events from db

    Hello all,
    I have a bit of a problem I'm running into.
    I have events in a database and each event has a start date and end date. They could be the same or different.
    also could be multiple events per day.
    the link to the output of this code is http://www.desertgatewaybaptist.org/calendar_new.php

    Here is my current code.
    PHP Code:
    <?php
    if( function_exists'date_default_timezone_set' ) )
    {
            
    // Set the default timezone to US/Eastern
            
    date_default_timezone_set'US/Eastern' );
    }
     
    // Will return a timestamp of the last day in a month for a specified year
    function last_day$month$year )
    {
            
    // Use mktime to create a timestamp one month into the future, but one
            // day less.  Also make the time for almost midnight, so it can be
            // used as an 'end of month' boundary
            
    return mktime235959$month 10$year );
    }
     function 
    in_multi_array($needle$haystack)
     {
      foreach(
    $haystack as $pos => $val)
      {
       
    //if (is_array($val))
       //{
       // if (in_multi_array($needle, $val))
       //  return 1;
       //} else
        
    if ($pos == $needle)
         return 
    1;
      }
     }
    // This function will print an HTML Calendar, given a month and year
    function print_calendar$month$year$weekdaytostart )
    {
            
    // There are things we need to know about this month such as the last day:
            
    $last idate'd'last_day$month$year ) );
     
            
    // We also need to know what day of the week the first day is, and let's
            //  let the system tell us what the name of the Month is:
            
    $firstdaystamp mktime000$month1$year );
            
    $firstwday     idate'w'$firstdaystamp );
            
    $name          date'F'$firstdaystamp );
            
    // To easily enable our 'any day of the week start', we need to make an
            //  array of weekday numbers, in the actual printing order we are using
            
    $weekorder = array();
            
            for ( 
    $wo $weekdaytostart$wo $weekdaytostart 7$wo++ )
            {
                    
    $weekorder[] = $wo 7;
            }
            
    // Now, begin our HTML table
            
    echo "<table cellspacing=\"0\">\n";
     
            
    // Now before we really start, print a day row:
            // Use the system to tell us the days of the week:
            
    echo '<thead><tr>';
     
            
    // Loop over a full week, based upon day 1
            
    foreach ( $weekorder as $w )
            {
                    
    $dayname date'D',
                    
    mktime000$month,1-$firstwday $w$year ) );
                    echo 
    "<th>{$dayname}</th>";
            }
            
            echo 
    "</tr></thead>\n";
     
            
    // Now we need to start some counters, and do some looping:
            
    $onday   0;
            
    $started false;
            echo 
    "<tbody>\n";
            if (
    strlen($month)==1) { $month "0"$month; }
            
    $min_dat $year"-"$month"-01";
            
    $max_dat $year"-"$month"-31";
            
    $sql "SELECT * from calendar WHERE (ev_dat>='$min_dat') AND (ev_dat<='$max_dat') ORDER by ev_dat";
            
    $conn = new mysql();
            
    $result $conn->query($sql);
            
    $count 0;
            
    $data = array();
            while (
    $rows $conn->FetchArray($result)) {
                    
    $data[$count]['title'] = $rows['ev_title'];
                    
    $data[$count]['loc'] = $row['ev_locn'];
                    
    $data[$count]['desc'] = $rows['ev_desc'];
                    
    $data[$count]['date'] = $rows['ev_dat'];
                    
    $data[$count]['date_end'] = $rows['ev_dat_end'];
                    
    $count++;
            }
            
            echo 
    "<pre>";
            
    print_r($data);
            echo 
    "</pre>";
            
            
                            
    $count 0;
            
    // While we haven't surpassed the last day of the month, loop:
            
    while ( $onday <= $last )
            {
                    
    // Begin our next row of the table
                    
    echo '<tr>';
                    
    // Now loop 0 through 6, for the days of the week, but in the order
                    //  we are actually going, use mod to make this work
                    
    foreach ( $weekorder as $d )
                    {
                            
    // If we haven't started yet:
                            
    if ( !( $started ) )
                            {
                                    
    // Does today equal the first weekday we should start on?
                                    
    if ( $d == $firstwday )
                                    {
                                            
    // Set that we have started, and increment the counter
                                            
    $started true;
                                            
    $onday++;
                                    }
                            }
                            
    // Now if the day is zero or greater than the last day make a
                            //  blank table cell.
                            
    if ( ( $onday == ) || ( $onday $last ) )
                            {
                                    echo 
    '<td class="padding"></td>';
                            }
                            else
                            {
                                    
    $spans "";
                                    
    $count 0;
                                    
    $loop "true";
                                    
    $newday $onday;
                                    
    $events "false";
                                    if (
    strlen($newday)==1) { $newday "0"$newday; }
                                    
    $date $year."-".$month."-".$newday;
                                    
    $todayunix strtotime($date);
                                    
    // Otherwise, echo out a day & Increment the counter
                                    //if (in_multi_array($date,$data)) {
                                    
    While ($loop == "true") {
                                            if ((
    $todayunix >= strtotime($data[$count]['date'])) && ($todayunix <= strtotime($data[$count]['date_end']))) {
                                            
    $top =  "<td class=\"date_has_event\">".$onday."<div class=\"events\">";
                                            
    $top .= "               <ul>";
                                            
    $top .= "                       <li>";
                                            
    $spans .= "                             <span class=\"title\">".$data[$count]['title']." ".$count."</span>";
                                            
    /*if (!empty($_SESSION[$date]) && $_SESSION[$date] == "1") {
                                                    $spans .= "<span class=\"title\">".$data[$count]['title']."</span>";
                                            }*/
                                            
    $bottom .= "                    </li>";
                                            
    $boomom .= "            </ul>";
                                            
    $bottom .= "            </div></td>";
                                            
    $_SESSION[$date] = "1";
                                            
    $events "true";
                                            } 
                                            if (
    $count 50 && $events == "false") {
                                                    
    $blank "<td>{$onday}</td>";
                                                    
    $loop "false";
                                            }
                                            
    // if ($onday > $last ) {
                                                    // $loop = "false";
                                                    // $events = "false";
                                            // }
                                            
    if ($count 10000) {
                                                    
    $loop "false";
                                                    
    $events "false";
                                            }
                                            
    $count++;
                                    }
                                    
    $onday++;
                            }
                            echo 
    $top;
                            echo 
    $spans;
                            echo 
    $bottom;
                    }
     
                    
    // End this table row:
                    
    echo "</tr>\n";
            }
            echo 
    "</tbody>\n";
            
    // Now end the table:
            
    echo "<tfoot><th>Mon</th><th>Tue</th><th>Wed</th>";
            echo 
    "<th>Thu</th><th>Fri</th><th>Sat</th>";
            echo 
    "<th>Sun</th></tfoot>";
            echo 
    '</table>';
    }
     
    // ========================================================
    // = Demo showing calendar for all months of current year =
    // ========================================================
     
     
    // Create an entire year calendar for 2006 with Monday as the first day:
     
            
    print_calendar"3"date'Y' ), );
            echo 
    '<br />';
    ?>

  2. #2
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anybody?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,140
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    What is the problem exactly?


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
  •