我将如何使用 PHP A-Z 显示 SQL 中的 table,其中在 10 行之后脚本在其旁边开始一个新的 table

How would I go about displaying a table from SQL with PHP A-Z where after 10 rows the script starts a new table next to it

我正在尝试用 PHP 显示 SQL table 数据,现在我制作了一个 HTML table 无限循环直到页面的末尾。现在我想尝试垂直显示 10~ 个名字,然后在它旁边开始一个新的 HTML table,继续循环直到显示整个 SQL table。我现在的脚本如下

$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");

while($row = mysqli_fetch_array($result)){  
    echo "<table>
    <tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>
    </table>" ;    
}

如果我理解正确的话,你希望每 10 行有一个 table,如果是这样的话你可以按如下方式进行:

$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
$i = 1;
while ($row = mysqli_fetch_array($result)) {
    if ($i == 1) {
        print "<table>";
    } elseif ($i == 10) {
        print " </table><table>";
        $i = 1;
    }
    echo "
    <tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>
    ";
    $i++;
}

您可以简单地将所有行提取到一个 table 中,然后将其分成 10 个块。

$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
$data = $result->fetch_all(MYSQLI_ASSOC);

foreach (array_chunk($data, 10) as $table) {
    echo "<table>";
    foreach ($table as $row) {
        echo "<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>";
    }
    echo "</table>";
}