HTML表格已满时转到下一行

问题描述:

我正在使用HTML表格使用PHP显示MySQL表格中的数据。我需要它,所以一旦表格有10列,它就会移动到下一行。

I am using an HTML table to display data from a MySQL table using PHP. I need it so once the table has 10 columns, it will move on to the next row.

<?php
$result = mysqli_query($con,"SELECT * FROM table");

echo '<table width="100%" border="1px"><tr width="100%">';

while($row = mysqli_fetch_array($result))
{
?>
                    <td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";

mysqli_close($con);
?>

如何做到这一点?

未经测试但这样的事情应该有效或让你开始朝着一个好的方向发展:

Untested but something like this should work or get you started in a good direction:

<?php
$result = mysqli_query($con,"SELECT * FROM table");

echo '<table width="100%" border="1px"><tr width="100%">';

$x=0;
while($row = mysqli_fetch_array($result))
{

    if($x==0){      
        echo "<tr>\n";

    }elseif($x%10){
        echo"</tr><tr>\n";
    }
    ?>
    <td width="10%"><?php echo $row['Name']; ?></td>
    <?php
    $x++;       
}
echo "</tr></table>";

mysqli_close($con);
?>