echo "<table><tr>";
$i=1;
while($array=mysql_fetch_array($result))
{
if(($i%2)==0)
echo "<tr>";
for(reset($array);$fieldname=key($array);next($array))
echo "<td>".$array[$fieldname]."</td>";if(($i%2)==0)
echo "</tr>"; 
}
echo "</table>";

解决方案 »

  1.   

    (1)
    <tr><td>1</td><td>2</td></tr>
    <tr><td>3</td><td>4</td></tr>
    <tr><td>5</td><td>6</td></tr>
    -------------------------------------
    (2)
    echo "<tr>";
    $i=0;           //做输出</tr><tr>标志用
    while($arr=...){
       echo "<td>$arr[$name]<td>";
       if(++$i%2==0) {echo "</tr><tr>";$i=0;}
    }
    echo "</tr>";
      

  2.   

    <?
    function draw_table($rows, $cols, $array) {
    echo "<table border='1' width='760'>";
    for ($i=0; $i<$rows; $i++) {
    echo "<tr>";
    for ($j=0; $j<$cols; $j++) {
    echo "<td>" . $array[$i+$j] . "</td>";
    }
    echo "</tr>";
    }
    echo "</table>";
    } // end  draw_table(3, 2, array(1, 2, 3, 4, 5, 6));
    ?>这样行吗?
      

  3.   

    <?
    function draw_table($rows, $cols, $array) {
    echo "<table border='1' width='760'>";
    for ($i=0; $i<$rows; $i++) {
    echo "<tr>";
    for ($j=0; $j<$cols; $j++) {
    echo "<td>" . $array[$i * $cols +$j] . "</td>";
    }
    echo "</tr>";
    }
    echo "</table>";
    } // end  draw_table(3, 2, array(1, 2, 3, 4, 5, 6));
    ?>
      

  4.   

    这个应该好用了,昨天忘加了个变量

    $i * $cols +$j
    写成了
    $i+$j]
      

  5.   

    echo "<table width=100% border=0 cellspacing=0 cellpadding=4>\n";
      $col=0;
      $cols=2;//每行2列
      $query="select id,title from table order by displayorder";
      $result=mysql_query($query);
      while($row=mysql_fetch_array($result))
      {
        $col=$col+1;
        if ($col%$cols==1)echo "<tr>\n";
        echo "<td>";
        echo "<a href=list.php?id=$row[id]>$row[title]</a>";
        echo "</td>\n";
        if ($col%$cols==0) echo "</tr>\n";
      }
      // 下面处理当总数是单数的时候输出空表单元
      if($col%$cols!=0)
      {
        for($i=1;$i<=$cols-$col%$cols;$i++)
        {
          echo "<td>&nbsp;</td>\n";
        }
      }
      if($i>1) echo "</tr>\n";
      echo "</table>\n";以上代码可以处理N行N列