在MySQL表(PHP)的行中显示存储产品
我被卡住了——我正在从一个在线商店的MySQL表中提取商店项目。查询将返回这些项。通常,我会使用以下内容,将每个产品列在单独的一行中:在MySQL表(PHP)的行中显示存储产品,php,mysql,Php,Mysql,我被卡住了——我正在从一个在线商店的MySQL表中提取商店项目。查询将返回这些项。通常,我会使用以下内容,将每个产品列在单独的一行中: <table> <?php while($rows = mysql_fetch_array($productListResults)){ ?> <tr align="left"> <td><b><?php echo $rows['manufacturer']; ?> </tr>
<table>
<?php
while($rows = mysql_fetch_array($productListResults)){
?>
<tr align="left">
<td><b><?php echo $rows['manufacturer']; ?>
</tr>
<?php } ?>
</table>
当然。最简单的方法可能是使用一个变量来跟踪这一行中已经打印了多少 伪代码看起来像:
let $i = 0
for each result from the database:
if $i is equal to 0:
echo the stuff that's needed to start a row (<tr>, etc.)
print out the stuff needed for a cell in the table, based on $row
increment $i by 1
if $i is equal to 3:
echo the stuff that's needed to end a row (</tr>, etc.)
repeat the process if you still have more rows to print
让$i=0
对于数据库中的每个结果:
如果$i等于0:
重复开始一行所需的内容(,等)
根据$row打印出表中单元格所需的内容
增加$i 1
如果$i等于3:
重复结束一行所需的内容(,等)
如果仍有更多行要打印,请重复此过程
如果它不起作用,请告诉我。有几种方法可以解决这个问题。PHP方法如下所示:
<table><tr>
<?php $i = 0; while ($row = mysql_fetch_array($productListResults)) { ?>
<td><?php echo $row['manufacturer']; ?></td>
<?php if (0 == ++$i % 3) { ?>
</tr><tr>
<?php } ?>
<?php } ?>
</tr></table>
这种方法从一行开始,然后开始循环和添加列。当列计数是所需计数的整数除数时,行停止。(“$i%3”中的“3”。)
这种方法并不理想。请注意,在特定的边缘情况下,它可能会导致空。修正这是一个读者练习
也许你会考虑在CSS中做这件事。这样,您可以直接输出一个列表,然后使用演示文稿调整流程以满足您的需要。下面是一个简单的介绍:
<table>
<tr>
<?php
$split=0;
while($rows = mysql_fetch_array($productListResults)){
echo '<td><strong>'.$rows['manufacturer'].'</strong></td>';
$split++;
if ($split%3==0){
echo '</tr><tr>';
}
} ';
</tr>
</table>
<table>
<?php
$rows = mysql_fetch_array($productListResults);
$columns = 3
for ($i = 0; $i < count($rows); $i++) {
?>
<?php if ($i % $columns == 0): ?>
<tr align="left">
<?php endif; ?>
<td><b><?php echo $rows[$i]['manufacturer']; ?></b></td>
<?php if ($i % $columns == ($columns - 1) || $i == count($rows) - 1): ?>
</tr>
<?php endif; ?>
<?php } ?>
</table>
<table><tr>
<?php $i = 0; while ($row = mysql_fetch_array($productListResults)) { ?>
<td><?php echo $row['manufacturer']; ?></td>
<?php if (0 == ++$i % 3) { ?>
</tr><tr>
<?php } ?>
<?php } ?>
</tr></table>