将多个查询数据合并到单个HTML表中(PHP,MySQL) [英] Multiple query data into single HTML Table (PHP, MySQL)

查看:59
本文介绍了将多个查询数据合并到单个HTML表中(PHP,MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的代码.我正在尝试将来自三个单独查询的数据显示到具有多列的单个表中.我的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个不同的行,您应该将它们分别进行处理并将它们存储在本地变量中,如果它是一个大数组,则在单词后取消设置. /p>

我的代码未经测试.

Here is my current code. I'm trying to display data from three separate queries into a single table with multiple columns. Is my while statement wrong here? It's printing 1 table data, then the one after, instead of next to it in the same row.

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>';
}

By fetching the results individually into a local array instead of trying to fetch 3 different rows at the same time you should do them individually and store them in a local variable, just unset the variable after words if its a large array.

my code is offered as untested.

这篇关于将多个查询数据合并到单个HTML表中(PHP,MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