这是我当前的代码.我正在尝试将来自三个单独查询的数据显示到具有多列的单个表中.我的while陈述在这里错误吗?它先打印1个表数据,然后再打印一个,而不是在同一行中紧挨着它.echo table border=1trTH COLSP...
这是我当前的代码.我正在尝试将来自三个单独查询的数据显示到具有多列的单个表中.我的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条不同的行,您应该将它们分别进行处理并将它们存储在本地变量中,如果该变量是一个大型数组,则只需在单词后取消设置即可.
我的代码未经测试.
沃梦达教程
本文标题为:将多个查询数据合并到单个HTML表中(PHP,MySQL)
基础教程推荐
猜你喜欢
- 手把手教你实现vue下拉菜单组件 2023-10-08
- ajax传送参数含有特殊字符的快速解决方法 2022-12-28
- Vuex 2023-10-08
- Vuex状态管理 2023-10-08
- ajax从JSP传递对象数组到后台的方法 2023-02-15
- ajax验证用户名和密码的实例代码 2022-12-28
- Linux 之 HTML 页面转图片软件:wkhtmltox 2023-10-25
- Centos中解决html页面访问中文乱码问题 2023-10-25
- php – Laravel – 保存HTML Dom-Parser对数据库的响应 2023-10-26
- javascript请求servlet实现ajax示例(分享) 2023-02-14