我知道如何在html表上打印来自数据库的数据,但我有一个任务我无法理解,而我无法完成。 我在数据库中的表,当我选择这些数据我想他们显示在HTML表,但这样的事情:在html中打印数据库数据表
Image http://img683.imageshack.us/img683/9350/tablea.png
我知道如何在html表上打印来自数据库的数据,但我有一个任务我无法理解,而我无法完成。 我在数据库中的表,当我选择这些数据我想他们显示在HTML表,但这样的事情:在html中打印数据库数据表
Image http://img683.imageshack.us/img683/9350/tablea.png
取得一个表的布局一样,你只需要条件的放置上表格行的结尾。
<table>
<tr>
<?php
$count = 0;
while($row = mysql_fetch_assoc($result)) :
$count++;
?>
<td><?php echo $row['value'] ?></td>
<?php if($count % 2 == 0 || $count == mysql_num_rows($res)) : ?>
</tr>
<?php if($count != mysql_num_rows($result)) : ?>
<tr>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
</table>
什么上面所做的是使用模运算符(%
,计算从余数)来打印关闭和打开排标签时,我们正处在一个均匀的编号结果。
另外,如果你想改变你的表格布局为3个或4列宽,所有你需要做的是改变应用到模数:
$count % 3 == 0 //3 columns
$count % 4 == 0 //4 columns, etc
<table>
<?php while (TRUE) { ?>
<?php
// Make an array of n rows. Trailing items may be FALSE if there
// are not enough rows to fill the table row.
//
$rows= array();
for ($i= 0; $i<n; $i++)
$rows[$i]= mysql_fetch_assoc($result);
if (!$row[0])
break;
?>
<tr>
<?php foreach ($rows as $row) { ?>
<td>
<?php if ($row) { ?>
Value: <?php echo(htmlspecialchars($row['value']); ?>
<?php } ?>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
的很好的例子面条编码 – Jacco 2010-02-16 12:11:19
你在说什么?这是一个单一的一致的标记树,与“意大利面代码”完全相反。您可以从缩进中看到每个元素和PHP结构开始和结束的位置,这使得它比隐藏在大块PHP中的随机“回声”更易于维护。 – bobince 2010-02-16 13:28:15
我认为如果它在解析和输出模式之间使用较少的切换,它会更具可读性。我读了你的'正确的PHP教程'(http://stackoverflow.com/questions/2119083/php-tutorial-that-is-security-accuracy-and-maintainability-conscious)问题,我同意你的观点,除了一件事:从一开始就教会新的语言使用模板系统。 – Jacco 2010-02-17 13:14:08