Dispaly Events


All,
I am trying to display a calendear and then select an event from the db and change the shading of the calendar block if there is an event in the db. I am having troube displaying the events on teh calendar though. Here is the code I am trying to use. It only changes the color for one block but there are multiple events. I'm not sure how to get it to work for all the events.

if(isset($prm) and $prm > 0){ 
$m=$prm+$chm;}else{ 
$m= date("m");} 

$d= date("d");     // Finds today's date 
$y= date("Y");     // Finds today's year 

$no_of_days = date('t',mktime(0,0,0,$m,$d,$y)); // This is to calculate number of days in a month 

$mn=date('M',mktime(0,0,0,$m,$d,$y)); // Month is calculated to display at the top of the calendar 

$yn=date('Y',mktime(0,0,0,$m,$d,$y)); // Year is calculated to display at the top of the calendar 

$j= date('w',mktime(0,0,0,$m,1,$y)); // This will calculate the week day of the first day of the month 

for($k=1; $k<=$j; $k++){ // Adjustment of date starting 
$adj .="<td>&nbsp;</td>"; 
} 

/// Starting of top line showing name of the days of the week 

echo " <table border='1' bordercolor='#FFFF00' cellspacing='0' cellpadding='0' align=center> 

<tr><td>"; 

echo "<table cellspacing='0' cellpadding='0' align=center width='100' border='1'><td align=center bgcolor='#ffff00'><font size='3' face='Tahoma'> <a href='php_calendar.php?prm=$m&chm=-1'><</a> </td><td colspan=5 align=center bgcolor='#ffff00'><font size='3' face='Tahoma'>$mn $yn </td><td align=center bgcolor='#ffff00'><font size='3' face='Tahoma'> <a href='php_calendar.php?prm=$m&chm=1'>></a> </td></tr><tr>"; 

echo "<td><font size='3' face='Tahoma'><b>Sun</b></font></td><td><font size='3' face='Tahoma'><b>Mon</b></font></td><td><font size='3' face='Tahoma'><b>Tue</b></font></td><td><font size='3' face='Tahoma'><b>Wed</b></font></td><td><font size='3' face='Tahoma'><b>Thu</b></font></td><td><font size='3' face='Tahoma'><b>Fri</b></font></td><td><font size='3' face='Tahoma'><b>Sat</b></font></td></tr><tr>"; 

////// End of the top line showing name of the days of the week////////// 

//////// Starting of the days///////// 
$query = "Select date from events where id='1'"; 
$result = mysql_query($query); 
$resultset = mysql_fetch_array($result); 
for($i=1;$i<=$no_of_days;$i++){ 
$date = "$mn/$i/$yn"; 
if ($date == $resultset[date]){ 
echo $adj."<td valign=top bgcolor="CCCCCC"><font size='2' face='Tahoma'>$i<br>";  
echo "</font></td>"; }else{ echo $adj."<td valign=top><font size='2' face='Tahoma'>$i<br>";
echo "</font></td>"; } $adj=''; $j ++; if($j==7){echo "</tr><tr>"; $j=0;} } echo "</tr></table></td></tr></table>";

