多个查询数据转换成单个html表(PHP,Mysql)数组不能正确打印? [英] Multiple query data into single html table (PHP, Mysql) array not printing in correct position?

查看:97
本文介绍了多个查询数据转换成单个html表(PHP,Mysql)数组不能正确打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个网站获得了代码片段,我使用如下所示

I got code snippet from this site, which I used as shown below

    $data = array();

while($row = mysql_fetch_assoc($num1)) {$data['row'][] = $row;}
while($row = mysql_fetch_assoc($num2))  {$data['row2'][] = $row;}


$count = count($data['row']);
echo "<table>" ;
echo "<tr>";
echo "<td width='300' bgcolor='#99CCF5' align='Left' style='padding-left:30px'><b>Country</b></td>" ;
echo "<td width='150' bgcolor='#99CCF5' align='center'><b>Mid Estimate 1</b></td>";
echo "<td width='150' bgcolor='#99CCF5' align='center'><b>Mid Estimate 2</b></td>";
echo "</tr>";
for($i=0;$i<=$count;$i++)
{

        if(($i % 2) == 1)
        {
            echo "<tr>" ;
            echo "<td align='center'>" . $data['row'][$i]['Country']."</td>";
            echo "<td align='center'>" . $data['row'][$i]['MidEstimate']."</td>";
            echo "<td align='center'>" . $data['row2'][$i]['MidEstimate']."</td>";
            echo "</tr>" ;
        }else
        {
            echo "<tr>" ;
            echo "<td align='center'>" . $data['row'][$i]['Country'] ."</td>";
            echo "<td align='center'>" . $data['row'][$i]['MidEstimate']."</td>";
            echo "<td align='center'>" . $data['row2'][$i]['MidEstimate']."</td>";
            echo "</tr>" ;
        }

}

echo "</table>" ;

给出如下的结果

image1 http://img4.pixa.us/8ba/19338641.jpg

其中正确的结果应该是这样的

where the correct result should be like this

image2 http://img4.pixa.us/c1d/19338642.jpg

如果列中的任何值为空,则下一个辅助值将获得该位置。我怎样才能做到这一点?也就是说,如果任何值为空,那列必须为空。

that is if any value in a column is empty the next adjucent value gets that position. How can I make this correct? that is if any value is empty that column must be empty.

请提前帮助并提前谢谢。

please help and thanks in advance.

推荐答案

您必须收集每个国家/地区的数据。由于数组的键不同步,因此您在问题中的方法会混淆列表。让我们按国家/地区同步你的行:

You have to gather the data for each country. Your approach in the question messes up the listing since the keys for the array are not in sync. Let's sync your rows by 'Country':

$data = array();
while($row = mysql_fetch_assoc($num1))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate1'] = $row['MidEstimate'];
}
while($row = mysql_fetch_assoc($num2))
{
    $c = $row['Country'];
    if (!isset($data[$c]))
    {
        $data[$c] = array('Country' => $c);
    }
    $data[$c]['MidEstimate2'] = $row['MidEstimate'];
}

现在,您的数组中每个国家/地区都有一行,其数据来自每个查询。

Now you have a row in your array for every Country, with their data from each query.

$i = 0;
foreach ($data as $row)
{
    echo ($i % 2) ? "<tr class='odd'>" : "<tr class='even'>" ;
    echo "<td align='center'>" . $row['Country']."</td>";
    echo "<td align='center'>" . $row['MidEstimate1']."</td>";
    echo "<td align='center'>" . $row['MidEstimate2']."</td>";
    echo "</tr>" ;
}

注意:这只适用于'Country'字段,在两个SQL查询中都有。

Note: this only works in 'Country' field is present in both SQL query.

这篇关于多个查询数据转换成单个html表(PHP,Mysql)数组不能正确打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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