我使用这个PHP代码来显示日历。
<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
?>
<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
?>
<?php
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
$next_month = 1;
$next_year = $cYear + 1;
}
?>
<table width="100%">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a> </td>
</tr>
</table></td>
</tr>
<tr>
<td align="center"><table width="100%" border="1" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
</tr>
<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday); $i++)
{
$sql="SELECT * from calendar where date = '".$cYear."-".$cMonth."-".($i - $startday + 1)."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_array($rs);
if(($i % 7) == 0 )
{
echo '<tr>';
}
if($i < $startday)
{
echo '<td></td>';
}
else
{
echo '<td align="center" valign="middle" height="80px" width="80px"><a href="add.php?date='.($i - $startday + 1).'-'.$cMonth.'-'.$cYear.'">'. ($i - $startday + 1) . '</a><br>'.$result["title"].'</td>';
}
if(($i % 7) == 6 )
{
echo '</tr>';
}
}
?></table></td>
</tr>
</table>
它工作得很好,但是我在底部附近添加了一个select sql查询,它将显示MySQL中某个日期的某些行的行,但是我无法弄清楚如何让它显示该日期的所有行。我试过用这个作为一个循环但没有运气。如果有多行,我怎么能让它显示每个日期的所有行?
答案 0 :(得分:1)
您应该在查询中使用GROUP BY
$sql="SELECT * from calendar where date = '".$cYear."-".$cMonth."-".($i - $startday + 1)."' GROUP BY the_column_that_you_sort_your_table ";