Splitting Long php generated HTML table?

I use a query MySqlto select data from my database, and then print it in a form HTML Table. It works great, great, but sometimes a table consists of hundreds of rows and the web page looks incredibly uncomfortable. Is there a way to divide the table side by side into 2 or 3 halves.

Existing Exit

enter image description here

Desired Conclusion

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 this? Help will be appreciated.

+4
source share
3 answers

You can use PHP to determine in your loop if the loop index is divisible by a specific 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>";

. , , $maxRows, , . , .

, $maxRows $numRows. , , , ... $numRows = count($results); $maxRows = round($numRows / 2);

+2

:

<?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++;
}
?>

, CSS .

+1

array_chunk() . , , . :

SELECT * FROM `clients` LIMIT 5, 10 

10 , 5. , , .

+1

Source: https://habr.com/ru/post/1653191/


All Articles