PHP while while循环和表头只显示数据库值是否设置
问题描述:
这是我的代码。PHP while while循环和表头只显示数据库值是否设置
<table border="1" style="width:800px">
<?php $schedule_db = mysql_query("SELECT * FROM schedule WHERE '00:00' is not null and '01:00' is not null and '02:00' is not null and '03:00' is not null and '04:00' is not null and '05:00' is not null and '06:00' is not null and '07:00' is not null and '08:00' is not null and '09:00' is not null and '10:00' is not null and '11:00' is not null and '12:00' is not null and '13:00' is not null and '14:00' is not null and '15:00' is not null and '16:00' is not null and '17:00' is not null and '18:00' is not null and '19:00' is not null and '20:00' is not null and '21:00' is not null and '22:00' is not null and '23:00' is not null")
or die(mysql_error()); ?>
<form method="post" action="config_action.php">
<?php while($schedule = mysql_fetch_array($schedule_db)) { ?>
<tr><?php if(isset($schedule['00:00'])) { ?><td style="width:32px"><?php echo $schedule['00:00'] ?></td><?php } if(isset($schedule['02:00'])) { ?><td style="width:32px"><?php echo $schedule['02:00'] ?></td><?php } if(isset($schedule['03:00'])) { ?><td style="width:32px"><?php echo $schedule['03:00'] ?></td><?php } if(isset($schedule['04:00'])) { ?><td style="width:32px"><?php echo $schedule['04:00'] ?></td><?php } if(isset($schedule['05:00'])) { ?><td style="width:32px"><?php echo $schedule['05:00'] ?></td><?php } if(isset($schedule['06:00'])) { ?><td style="width:32px"><?php echo $schedule['06:00'] ?></td><?php } if(isset($schedule['07:00'])) { ?><td style="width:32px"><?php echo $schedule['07:00'] ?></td><?php } if(isset($schedule['08:00'])) { ?><td style="width:32px"><?php echo $schedule['08:00'] ?></td><?php } if(isset($schedule['09:00'])) { ?><td style="width:32px"><?php echo $schedule['09:00'] ?></td><?php } if(isset($schedule['10:00'])) { ?><td style="width:32px"><?php echo $schedule['10:00'] ?></td><?php } if(isset($schedule['11:00'])) { ?><td style="width:32px"><?php echo $schedule['11:00'] ?></td><?php } if(isset($schedule['12:00'])) { ?><td style="width:32px"><?php echo $schedule['12:00'] ?></td><?php } if(isset($schedule['13:00'])) { ?><td style="width:32px"><?php echo $schedule['13:00'] ?></td><?php } if(isset($schedule['14:00'])) { ?><td style="width:32px"><?php echo $schedule['14:00'] ?></td><?php } if(isset($schedule['15:00'])) { ?><td style="width:32px"><?php echo $schedule['15:00'] ?></td><?php } if(isset($schedule['16:00'])) { ?><td style="width:32px"><?php echo $schedule['16:00'] ?></td><?php } if(isset($schedule['17:00'])) { ?><td style="width:32px"><?php echo $schedule['18:00'] ?></td><?php } if(isset($schedule['19:00'])) { ?><td style="width:32px"><?php echo $schedule['19:00'] ?></td><?php } if(isset($schedule['20:00'])) { ?><td style="width:32px"><?php echo $schedule['20:00'] ?></td><?php } if(isset($schedule['21:00'])) { ?><td style="width:32px"><?php echo $schedule['21:00'] ?></td><?php } if(isset($schedule['22:00'])) { ?><td style="width:32px"><?php echo $schedule['22:00'] ?></td><?php } if(isset($schedule['23:00'])) { ?><td style="width:32px"><?php echo $schedule['23:00'] ?></td><?php } ?></tr>
<?php } ?>
</form>
</table>
我想要做的是有一个表头(只是所有其他行上述行),显示什么时间每列但前提是该列的值。
我想我的代码是这样的:
<tr><?php if(isset($schedule['00:00'])) { ?><td style="width:32px">00:00</td><?php } if(isset($schedule['01:00'])) { ?><td style="width:32px">01:00</td><?php } ?> etc.
然而,因为它是在一个while循环,它会在每次实际行,这不是我希望发生的事情发生之前,我只是希望它在顶部发生一次。
我怎样才能解决这个问题?有什么我可以附加到,使它只循环一次?
谢谢!
答
然后不要在while循环内输出你的行。将所有数据保存到var中。然后,如果var包含内容,则放出headrow,之后包含所有内容行的var。
编辑
好吧这是我写过的最丑陋的代码,你很可能使用数组和东西使代码更小。但这是获取想法的代码片段...捕获所有行。这样做时会检查每个字段是否填写完整。如果是这样,那么您需要在head头中有一列。我们通过将data0
设置为true
来实现此目的。处理完整个数据后,我们检查每个变量并查看它是否为真。如果是这样,那么你在头顶上打印柱子。所以最后你会看到,你输出表格标签,然后是headrow-var,然后是具有所有格式化数据的字符串,然后是结束表格标签。所以你得到了基本的想法。将头发和其他排放在分开的变量中,并按正确的顺序排列出来...
// Get data from database-table...
$schedule_db = mysql_query('...your query...') or die(mysql_error());
$data0 = false;
$data1 = false;
$data2 = false;
// reapeat this for all hours till 23...
while($schedule = mysql_fetch_array($schedule_db))
{
if(isset($schedule['00:00']))
{
$data0 = true;
$row = '<td style="width: 32px;">' . $schedule['00:00'] . '</td>';
}
// Reapeat this for every hour you need...
if(isset($schedule['01:00']))
{
$data1 = true;
$row .= '<td style="width: 32px;">' . $schedule['01:00'] . '</td>';
}
//...
$rows .= $row;
}
$headrow = '<tr>';
if($data0 === true)
$headrow .= '<th>00:00</th>'
if($data1 === true)
$headrow .= '<th>01:00</th>';
// Repeat for all hours...
$headrow = '</tr>';
echo '<table>';
echo $headrow;
echo $rows;
echo '</table>';
您能否重新说明这一点?这不是很清楚。 – Sam 2010-06-24 11:32:19
我编辑了我的帖子。这是比丑陋,但我希望你能得到基本的想法... – 2010-06-24 12:27:41
我明白了,谢谢:)。 – Sam 2010-06-24 13:04:04