Multiple query data into single html table (PHP, Mysql) array not printing in correct position?

六眼飞鱼酱① 提交于 2019-12-25 05:09:32

问题


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>" ;

which gives a reult like below

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.


回答1:


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

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



来源:https://stackoverflow.com/questions/6394240/multiple-query-data-into-single-html-table-php-mysql-array-not-printing-in-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!