在部件中并排设置php生成的html表
我编写这段代码是为了从mysql中提取数据,并将结果显示在页面上。代码生成的结果将显示在页面下方。我想把它们并排设置,可能是30行,然后是接下来的30行。在表的3列之后,如果还有更多的数据,如果可能的话,它们会沿着页面向下继续。我在这方面也发现了类似的问题,但由于我是新手,因此无法将提供的解决方案应用到我的代码中。任何帮助都将不胜感激。谢谢 我需要一个简单的示例来说明我需要的解决方案:在部件中并排设置php生成的html表,php,mysql,html-table,Php,Mysql,Html Table,我编写这段代码是为了从mysql中提取数据,并将结果显示在页面上。代码生成的结果将显示在页面下方。我想把它们并排设置,可能是30行,然后是接下来的30行。在表的3列之后,如果还有更多的数据,如果可能的话,它们会沿着页面向下继续。我在这方面也发现了类似的问题,但由于我是新手,因此无法将提供的解决方案应用到我的代码中。任何帮助都将不胜感激。谢谢 我需要一个简单的示例来说明我需要的解决方案: 如果要获得所描述的输出,可以一次将所有行提取到关联数组中,然后根据要显示的列数按组逐步遍历列 注意:我在SQL
如果要获得所描述的输出,可以一次将所有行提取到关联数组中,然后根据要显示的列数按组逐步遍历列 注意:我在SQL语句中添加了ORDERBY子句
<?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> </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();
?>
如果要获得所描述的输出,可以一次将所有行提取到关联数组中,然后根据要显示的列数按组逐步遍历列 注意:我在SQL语句中添加了ORDERBY子句
<?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> </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();
?>
听起来您希望为每30个值并排显示一个单独的表,但在您的图像中,它们似乎也会在三组30个值之后进入一个单独的行。该部分可使用CSS处理,即宽度:33%;,因此,我将提供一个处理PHP部分的解决方案。下面使用一个计数器将30个组中的每个组分隔成一个新表
<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) {
// HERE IS WHERE I ADD A COUNTER
$counter = 1;
// 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>";
// HERE I INCREMENT THE COUNTER, AND REPEAT THE END OF THE CURRENT TABLE/BEGINNING OF THE NEXT IF $counter%30 == 0
$counter++;
if($counter % 30 == 0) { ?>
</table>
<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 }
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
听起来您希望为每30个值并排显示一个单独的表,但在您的图像中,它们似乎也会在三组30个值之后进入一个单独的行。该部分可使用CSS处理,即宽度:33%;,因此,我将提供一个处理PHP部分的解决方案。下面使用一个计数器将30个组中的每个组分隔成一个新表
<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) {
// HERE IS WHERE I ADD A COUNTER
$counter = 1;
// 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>";
// HERE I INCREMENT THE COUNTER, AND REPEAT THE END OF THE CURRENT TABLE/BEGINNING OF THE NEXT IF $counter%30 == 0
$counter++;
if($counter % 30 == 0) { ?>
</table>
<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 }
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
不清楚你所说的并排是什么意思。你只有一张桌子。您想要两张表,每个表都有周和Ball1到Ball6的列吗?或者您正在尝试分页,以便在一个屏幕上显示30行,然后在接下来的30行中单击“下一步”按钮?是的,生成了一个表,该表将在页面下方显示。我寻求一种解决方案,将表格并排设置为3块,而不是使用分页。在前30行之后,它继续在前30行右侧的另30行,然后是第三个30行。事情是这样的。我还插入了一个图像以立即显示。图像太小,无法读取。请在SQL语句中添加order by子句,以便周数的顺序正确。不清楚“并排”是什么意思。你只有一张桌子。您想要两张表,每个表都有周和Ball1到Ball6的列吗?或者您正在尝试分页,以便在一个屏幕上显示30行,然后在接下来的30行中单击“下一步”按钮?是的,生成了一个表,该表将在页面下方显示。我寻求一种解决方案,将表格并排设置为3块,而不是使用分页。在前30行之后,它继续在前30行右侧的另30行,然后是第三个30行。事情是这样的。我还插入了一个图像以立即显示。图像太小,无法读取。请在SQL语句中添加order by子句,以便周数的顺序正确。感谢您的时间和解决方案,Sloan。不幸的是,我犯了一些错误,我想我现在无法自己解决。注意:使用未定义的常量numRowsPerGroup-第58行假定为'numRowsPerGroup',即$endColNo=$rownum+numRowsPerGroup;但我稍后会再次检查你的代码,我需要一点时间。那是一个缺少的$符号。我已经编辑了上面的代码。我已经添加了一条我现在看不到的评论,它不会像你更新的问题所指定的那样输出。我在看到你的更新之前添加了。谢谢你的时间和解决方案,斯隆。不幸的是,我犯了一些错误,我想我现在无法自己解决。注意:使用未定义的常量numRowsPerGroup-第58行假定为'numRowsPerGroup',即$endColNo=$rownum+numRowsPerGroup;但我稍后会再次检查你的代码,我需要一点时间。那是一个缺少的$符号。我已经编辑了上面的代码。我已经添加了一条我现在看不到的评论,它不会像你更新的问题所指定的那样输出。在看到您的更新之前我添加了。谢谢,mtr.web。我试过你的解决方案,它解决了问题。我可以用CSS来调整。谢谢,mtr.web。我试过你的解决方案,它解决了问题。我可以用CSS来调整它。