adam291086 Posted December 5, 2007 Share Posted December 5, 2007 OK i have a database connected to a calendar. What i want it to do is highlight all the days that have an event. At the moment it only highlights one out of the two. I understand i need to put a while loop in but this causes the last event date to be repeated over and over and over again. This is what i have so far for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) { $rowcounter ++; while ($counter == $day) { echo "<td bgcolor=#c0c0c0><strong>$counter</strong></td>"; } If (!$counter == $day) { echo "<td width='14%'>$counter</td>"; } The calendar works by adding one to a counter if the counter is under the current number of days within a month. This is where $day is set $result = mysql_query( " SELECT * FROM `table` WHERE `Month` = $thismonth ") or die('Query failed. ' . mysql_error()); while($row = mysql_fetch_array($result)) { $day = $row['Day'] ; $Month = $row['Month'] ; $Year = $row['Year'] ; Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 5, 2007 Author Share Posted December 5, 2007 anyone? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 5, 2007 Share Posted December 5, 2007 post your full code its hard to tell. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 5, 2007 Author Share Posted December 5, 2007 here is the full code. At the moment it just repeats the last record from the query results. <!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" /> <title>Calendar</title> </head> <body> <?php //current day $weekday = date("d"); // get this month and this years as an int $thismonth = ( int ) date( "m" ); $thisyear = date( "Y" ); //get database info include 'config.php'; $result = mysql_query( " SELECT * FROM `table` WHERE `Month` = $thismonth ") or die('Query failed. ' . mysql_error()); while($row = mysql_fetch_array($result)) { $day = $row['Day'] ; $Month = $row['Month'] ; $Year = $row['Year'] ; echo $day; echo " "; echo $Month; echo " "; echo $Year; } // find out the number of days in the month $numdaysinmonth = cal_days_in_month( CAL_GREGORIAN, $thismonth, $thisyear ); // create a calendar object $jd = cal_to_jd( CAL_GREGORIAN, date( "m" ),date( 1 ), date( "Y" ) ); // get the start day as an int (0 = Sunday, 1 = Monday, etc) $startday = jddayofweek( $jd , 0 ); // get the month as a name $monthname = jdmonthname( $jd, 1 ) ?> <table border="1"> <tr> <td colspan="7"><div align="center"><strong><?= $monthname ?></strong></div></td> </tr> <tr> <td><strong>S</strong></td> <td><strong>M</strong></td> <td><strong>T</strong></td> <td><strong>W</strong></td> <td><strong>T</strong></td> <td><strong>F</strong></td> <td><strong>S</strong></td> </tr> <tr> <?php // put render empty cells $emptycells = 0; for( $counter = 0; $counter < $startday; $counter ++ ) { echo "\t\t<td>-</td>\n"; $emptycells ++; } // renders the days $rowcounter = $emptycells; $numinrow = 7; for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) { $rowcounter ++; while ($counter == $day) { echo "<td bgcolor=#c0c0c0><strong>$counter</strong></td>"; } If (!$counter == $day) { echo "<td width='14%'>$counter</td>"; } $rowcounter ++; if( $rowcounter % $numinrow == 0 ) { echo "\t</tr>\n"; if( $counter < $numdaysinmonth ) { echo "\t<tr>\n"; } $rowcounter = 0; } } // clean up $numcellsleft = $numinrow - $rowcounter; if( $numcellsleft != $numinrow ) { for( $counter = 0; $counter < $numcellsleft; $counter ++ ) { echo "\t\t<td>-</td>\n"; $emptycells ++; } } ?> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 5, 2007 Share Posted December 5, 2007 try out this while($row = mysql_fetch_array($result)) { $day = $row['Day'] ; $Month = $row['Month'] ; $Year = $row['Year'] ; echo $day; echo " "; echo $Month; echo " "; echo $Year; } should be $arrDays = array(); while($row = mysql_fetch_array($result)) { $arrDays[] = $row['Day'] ; $Month = $row['Month'] ; $Year = $row['Year'] ; } and while ($counter == $day) { echo "<td bgcolor=#c0c0c0><strong>$counter</strong></td>"; } If (!$counter == $day) { echo "<td width='14%'>$counter</td>"; } should be if (in_array($counter,$arrDays)) { echo "<td bgcolor=#c0c0c0><strong>$counter</strong></td>"; } else { echo "<td width='14%'>$counter</td>"; } hope its helpful... Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 5, 2007 Author Share Posted December 5, 2007 It works beautiful. Thanks : Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.