Skip to content Skip to sidebar Skip to footer

Multiple Query Data Into Single Html Table (php, Mysql) Array Not Printing In Correct Position?

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(

Solution 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 ($dataas$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.

Post a Comment for "Multiple Query Data Into Single Html Table (php, Mysql) Array Not Printing In Correct Position?"