所以我有这个代码组成了一个时间表网格(日期和时间),我连接到数据库并选择表格,你可以看到...我想要做的是例如当班级英语从0900开始星期一,我想将这些信息插入网格,我怎么能实现这个目标呢?非常感谢
<?php
error_reporting(E_ALL);
mysql_connect('db','user','password')
or die(mysql_error());
mysql_select_db('db')
or die(mysql_error());
$sql = "select day,start,class,time
from event where class='english'";
$res = mysql_query($sql)
or die(mysql_error());
$days = array('Monday','Tuesday','Wednesday','Thursday','Friday');
$times= array('09','10','11','12','13','14','15','16');
echo "<table id='grid'>\n";
echo "<tr><td></td>";
for($t=0;$t<count($times);$t++)
echo "<th>$times[$t]:00</th>";
echo "<tr>\n";
for($d=0;$d<count($days);$d++){
print "<tr><th>$days[$d]</th>";
for($t=0;$t<count($times);$t++)
echo "<td id='td_$days[$d]_$times[$t]'></td>";
echo "</tr>\n";
}
echo "</table>\n";
?>
答案 0 :(得分:0)
首先为数组使用foreach()
foreach($times as $time)
echo "<th>$time:00</th>";
其次,尽量不要使用html来破坏php。 所有这些都使您的代码至少更清晰。 对于db结果,您应该使用mysql_fetch_array();
比这样:
<table id='grid'>
<tr>
<td></td>
<?php
foreach ($times as $time)
echo "<th>$time:00</th>";
?>
<tr>
<?php
foreach ($days as $day){
echo "<tr><th>$day</th>";
foreach ($times as $time){
$english = ($day == $res['day'] && $time == $res['time']) ? $res['class'] : '';
echo "<td id='td_$day_$time'>{$class}</td>";
}
}
?>
</tr>
</table>