0

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.

4
  • Your SQL and table schema + sample data would be helpful. You may be able to solve this at the SQL level and eliminate some code complexity. Commented Jun 18, 2011 at 6:17
  • I got only a single table in mysql. My previous question on this site[link]stackoverflow.com/questions/6341613/… and I got the above code from this link[link]stackoverflow.com/questions/3613498/…. thanx for the quick response. Commented Jun 18, 2011 at 6:21
  • I agree with Robin. It looks like you could more efficiently receive your data using a JOIN, but your code really doesn't help all that much without at the very least the queries that you run. Obviously a table layout would help a lot as well. Commented Jun 18, 2011 at 6:26
  • As I said I got a mysql database with a single table named mtable where all data's are stored. Commented Jun 18, 2011 at 6:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.