以下代码显示表单上的表格,但我需要以特定方式格式化表格
所需的输出显示在图像中。所以我需要操纵查询,我可以将Sprinkler_ID列转换为1,2,3,4,5,6,7,8。所以,如果没有Sprinkler_ID,请说" 2"在我们原来的表中,然后所需的表格应打印出来#34; no"在" 2"否则是字段。
<?php
include('config.php');
echo "<h4>Current Schedules</h4>";
echo "<table border=2 >";
echo "<tr>";
echo "<td><b>Sprinkler_Id</b></td>";
echo "<td><b>Schedule_Id</b></td>";
echo "<td><b>Starttime</b></td>";
echo "<td><b>Stoptime</b></td>";
echo "<td><b>Monday</b></td>";
echo "<td><b>Tuesday</b></td>";
echo "<td><b>Wednesday</b></td>";
echo "<td><b>Thursday</b></td>";
echo "<td><b>Friday</b></td>";
echo "<td><b>Saturday</b></td>";
echo "<td><b>Sunday</b></td>";
echo "</tr>";
$sql = "SELECT daystime.*, Sprinkler_ID FROM daystime, scheduler WHERE daystime.id = scheduler.DaysTime_ID ORDER BY Sprinkler_ID, daystime.id";
$results=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($results))
{
$id = $row['id'];
echo "<tr>";
echo "<td valign='top'>" . nl2br( $row['Sprinkler_ID']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['id']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['starttime']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['stoptime']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['mon']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['tue']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['wed']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['thu']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['fri']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['sat']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['sun']) . "</td>";
echo "</tr>";
}
echo "</table>";
?>
答案 0 :(得分:1)
您必须先自定义创建不存在的1-8列,以便执行此操作:
//...continued from code
echo "<td><b>Saturday</b></td>";
echo "<td><b>Sunday</b></td>";
for($i=1;$i<=8;$i++){
echo "<td><b>".$i."</b></td>";
}
echo "</tr>";
然后,我们使用以下内容:
$ids=array();
#we'll first save all the ids into an array
while($row=mysql_fetch_array($results))
{
$ids[$row['id']][]=$row['Sprinkler_ID'];
}
$listed=array();
while($row=mysql_fetch_array($results))
{
$id = $row['id'];
if(!in_array($id,$listed)){
echo "<tr>";
echo "<td valign='top'>" . nl2br( $row['id']) . "</td>";
//...all your other rows
for($i=1;$i<=8;$i++){
if(in_array($i,$ids[$id])){
echo "<td valign='top'>yes</td>";
}
else{
echo "<td valign='top'>no</td>";
}
}
echo "</tr>";
$listed[]=$id;
}
}
逻辑是,我们首先将Sprinkler_id
的重复id
分组为:
19 =>
[0] => 4,
[1] => 5,
20 =>
[0] => 5,
21 =>
[0] => 4,
[1] => 6
然后,当它循环遍历id
时,我们确保使用$listed[]
数组仅打印一行。对于“是”和“否”,我们使用先前分组的数组来检查sprinkler_id
的{{1}}是否存在。如果是的话,那就是'是'否则'不'。
答案 1 :(得分:0)
为了实现这一点,在php文件中为Sprinkle Ids维护一个静态数组,然后检查来自Db的值是否存在于此数组中
<?php
include('config.php');
$sprinkleIds = range(1,8);
echo "<h4>Current Schedules</h4>";
echo "<table border=2 >";
echo "<tr>";
echo "<td><b>Sprinkler_Id</b></td>";
echo "<td><b>Schedule_Id</b></td>";
echo "<td><b>Starttime</b></td>";
echo "<td><b>Stoptime</b></td>";
echo "<td><b>Monday</b></td>";
echo "<td><b>Tuesday</b></td>";
echo "<td><b>Wednesday</b></td>";
echo "<td><b>Thursday</b></td>";
echo "<td><b>Friday</b></td>";
echo "<td><b>Saturday</b></td>";
echo "<td><b>Sunday</b></td>";
echo "</tr>";
$sql = "SELECT daystime.*, Sprinkler_ID FROM daystime, scheduler WHERE daystime.id = scheduler.DaysTime_ID ORDER BY Sprinkler_ID, daystime.id";
$results=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($results))
{
$id = $row['id'];
echo "<tr>";
if( in_array( $row['Sprinkler_ID'], $sprinkleIds ) {
$text = 'Yes';
} else {
$text = 'No';
}
echo "<td valign='top'>" . $text . "</td>";
echo "<td valign='top'>" . nl2br( $row['id']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['starttime']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['stoptime']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['mon']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['tue']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['wed']) . "</td>";
echo "<td valign=
'top'>" . nl2br( $row['thu']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['fri']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['sat']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['sun']) . "</td>";
echo "</tr>";
}
echo "</table>";
?>