我有一个product_category的表,它有20多个类别,我想以如下图片的表格格式显示它们
我喜欢将类别放在5td中,然后如何在atr中固定到table
下面是我尝试过的代码,但它没有给出所需的输出
下面是我需要的解决方案
低于代码

<table>
<?php

$fquery5 = mysql_query("select Cat_ID, Cat_Name from product_category where Active_Flag=1");

    $count1 = mysql_num_rows($fquery5);

    $hor_length = 5;
    $ver_length = $count1 / $hor_length;

    $data1 = mysql_fetch_row($fquery5);

    for ($i = 0; $i <= $ver_length; $i++) {
        echo "<tr>";
            for ($j = $i + $hor_length; $j <= $count1; $j++) {
                    echo "<td>";
                        echo "Id : " . $data1[0] . " = " . $data1[1];
                    echo "</td>";
            }
        echo "</tr>";
    }

?>
</table>

添加了由@DaveRandom应答的结果输出图像

最佳答案

尝试此操作(已修复):

<?php

  // Number of columns
  $hor_length = 5;

  // Do the query, get the number of rows in the result
  $fquery5 = mysql_query("
    SELECT Cat_ID, Cat_Name
    FROM product_category
    WHERE Active_Flag = 1
  ");
  $numrows = mysql_num_rows($fquery5);

  // Start of table
  echo "<table>\n<tr>\n";

  // Loop the results with a counter
  for ($i = 1; $row = mysql_fetch_row($fquery5); $i++) {
    // Every iteration echos a cell
    echo "<td>Id : " . $row[0] . " = " . $row[1] . "</td>\n";
    // If we're at the end of a row, echo a row break, unless it is the last result
    if (!($i % $hor_length) && $i < $numrows) {
      echo "</tr>\n<tr>\n";
    }
  }

  // Right-pad the end row with empty cells
  for ($i--; $i % $hor_length; $i++) {
    echo "<td></td>\n";
  }

  // Echo the end of the table
  echo "</tr>\n</table>";

请参见aworking example(由于无法从codepad查询数据库,因此手动创建了数据数组)

关于php - 从MySQL以表格格式显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10175537/

10-09 15:25