0

我被困住了 - 我正在从 MySQL 表中提取商店项目以用于在线商店。查询返回项目。通常,我会使用以下内容,将每个产品列在单独的行中:

<table>
<?php
while($rows = mysql_fetch_array($productListResults)){
?>
<tr align="left">
<td><b><?php echo $rows['manufacturer']; ?> 
</tr>
<?php } ?>
</table>

但是,现在,我想每行显示 3-4 个产品,然后在下一行继续显示其余产品。我在下面提供了一个示例。任何帮助将不胜感激!谢谢!

PRODUCT1       PRODUCT2       PRODUCT3

PRODUCT4       PRODUCT5       PRODUCT6

PRODUCT7       PRODUCT8       PRODUCT9
4

4 回答 4

0

Sure. The easiest way to do this would likely be to use a variable to keep track of how many you've already printed in this row.

The pseudocode would look something like:

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
于 2013-11-02T03:37:48.580 回答
0
    <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>
于 2013-11-02T03:39:18.630 回答
0
<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>

Please let me know if it does not work.

于 2013-11-02T03:40:16.013 回答
0

有几种方法可以解决这个问题。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 中执行此操作。这样,您可以输出一个直接的列表,然后使用演示来调整流程以满足您的需求。这是一个简单的介绍:http: //csswizardry.com/2010/02/mutiple-column-lists-using-one-ul/

于 2013-11-02T03:45:26.510 回答