douzhuo1853 2012-04-16 13:57
浏览 55
已采纳

从mysql以表格格式显示

I have table with product_category and its has more than 20 categories and I want to display them in table format like below image

enter image description here

I like to place categories in 5 td then how I can be fixed towards tr in a table

Below is code I have tried, but its not giving the desired output

And below is the image that might be solution I needed

enter image description here

Below code

<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>

Added resultant output image answered by @DaveRandom

enter image description here

  • 写回答

1条回答 默认 最新

  • duannaxin9975 2012-04-16 14:10
    关注

    Try this (FIXED):

    <?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>
    <tr>
    ";
    
      // 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>
    ";
        // 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>
    <tr>
    ";
        }
      }
    
      // Right-pad the end row with empty cells
      for ($i--; $i % $hor_length; $i++) {
        echo "<td></td>
    ";
      }
    
      // Echo the end of the table
      echo "</tr>
    </table>";
    

    See a working example (manually created the data array since I can't query a DB from codepad)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)