HurricaneDan Posted October 8, 2007 Share Posted October 8, 2007 I have a calendar that I would like to display some events on. The events are stored in a database but I do not think I am getting the information in the right place and displaying like it should be. I cannot seem to get an event to display on the exact date that it should be displayed on. I can get one event for a whole week but that, of course, is not right. Any help would be appreciated, Dan // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $selectquery=mysql_query (" SELECT `p`.`id` as id , `p`.`hostname` as hostname , `p`.`partydate` as partydate , `p`.`partytime` as partytime , COUNT( `g`.`id` ) AS numofguests FROM parties AS p LEFT JOIN guests AS g ON `p`.`id` = `g`.`host_id` WHERE partydate > NOW( ) GROUP BY `p`.`id`"); $num = mysql_num_rows($selectquery); ?> <?php error_reporting('0'); ini_set('display_errors', '0'); //gather variables from the user and break them down for this script if(!isset($_REQUEST['date'])) { $date = mktime(0,0,0,date('m'),date('d'), date('Y')); } else { $date =$_REQUEST['date']; } $day = date('d', $date); $month = date('m', $date); $year = date('Y', $date); //Get the first day of the month $month_start = mktime(0,0,0,$month, 1, $year); //Get the friendly month name $month_name = date('M', $month_start); //Find first day of the week the month starts $month_start_day = date('D', $month_start); switch($month_start_day){ case "Sun": $offset = 0; break; case "Mon": $offset = 1; break; case "Tue": $offset = 2; break; case "Wed": $offset = 3; break; case "Thu": $offset = 4; break; case "Fri": $offset = 5; break; case "Sat": $offset = 6; break; } //determine how many days are in the previous month if($month == 1) { $num_days_last = cal_days_in_month(0, 12, ($year - 1)); } else { $num_days_last = cal_days_in_month(1, ($month - 1), $year); } //determine how many days are in the current month $num_days_current = cal_days_in_month(0, $month, $year); //Build an array of the days for($i = 1; $i <= $num_days_current; $i++) { $num_days_array[] = $i; } //build an array from the last months days for($i = 1; $i <= $num_days_last; $i++) { $num_days_last_array[] = $i; } //check the offset for day of the week, none is needed if month starts on Sunday if($offset > 0) { $offset_correction = array_slice($num_days_last_array, -$offset, $offset); $new_count = array_merge($offset_correction, $num_days_array); $offset_count = count($offset_correction); } else { $new_count = $num_days_array; } //count how many total days we have with these two arrays merged $current_num = count($new_count); //With 5 HTML table rows with 7 table data entries we will need a total //of 35 TDs. We will need to see from a third array how many days we need if($current_num > 35 ) { $num_weeks = 6; $outset = (42 - $current_num); } elseif($current_num < 35) { $num_weeks = 5; $outset = (35 - $current_num); } if($current_num == 35) { $num_weeks = 5; $outset = 0; } //outset correction for($i = 1; $i <= $outset; $i++) { $new_count[] = $i; } //separate the $new_count array into 7 day weeks $weeks = array_chunk($new_count, 7); $previous_link = "<a href=\"".$_SERVER['PHP_SELF']."?date="; if($month == 1) { $previous_link .= mktime(0,0,0,12,$day,($year - 1)); } else { $previous_link .= mktime(0,0,0,($month - 1), $day, $year); } $previous_link .= "\"><< Prev</a>"; $next_link = "<a href=\"".$_SERVER['PHP_SELF']."?date="; if($month == 12) { $next_link .= mktime(0,0,0,1,$day,($year + 1)); } else { $next_link .= mktime(0,0,0,($month + 1), $day, $year); } $next_link .= "\">Next >></a>"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css"> </head> <boby> <div id="wrapper"> <div id="navigation"> <div id="menuh-container"> <div id="menuh"> <!--<ul> <li><a href="index.html">Home</a></li> </ul>--> <ul> <li><a href="about.html">About Us</a></li> </ul> <ul> <li><!--<a href="products.html" class="top_parent">Products</a>--><a href="#" class="top_parent">Products</a> <ul> <li><a href="clothes.html">Kids' Clothing</a></li> <li><a href="gifts.html">Baby Gifts & <br />Accesories</a></li> </ul> </li> </ul> <ul> <li><a href="schedule.php">Calendar</a></li> </ul> <ul> <li><a href="contact_us.html">Contact Us</a></li> </ul> </div> <!-- end the menuh-container div --> </div><!-- end the menuh div --> </div><!-- end navigation --> <div id="content"> <?php echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"520\" class=\"calendar\">\n". "<tr>\n". "<td colspan=\"7\">". "<table align=\"center\">". "<tr>". "<td colspan=\"2\" width=\"150\" align=\"left\">$previous_link</td>\n". "<td colspan=\"3\" width=\"200\" align=\"center\">$month_name $year</td>\n". "<td colspan=\"2\" width=\"150\" align=\"right\"$next_link</td>\n". "</tr>\n". "</table>\n". "</td>\n". "<tr>\n". "<td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td>\n". "</tr>\n"; //Break each key of the array into a week and create a new table row for each week //with the days of the week in the table data $i = 0; foreach($weeks AS $week) { echo "<tr>\n"; while ($row = mysql_fetch_array($selectquery)) { $party_host_id = $row['id']; $party_host_name = $row['hostname']; $party_calendar_entry = strtotime($row['partydate']); if($party_calendar_entry == $date) { $host_link = "<a href=\"party_sign_up.php?id=".$party_host_id."\">".$party_host_name."</a>"; //echo "td class=\"days\">$host_link</td>\n"; }//if $party_calendar_entry=$date }//while foreach($week as $d) { if($i < $offset_count) { $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,($month - 1),$d,$year)."\">$d</a>"; echo "<td class=\"nonmonthdays\">$day_link.$host_link</td>\n"; } if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)) { if($date == mktime(0,0,0,$month, $d, $year)) { echo "<td class=\"today\">$d</td>\n"; } else { echo "<td class=\"days\"><a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month,$d,$year)."\">$d</a></td>\n"; } } elseif($i >= ($num_weeks * 7) - $outset) { $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month + 1,$d,$year)."\">$d</a>"; echo "<td class=\"nonmonthdays\">$day_link.$host_link</td>\n"; } }//foreach $week as $d $i++; echo "</tr>\n"; }//foreach $weeks as $week echo '<tr><td colspan="7" class="days"> </td></tr>'; echo '</table>'; ?> </div><!--end content--> </div><!--end wrapper--> </body> </html> Link to comment https://forums.phpfreaks.com/topic/72317-displaying-events-in-a-calendar/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.