请教各位,用php输出5行每行8列的表格,并依次输出40条mysql记录数,结果就是像下面这样的,应该如何实现呢?另外,如果记录数不足40条,比如只有36条,那就是还有4对空内容的<td></td>标签,这个也要显示出来的话,又应该怎么样实现呢?我自己试了几种方法,都不好使,希望高手解答一下,非常感谢!
<table><tr>
<td>记录1</td>
...
<td>记录8</td>
</tr><tr>
<td>记录9</td>
...
<td>记录16</td>
</tr><tr>
<td>记录17</td>
...
<td>记录24</td>
</tr><tr>
<td>记录25</td>
...
<td>记录32</td>
</tr><tr>
<td>记录33</td>
...
<td>记录40</td>
</tr></table>

解决方案 »

  1.   

    在script中循环document.write("<tr>");......
    如上,如果是用smarty模板的话就用section循环
      

  2.   

    <?php
    $output = "<table>";
    for($i=0; $i<8; $i++){
    // $result is your query result array
    $output .= "<tr>";
    $output .= "<td>".$result[$i*5 + 1]."</td>";
    $output .= "<td>".$result[$i*5 + 2]."</td>";
    $output .= "<td>".$result[$i*5 + 3]."</td>";
    $output .= "<td>".$result[$i*5 + 4]."</td>";
    $output .= "<td>".$result[$i*5 + 5]."</td>";
    $output .= "</tr>";
    }
    $output .= "</table>";
    echo $output;
    ?>
      

  3.   

    $i = 0;
    while(读取记录到$r) {
      if($i==0) echo '<tr>';
      echo '<td>'.$r.'</td>';
      $i = ($i+1)%8;
      if($i == 0) echo '</tr>';
    }
    if($i>0) echo str_repeat('<td>&nbsp;</td>', 8-$i) . '</tr>';
      

  4.   

    这种情况尽量用div 浮动来做。用table,比较麻烦,代码如下:<?php
    $sql = "xxx limit 40";
    $query = mysql_db_query($sql);
    if(mysql_num_rows($query) > 0){
    ?>
    <table>
    <?php
    $ct = 0;
    $cols = 8;

    while($rs = mysql_fetch_array($query)){
    //输出行首
    if( $ct % $cols == 0 ){ 
    echo '<tr>'; 
    }
    ?>
    <td>记录</td>
    <?php
    ++$ct;
    //输出行尾巴
    if( $ct % $cols == 0 ){ 
    echo '</tr>'; 
    }
    }

    //单元格不够,补全单元格与行尾
    if( $ct % $cols != 0 ){ 

    $lack = ($cols - $ct % $cols);
    for($i = 0; $i <= $lack; $i++){
    echo '<td>&nbsp;</td>';
    }

    echo '</tr>'; 
    }
    ?>
    </table>
    <?php
    }
    ?>