gpt4 book ai didi

php - 并排设置 php 生成的 html 表

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:06 24 4
gpt4 key购买 nike

我编写这段代码是为了从 mysql 中提取数据并将结果显示在页面上。代码生成的结果在页面下方。我希望它们并排设置,可能是 30 行,然后是接下来的 30 行。 (在表格的3列之后,如果还有更多的数据,如果可能的话,他们会在页面下面继续这样下去)。我找到了一个类似的 post关于这个,但由于我是新手,所以我无法将提供的解决方案应用到我的代码中。任何帮助将不胜感激。谢谢!

一个简单的插图来展示我需要的解决方案:

enter image description here

<table class="result-table">
<tr>
<th>Week</th>
<th>Ball1</th>
<th>Ball2</th>
<th>Ball3</th>
<th>Ball4</th>
<th>Ball5</th>
<th>Ball6</th>
</tr>


<?php
$conn = mysqli_connect("localhost", "root", "", "db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["Week"]."</td><td>" . $row["Ball1"] . "</td><td>"
. $row["Ball2"]. "</td><td>". $row["Ball3"]. "</td><td>". $row["Ball4"]. "</td><td>".$row["Ball5"]. "</td><td>". $row["Ball6"]. "</td></tr>";
}
echo "</table>";

} else { echo "0 results"; }
$conn->close();
?>


</table>

最佳答案

请务必获得您描述的输出,您可以一次将所有行提取到一个关联数组中,然后根据要显示的列数按组遍历列。

注意:我在您的 SQL 语句中添加了一个 order by 子句。

<?php
$numRowsPerGroup = 30;
$rowTitles = array(
array('title' => "Ball1",'colname' => "Ball1"),
array('title' => "Ball2",'colname' => "Ball2"),
array('title' => "Ball3",'colname' => "Ball3"),
array('title' => "Ball4",'colname' => "Ball4"),
array('title' => "Ball5",'colname' => "Ball5"),
array('title' => "Ball6",'colname' => "Ball6")
);
$conn = mysqli_connect("localhost", "root", "", "db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample ORDER BY week";
$result = $conn->query($sql);
$numrows = $result->num_rows;
$allrows = $result->fetch_all(MYSQLI_ASSOC);

if($allrows) {
$rownum = 0;
while($rownum < $numrows) {
// Output heading row for this group
echo "<table>\n";
echo "<thead>\n";
echo "<tr>\n";
echo " <th>&nbsp;</th>\n";
// Calculate the starting and ending column numbers. These correspond to the rows in the results
$startColNo = $rownum;
$endColNo = $rownum + $numRowsPerGroup;
if($endColNo > $numrows) {
$endColNo = $numrows;
}
// Output the week column headers
for($colNo = $startColNo;$colNo < $endColNo;$colNo++) {
echo " <th>".$allrows[$colNo]['week']."</th>\n";
}
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
// Output each item type row of the columns for this group.
foreach($rowTitles as $idx => $rowInfo) {
echo "<tr>\n";
// Step through the columns for this group for this item within the range.
echo " <td class='rowtitle'>".$rowInfo['title']."</td>\n";
for($colNo = $startColNo;$colNo < $endColNo;$colNo++) {
echo " <td>".$allrows[$colNo][$rowInfo['colname']]."</td>\n";
}
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n";
$rownum = $endColNo + 1;
}
} else {
echo "<p>0 results</p>\n";
}
$conn->close();
?>

关于php - 并排设置 php 生成的 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49738332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com