gpt4 book ai didi

php - 将多个查询数据放入单个 HTML 表(PHP、MySQL)

转载 作者:行者123 更新时间:2023-11-29 04:59:02 24 4
gpt4 key购买 nike

这是我当前的代码。我正在尝试将来自三个单独查询的数据显示到一个包含多个列的表中。我的 while 声明在这里错了吗?它正在打印 1 个表数据,然后是后面的数据,而不是在同一行中的旁边。

echo "<table border='1'>
<tr>
<TH COLSPAN=2>July 2010</TH>
<TH COLSPAN=2>August 2010</TH>
<TH COLSPAN=2>September 2010</TH>
</tr>
<tr>
<th>User</th>
<th>Posts</th>
<th>User</th>
<th>Posts</th>
<th>User</th>
<th>Posts</th>
</tr>";

while (($row = mysql_fetch_assoc($july)) || ($row2 = mysql_fetch_assoc($aug)) || ($row3 = mysql_fetch_assoc($sept))) {
echo "<tr>";
echo "<td>" . $row['cUsername'] . "</td>";
echo "<td>" . $row['postCount'] . "</td>";
echo "<td>" . $row2['cUsername'] . "</td>";
echo "<td>" . $row2['postCount'] . "</td>";
echo "<td>" . $row3['cUsername'] . "</td>";
echo "<td>" . $row3['postCount'] . "</td>";
echo "</tr>";
}

echo "</table>";

最佳答案

$data = array();

while($row = mysql_fetch_assoc($july)) {$data['row'][] = $row;}
while($row = mysql_fetch_assoc($aug)) {$data['row2'][] = $row;}
while($row = mysql_fetch_assoc($sept)) {$data['row3'][] = $row;}

$count = count($data['row']);

for($i=0;$i<=$count;$i++)
{
echo '<tr>';
if(($i % 3) == 1)
{
echo "<td>" . $data['row3'][$i]['cUsername'] . "</td>";
echo "<td>" . $data['row3'][$i]['postCount'] . "</td>";
}else if(($i % 2) == 1)
{
echo "<td>" . $data['row2'][$i]['cUsername'] . "</td>";
echo "<td>" . $data['row2'][$i]['postCount'] . "</td>";
}else /*Never try find remainder of 1 as theres always a multiple of 1*/
{
echo "<td>" . $data['row'][$i]['cUsername'] . "</td>";
echo "<td>" . $data['row'][$i]['postCount'] . "</td>";
}
echo '</tr>';
}

通过将结果单独提取到本地数组中,而不是尝试同时提取 3 个不同的行,您应该单独执行它们并将它们存储在局部变量中,如果它是一个大数组,只需在 words 之后取消设置变量。

我的代码未经测试。

关于php - 将多个查询数据放入单个 HTML 表(PHP、MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3613498/

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