我有一个名为约会的表格,字段是医生,日期,时间,appuser(即患者)。我想打印链接中显示的第一个表。我已经显示了第二个,但现在我想把它改成第一个。
这是我目前第二个表的代码:
<div id="grid">
<ul>
<li>Time</li>
<li>Doctor Name</li>
<li>Patient Name</li>
</ul>
</div>
<?php
if($_POST['submit'] == "Search")
{
$select= $_POST['datesel'];
$s = mysql_query("select * from appntt where date = '".$select."'");
}
else
{
$d= date("j/n/Y");
$s=mysql_query("select * from appointment where date = '".$d."' ORDER BY time ASC");
}
$a=1;
while($fetch=mysql_fetch_array($s))
{
if($fetch['time'] > '12:00')
{
$y = $fetch['time'].' PM';
}
else
{
$y= $fetch['time'].' AM';
}
?>
<div id="grid1">
<ul>
<li><?php echo $y; ?></li>
<li><?php echo $fetch['doctor']; ?></li>
<li><?php echo $fetch['appuser']; ?></li>
</ul>
</div>
<?php $a++; } ?>
答案 0 :(得分:1)
首先应该在php中的数组中对结果进行排序,然后根据此数组打印表。
首先,我们需要一个数组来对查询结果进行排序。这个数组到底会是二维的。我们还需要记住所有医生,因为从排序的约会中提取它们会更有效:
$table = []; //The array for the sorted appointments
$doctors = []; //The array for the doctors
然后我们需要处理查询结果并将它们排序到数组中:
while($fetch=mysql_fetch_array($s)){
// The keys of the first dimension will be the appointment time
// and the key of the second dimension will be the doctor
// So we can access a specific appointment by $table[time][doctor]
$table[$fetch['time']][$fetch['doctor'] = $fetch['appuser'];
//save the doctors name
$doctors[] = $fetch['doctor'];
}
array_unique($doctors); // delete duplicate values, so that we have just
// the names of all the doctors
现在我们需要打印表格:
> <table>
<tr>
<td> TIME </td> <?
foreach($doctors as $doctor){
> <td> <? echo $doctor > </td> <? //print the header
}
> </tr> <?
// Depending on the result of your query,
// you might need to order the array first or the times will be mixed up.
foreach($array as $time=>$appointments){ //loop over all the times.
> <tr>
<td> <? echo $time; > </td> <? //first column: print appointment time
foreach($doctors as $doctor){
> <td> <?
if($appointments[$doctor] != null){
echo $appointments[$doctor]; //other columns: print patient name
}
// if there is no appointment, you might need to add a
// non-breakable-space to the cell
> </td> <?
}
> </tr> <?
}
> </table>
这有望成功。我不能保证它开箱即用,因为我无法测试它,但我认为你可以得到这个想法。
旁注:从不将用户的值直接插入数据库查询!这使您容易受到SQL注入的攻击。</ p>