dqdes60666 2016-08-31 16:38
浏览 130
已采纳

拆分长PHP生成的HTML表格?

I use a MySql query to select data from my DB and then print it in the form of a HTML Table. It works perfectly, fine but sometimes the table consists of hundreds of rows and the webpage looks incredibly akward. Is there a way to split the table side by side into 2 or 3 halves.

Present Output

enter image description here

Desired output

enter image description here

PHP

<?php
....
echo "<h3>Classes attended :</h3>";
echo "<table class='dates' border='1'>";

 foreach ($results as $dates) {

    echo "<tr><td width='50%'>";
    echo $dates->db_date;
    echo "</td>";
    echo "<td width='50%'>";
    echo $dates->day_name;
    echo "</td>";
    echo "</tr>";

}
echo "</table>";
?>

What would be the best way to achieve it? Help would be appreciated.

  • 写回答

3条回答 默认 最新

  • duanhong8839 2016-08-31 17:00
    关注

    You can use PHP to determine in your loop if the loop index is divisible by a certain number using something like this:

    echo "<h3>Classes attended :</h3>";
    echo "<table class='dates' border='1'>";
    $rowCount = 1;
    $numRows = count($results);
    $maxRows = 12;
    
    foreach ($results as $dates) {
    
        echo "<tr><td width='50%'>";
        echo $dates->db_date;
        echo "</td>";
        echo "<td width='50%'>";
        echo $dates->day_name;
        echo "</td>";
        echo "</tr>";
        if($rowCount % $maxRows == 0 && $rowCount != $numRows)  {
           echo "</table><table class='dates' border='1'>";
        }
        $rowCount ++;
    }
    
    echo "</table>";
    

    That's the basics of doing this. Basically in your loop you're testing each index to see if it's divisible by $maxRows, and if so then you're going to close your table and open a new one. You'll have to add the styling to place the tables side by side.

    If you wanted to expand upon this concept you can set $maxRows to be an evaluation of $numRows. For instance if you wanted to split the items as close as possible to half in order to show just two tables, you could do... $numRows = count($results); $maxRows = round($numRows / 2);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度