将多个查询数据合并到单个HTML表中(PHP,MySQL)

这是我当前的代码.我正在尝试将来自三个单独查询的数据显示到具有多列的单个表中.我的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)

基础教程推荐