Drewser33 Posted August 29, 2008 Share Posted August 29, 2008 I used some code I found on the internet to make a calendar, which the calendar part is working but now I want to insert information into the cells/days that are created by the calendar. Below is the code I am using now but it is only displaying the last database rows information. Is it possible to loop inside the loop? And then only display it once? Because if I change my code to have the while include the 'td' I get two displays of everyday. THANKS!! <?php if(isset($_GET['prm'])) { $prm = $_GET['prm']; $chm = $_GET['chm']; $m=$prm+$chm; } else { $m= date("m"); } $month = date('F'); $d= date("d"); // Finds today's date $y= date("Y"); // Finds today's year $no_of_days = date('t',mktime(0,0,0,$m,1,$y)); // This is to calculate number of days in a month $mn=date('M',mktime(0,0,0,$m,1,$y)); // Month is calculated to display at the top of the calendar $yn=date('Y',mktime(0,0,0,$m,1,$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++) { $adj .="<td> </td>"; } echo $string; /// 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='750' border='1'><td align=center bgcolor='#ffff00'><font size='3' face='Tahoma'> <a href='mycalendar.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='mycalendar.php?prm=$m&chm=1'>></a> </td></tr><tr>"; echo "<td width=14.2%><font size='3' face='Tahoma'><b>Sun</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Mon</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Tue</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Wed</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Thu</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Fri</b></font></td><td width=14.2%><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////////// for($i=1;$i<=$no_of_days;$i++) { $daytotime = ($i.' '.$mn.' '.$yn); $string = strtotime($daytotime); $query = "SELECT * FROM calendar"; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fetch_array($result)) { $t = $row['startDate']; $u = $row['endDate']; if($string >= $t && $string <= $u) { $infonext = $row['title']; }else { $infonext = ''; } } echo $adj."<td valign=top height=100><font size='2' face='Tahoma'>$i<br></font><font size='1' color='#ff0000'>$infonext"; // This will display the date inside the calendar cell; echo " </font></td>"; $adj=''; $j ++; if($j==7) { echo "</tr><tr>"; $j=0; } } //echo $infonext; ?> Link to comment https://forums.phpfreaks.com/topic/121875-solved-looping-display-problem/ Share on other sites More sharing options...
rhodesa Posted August 29, 2008 Share Posted August 29, 2008 side note...save yourself time and use Google Calendar. In there, create a calendar, then go to the settings for it. There is a spot where it gives you code to 'embed it in a website'. Then just use Google Calendar to keep the stuff up to date. Link to comment https://forums.phpfreaks.com/topic/121875-solved-looping-display-problem/#findComment-628828 Share on other sites More sharing options...
cooldude832 Posted August 29, 2008 Share Posted August 29, 2008 its because you are echoing outside your collection of the query results loop so those vars you set are going to be equal to the last value when echoed. store those values in an array in the while($row) part and then use that array to produce in the fields. At rhodesa: google is good but a lot of times you don't get the flexibility you want. Link to comment https://forums.phpfreaks.com/topic/121875-solved-looping-display-problem/#findComment-628836 Share on other sites More sharing options...
Drewser33 Posted August 29, 2008 Author Share Posted August 29, 2008 I have been on php.net trying to build the array correctly but it is not going very well for me. Would it be possible to show an example code that would build the array correctly? THANKS!! Link to comment https://forums.phpfreaks.com/topic/121875-solved-looping-display-problem/#findComment-628869 Share on other sites More sharing options...
cooldude832 Posted August 29, 2008 Share Posted August 29, 2008 <?php while($row = mysql_fetch_assoc($r)){ $events[$row['eventid']]['Date'] = $row['Date']; $events[$row['eventid']]['Location'] = $row['Location']; $events[$row['eventid']]['Time'] = $row['Time']; } while(ECHO CALENDAR){ #find matches in array $events for the current date stamp and if match echo it Link to comment https://forums.phpfreaks.com/topic/121875-solved-looping-display-problem/#findComment-628936 Share on other sites More sharing options...
Drewser33 Posted August 29, 2008 Author Share Posted August 29, 2008 Thanks for the help, but I couldn't get it working with the arrays, but this is how I got it without using arrays -> <?php include 'includes/config.php'; include 'includes/opendb.php'; if(isset($_GET['prm'])) { $prm = $_GET['prm']; $chm = $_GET['chm']; $m=$prm+$chm; } else { $m= date("m"); } $month = date('F'); $d= date("d"); // Finds today's date $y= date("Y"); // Finds today's year $no_of_days = date('t',mktime(0,0,0,$m,1,$y)); // This is to calculate number of days in a month $mn=date('M',mktime(0,0,0,$m,1,$y)); // Month is calculated to display at the top of the calendar $yn=date('Y',mktime(0,0,0,$m,1,$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++) { $adj .="<td> </td>"; } echo $string; /// 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='750' border='1'><td align=center bgcolor='#ffff00'><font size='3' face='Tahoma'> <a href='mycalendar.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='mycalendar.php?prm=$m&chm=1'>></a> </td></tr><tr>"; echo "<td width=14.2%><font size='3' face='Tahoma'><b>Sun</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Mon</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Tue</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Wed</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Thu</b></font></td><td width=14.2%><font size='3' face='Tahoma'><b>Fri</b></font></td><td width=14.2%><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////////// for($i=1;$i<=$no_of_days;$i++) { $daytotime = ($i.' '.$mn.' '.$yn); $string = strtotime($daytotime); echo $adj."<td valign=top height=100><font size='2' face='Tahoma'>$i<br></font><font size='1' color='#ff0000'>"; $query = "SELECT * FROM calendar"; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fetch_array($result)) { $startDate = $row['startDate']; $endDate = $row['endDate']; $title = $row['title']; if($string >= $startDate && $string <= $endDate) { echo $title."<br />"; // This will display the date inside the calendar cell; } } echo " </font></td>"; $adj=''; $j ++; if($j==7) { echo "</tr><tr>"; $j=0; } } ?> Link to comment https://forums.phpfreaks.com/topic/121875-solved-looping-display-problem/#findComment-629065 Share on other sites More sharing options...
cooldude832 Posted August 29, 2008 Share Posted August 29, 2008 its a poor way to do it because you are looping your result through for each day you have in your calendar which is 30*(x) where X is the number of events so you could have it looping through 100s of times. Link to comment https://forums.phpfreaks.com/topic/121875-solved-looping-display-problem/#findComment-629258 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.