简体   繁体   English

分裂长PHP生成的HTML表?

[英]Splitting Long php generated HTML table?

I use a MySql query to select data from my DB and then print it in the form of a HTML Table . 我使用MySql查询从数据库中选择数据,然后以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 当前输出

在此处输入图片说明

Desired output 所需的输出

在此处输入图片说明

PHP 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. 帮助将不胜感激。

You can use PHP to determine in your loop if the loop index is divisible by a certain number using something like this: 您可以使用PHP来确定循环索引是否可以被某个数字整除,例如:

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. 基本上,在循环中,您正在测试每个索引,以查看是否可以将其用$ maxRows整除,如果是,则将关闭表并打开一个新索引。 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. 如果您想扩展此概念,可以将$ maxRows设置为$ 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); 例如,如果您想将项目尽可能地分成两半以便只显示两个表,则可以执行以下操作: $numRows = count($results); $maxRows = round($numRows / 2); $numRows = count($results); $maxRows = round($numRows / 2);

Inspired by Robert Wade's answer: 受到罗伯特·韦德(Robert Wade)的回答的启发:

<?php
....
echo "<h3>Classes attended :</h3>";

 $i=0;
 $maxRows=10;
 foreach ($results as $dates) {
    $a=$i/$maxRows == 0 ? "<table class='dates' border='1'>":"";
    $b=$i/$maxRows == 0 ? "</table>":"";

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

    $i++;
}
?>

At last, add some css style to the tables. 最后,在表中添加一些CSS样式。

You can also use array_chunk() for splitting your results. 您也可以使用array_chunk()拆分结果。 Or instead of displaing a lot of tables next to each other you can make pagination and get only some range in your query. 或者,您可以进行分页并仅在查询中获得一些范围,而不必彼此相邻放置许多表。 For example: 例如:

SELECT * FROM `clients` LIMIT 5, 10 

Selects 10 rows beggining from row 5. Now, when you change your page, just change limit values. 从第5行开始选择10行。现在,当您更改页面时,只需更改限制值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM