Split table rows into columns

hpcdzsge  于 2022-10-22  发布在  PHP
关注(0)|答案(3)|浏览(208)

I have a certain part of code that pulls numbers from a Mysql database and displays it into a HTML table using PHP. However, I have lots of data from this one row, over 100 lines easily, and I would like to split into columns by lengths of 25 or so.
This is the code so far:

while($row = mysql_fetch_array($results))
{
    echo "<tr>";
    echo "<td>" . $Destination = $row["Destination"] . "</td>";
    echo "</tr>";
}

Should I continue using a table or is there a better way to keep a nice format and split? This is going to be used for a web view, so I'd like to stick with CSS or HTML elements.

bfhwhh0e

bfhwhh0e1#

$i = 1;
echo "<tr>";
while($row = mysql_fetch_array($results))
{
    echo "<td>".$row['Destination']."</td>";

    if ($i % 25 == 0) echo "</tr><tr>";

    $i++;
}
echo "</tr>";
sr4lhrrt

sr4lhrrt2#

Try this:

<?php
$i = 1;
$columns = 25;
while($row = mysql_fetch_array($results))
{
    if($i == 1){ echo '<tr>'; }
    echo "<td>" . $Destination = $row["Destination"] . "</td>";
    $i++;
    if($i == ($columns + 1)) { echo '</tr>'; $i = 1; }
}
?>
brgchamk

brgchamk3#

You can use this:

<table>
          <?php
           $mysqli = new mysqli('localhost', 'root', '', 'crud') or die(mysqli_error($mysqli)); 
           $result = $mysqli->query("SELECT * FROM data") or die(mysqli->error);

          $i = 1;
          $columns = 3;
          echo "<tr>";
          while($row = $result->fetch_assoc())
          {
            if($i == 1){ echo '<tr>'; }
            echo "<td>" . $Destination = $row["name"] . "</td>";
            $i++;
            if($i == ($columns + 1)) { echo '</tr>'; $i = 1; }
          }
          echo "</tr>";
          ?></table>

相关问题