将值插入网格-php、sql
所以,我有这个代码,它组成了一个时间表网格(天和时间),我连接到一个数据库,并选择表,你可以看到。。。我想做的是,例如,当英语课在星期一早上9点开始时,我想把这些信息插入表格,我怎样才能做到这一点?非常感谢将值插入网格-php、sql,php,mysql,database,Php,Mysql,Database,所以,我有这个代码,它组成了一个时间表网格(天和时间),我连接到一个数据库,并选择表,你可以看到。。。我想做的是,例如,当英语课在星期一早上9点开始时,我想把这些信息插入表格,我怎样才能做到这一点?非常感谢 <?php error_reporting(E_ALL); mysql_connect('db','user','password') or die(mysql_error()); mysql_select_db('db') or die(mysql_error()); $
<?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";
?>
首先对数组使用foreach()
foreach($times as $time)
echo "<th>$time:00</th>";
foreach($times as$time)
回显“$time:00”;
第二,尽可能不要将php与html混为一谈。
所有这些至少使您的代码更干净。
对于数据库结果,您应该使用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>