I'm developing a calendar for a client. I want to list all the events for a certain day (can be more than one event per day), and here is the code I have for that:
//Limit a MySQL query to selected month only.
$low = mktime(0, 0, 0, $month, 1, $year);
$high = mktime(0, 0, 0, $month, $max_days, $year);
$qry = "SELECT * FROM events WHERE author=\"$author\" AND unixdate BETWEEN \"$low\" AND \"$high\" ORDER BY unixdate ASC";
//Return query into array, internal pointer will advance each time array is accessed.
$result = mysql_query($qry) or die(mysql_error());
$row = mysql_fetch_array($result);
$eventmonth = date("n", $row['unixdate']);
$eventday = date("j", $row['unixdate']);
$selected = " selected-day";
//Generate the days of the selected month up to max_days.
//The while-loop will generate any/all events listed on that day,
//then move on to the next day to test it again.
for ($i = 1; $i <= $max_days; $i++) {
if ($i == $day && $month == (int) date("m", $date)) {
$selected = " selected-day";
} else {
$selected = "";
}
//Start new cell.
echo "<td class=\"day$selected\"><span class=\"day-number\">$i</span>";
while ($i == $eventday && $month == $eventmonth) {
echo $row['title'];
$row = mysql_fetch_array($result);
$eventday = date("j", $row['unixdate']);
}
//End cell.
echo "</td>\n";
//Check if today is the beginning of a new week.
//If so, end current row and start a new one.
if (newWeek($day_count)) {
echo "</tr><tr>\n";
}
//Increment day_count.
//Each iteration of this loop only generates one cell.
$day_count++;
}
My problem is: when it gets to the last day of the month, the loop does not end. What is causing this?
By changing the while loop to an if statement, it works just as I want. But in order to support multiple events on the same day, I need it to be a while loop.
$max_days is defined in a separate file and included into this one.
//Limit a MySQL query to selected month only.
$low = mktime(0, 0, 0, $month, 1, $year);
$high = mktime(0, 0, 0, $month, $max_days, $year);
$qry = "SELECT * FROM events WHERE author=\"$author\" AND unixdate BETWEEN \"$low\" AND \"$high\" ORDER BY unixdate ASC";
//Return query into array, internal pointer will advance each time array is accessed.
$result = mysql_query($qry) or die(mysql_error());
$row = mysql_fetch_array($result);
$eventmonth = date("n", $row['unixdate']);
$eventday = date("j", $row['unixdate']);
$selected = " selected-day";
//Generate the days of the selected month up to max_days.
//The while-loop will generate any/all events listed on that day,
//then move on to the next day to test it again.
for ($i = 1; $i <= $max_days; $i++) {
if ($i == $day && $month == (int) date("m", $date)) {
$selected = " selected-day";
} else {
$selected = "";
}
//Start new cell.
echo "<td class=\"day$selected\"><span class=\"day-number\">$i</span>";
while ($i == $eventday && $month == $eventmonth) {
echo $row['title'];
$row = mysql_fetch_array($result);
$eventday = date("j", $row['unixdate']);
}
//End cell.
echo "</td>\n";
//Check if today is the beginning of a new week.
//If so, end current row and start a new one.
if (newWeek($day_count)) {
echo "</tr><tr>\n";
}
//Increment day_count.
//Each iteration of this loop only generates one cell.
$day_count++;
}
My problem is: when it gets to the last day of the month, the loop does not end. What is causing this?
By changing the while loop to an if statement, it works just as I want. But in order to support multiple events on the same day, I need it to be a while loop.
$max_days is defined in a separate file and included into this one.