Any help is appreciated. Thanks

    Hi there,

    You're going to have to read up on how to retrieve data from a database.

    Also, best start tidying up your code ... on looking through, there are a few missing tags etc and indenting your code really helps to stop this happening.

    Here's some tips.
    1) Indent your code
    2) Try using a bit of CSS - cleans things up nicely
    3) Use the $_GET array

    Anyway, this was a nice little job to do for myself ... so here's my version.

    <?php
    if(isset($_GET['prm']) and $_GET['prm'] > 0){  
    $m = $_GET['prm'] + $_GET['chm']; } else {
    $m = date("m"); } $d = date("d"); // Finds today's date $y = date("Y"); // Finds today's year $no_of_days = date('t',mktime(0,0,0,$m,$d,$y)); // This is to calculate number of days in a month $mn = date('M',mktime(0,0,0,$m,$d,$y)); // Month is calculated to display at the top of the calendar $yn = date('Y',mktime(0,0,0,$m,$d,$y)); // Year is calculated to display at the top of the calendar $j = date('w',mktime(0,0,0,$m,1,$y)); // This will calculate the week day of the first day of the month $query = "SELECT date FROM events WHERE id = '1'"; $result = mysql_query($query); // Build an events array $event_dates = array(); // Cycle through the results like this ... while($resultset = mysql_fetch_array($result)){ $event_dates[] = $resultset['date']; } ?> <html> <head> <title></title> <style type="text/css"> * { font-family: Tahoma; } TABLE#cal TH, TABLE#calendar TD { border: 1px solid #C0C0C0; } TABLE#cal TH { width: 50px;} TABLE#cal TD.title { text-align: center; vertical-align: top; background-color:#FFFF00; } </style> </head> <body> <table id="cal" align="center" bordercolor="#FFFF00" border="1" cellpadding="0" cellspacing="0"> <tr> <td class="title"> <a href="php_calendar.php?prm=<?php echo $m; ?>&chm=-1"><</a> </td> <td colspan="5" class="title"> <?php echo $mn.' '.$yn; ?> </td> <td class="title"> <a href="php_calendar.php?prm=<?php echo $m; ?>&chm=1">></a> </td> </tr> <tr> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> </tr> <?php // Get the number of weeks (or rows) $no_of_weeks = ceil(($j + $no_of_days)/7); // Cycle through the weeks for($week_no = 0; $week_no < $no_of_weeks; $week_no++){ echo '<tr>'; // Cycle through the days (Sun=0 ... Sat=6) for($day_no = 0; $day_no < 7; $day_no++){ // Get the 'day of the month' ... this // could actually be negative or bigger than // the number of days in the month, in which // case echo out an empty cell. $day = 1 + $week_no*7 + $day_no - $j; if($day <= 0 || $no_of_days < $day){ echo '<td>&nbsp;</td>'; } else { $date = $mn.'/'.$day.'/'.$yn; // Check if this date is an event $class = in_array($date, $event_dates)? ' bgcolor="#CCCCCC"': ''; echo '<td'.$class.'>'.$day.'<br>&nbsp;</td>'; } } echo '<tr>'; } ?> </table> </body> </html>

    By the way, I wasn't not sure if you had dealt with the possible empty cells at the end.

    Paul 😉

      4 days later

      I used your code but modified it just a little bit to be. This code is called calendar.php.

      <?php
      if(isset($_GET['prm']) and $_GET['prm'] > 0){  
      $m = $_GET['prm'] + $_GET['chm']; } else {
      $m = date("m"); } $d = date("d"); // Finds today's date $y = date("Y"); // Finds today's year $no_of_days = date('t',mktime(0,0,0,$m,$d,$y)); // This is to calculate number of days in a month $mn = date('M',mktime(0,0,0,$m,$d,$y)); // Month is calculated to display at the top of the calendar $yn = date('Y',mktime(0,0,0,$m,$d,$y)); // Year is calculated to display at the top of the calendar $j = date('w',mktime(0,0,0,$m,1,$y)); // This will calculate the week day of the first day of the month $query = "SELECT calendardate FROM upload WHERE id = '$_SESSION[company]'"; $result = mysql_query($query); // Build an events array $event_dates = array(); // Cycle through the results like this ... while($resultset = mysql_fetch_array($result)){ $event_dates[] = $resultset['calendardate']; } ?> <html> <head> <title></title> <style type="text/css"> * { font-family: Tahoma; } TABLE#cal TH, TABLE#calendar TD { border: 1px solid #C0C0C0; } TABLE#cal TH { width: 50px;} TABLE#cal TD.title { text-align: center; vertical-align: top; background-color:#FFFF00; } </style> </head> <body> <table id="cal" align="center" bordercolor="#FFFF00" border="1" cellpadding="0" cellspacing="0"> <tr> <td class="title"> <a href="account.php?prm=<?php echo $m; ?>&chm=-1"><</a> </td> <td colspan="5" class="title"> <?php echo $mn.' '.$yn; ?> </td> <td class="title"> <a href="account.php?prm=<?php echo $m; ?>&chm=1">></a> </td> </tr> <tr> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> </tr> <?php // Get the number of weeks (or rows) $no_of_weeks = ceil(($j + $no_of_days)/7); // Cycle through the weeks for($week_no = 0; $week_no < $no_of_weeks; $week_no++){ echo '<tr>'; // Cycle through the days (Sun=0 ... Sat=6) for($day_no = 0; $day_no < 7; $day_no++){ // Get the 'day of the month' ... this // could actually be negative or bigger than // the number of days in the month, in which // case echo out an empty cell. $day = 1 + $week_no*7 + $day_no - $j; if($day <= 0 || $no_of_days < $day){ echo '<td>&nbsp;</td>'; } else { $date = $mn.'/'.$day.'/'.$yn; // Check if this date is an event $date = $mn.'/'.$day.'/'.$yn; // Check if this date is an event if (in_array($date, $event_dates)){ $class = in_array($date, $event_dates)? ' bgcolor="#CCCCCC"': ''; echo '<td'.$class.'><a href=\'account.php?calendardate='.$date.'>'.$day.'</a>&nbsp;</td>'; } else{ echo '<td>'.$day.'&nbsp;</td>'; } } } echo '<tr>'; } ?> </table> </body> </html>

      I then have the following code:

      include "calendar.php";
      $calendardate = $_GET['calendardate'];
      $queryfile  = "SELECT name FROM upload where id='$_SESSION[company]' and calendardate='$calendardate'";
      echo $queryfile;
      $resultfile = mysql_query($queryfile) or die(mysql_error());
      $resultsetfile = mysql_fetch_array($resultfile);
      
      echo "<table><tr><td>";
      include "http://$username:$password@www.website.com/folder/files/$resultsetfile[name]"; 
      echo "</td></tr></table>";
         // }
      	echo "<a href=\"welcome.php\">Main Page</a>";
      	echo "<br>";
      	echo "<a href=\"logout.php\">Logout</a>";
      
      

      For some reason the file displays in the block that is selected. So if the date of the file in the db is 10/17/2005 the file will display in block 10/17/2005 instead of below the file. I want the calendar to be displayed all the time and when they click on a date the associated file gets displayed below it and I can't seem to get iit. The calendar loads properly with the correct dates that have files from the db but I just can't get the file to display below it. Please help.

        If I have an include how is possible the file is being displayed in the include file and not the page where the query is run at???

        I've been trying to work this and can't come up with anything.

        Please help

          Write a Reply...