while loop in a for php
Building a calendar with events. I have built the calendar and now trying to add the events to the corresponding date.
$dayName = "Monday";
$mo = date($m);
for($day = 1; $day <=7; $day++)
{
$timestamp = mktime(1,1,1,$mo,$day,$yr);
if(date('l', $timestamp) == $dayName)
{
if(date('d', $timestamp) == 1){
$num = cal_days_in_month(CAL_GREGORIAN, $m, $yr);
for($date = 1; $date <=$num; $date++){
if ($date == 1){
print "<tr>";
}
print "<td";if($mo == date("m")){ if($date == date("d")){ if($yr == date("y")){ print " style=\"background:#97d2fd;\"";}}}print " onClick=\"location.href = '?v=dd&id=$m&y=$yr&mid=$mm&my=$myr&d=$date';\" height=\"80\">". $date."<br/>";
$wqry = mysql_query("SELECT * FROM events")or die(mysql_error());
if (mysql_num_rows($wqry) > 0){
//need to be a while loop
$wrow = mysql_fetch_array($wqry, MYSQL_ASSOC) or die(mysql_error());
$alldate = $date."/".$mo."/".$yr;
if($alldate == $wrow['sdate']){
if($wrow['type'] == "1"){
print"<div id=\"hight\">";
}print $wrow['title']."</div><br/>";
}
}
print"</td> ";
if ($date == 7){
print "</tr><tr>";
}
if ($date == 14){
print "</tr><tr>";
}
if ($date == 21){
print "</tr><tr>";
}
if ($date == 28){
print "</tr>";
}
}
}
break;
print"
</tbody>
</table>
</div>
</div>
</div>
";
}
}
Trying to make it so that the events are shown in the right table. So far this code shows just one event. Im guessing the problem is putting a while loop in a for. As when I do:
if (mysql_num_rows($wqry) > 0){
while($wrow = mysql_fetch_array开发者_如何学Python($wqry, MYSQL_ASSOC) or
die(mysql_error())){ $alldate = $date."/".$mo."/".$yr;
if($alldate == $wrow['sdate']){ if($wrow['type'] == "1"){ print"<div id=\"hight\">"; }print $wrow['title']."</div><br/>"; } } }
It just shows the first date of the month. Meaning that it must stop the for loop.
It's because its just returning the first row from your database each time as its looping for each day in the month, you need to replace the query with something like:
$wqry = mysql_query("SELECT * FROM events WHERE sdate='$alldate'")
精彩评论