Help, how do I populate table cells with different mysql results

Discussion in 'PHP' started by thafroggy, Aug 8, 2008.

  1. #1
    Hello again,

    I have a table that has 5 Columns and each will have varying number of rows.

    I would like to use php to do multiple queries to the mysql db and create multiple results, depending on what the result is will determine what column to populate.

    Here is what I have,


    
    $link = "SELECT name FROM projects WHERE status = 'Artwork'";
    $resa = mysql_query($link) or die(mysql_error());
    $total = mysql_num_rows($resa);
    
    $link = "SELECT name FROM projects WHERE status = 'Print'";
    $resp = mysql_query($link) or die(mysql_error());
    $total = mysql_num_rows($resp);
    
    
    Code (markup):
    
    echo "<table border='1'>";
    echo "<tr>";
    echo "<td>Artwork</td>";
    echo "<td>Print</td>";
    echo "<td>Waiting</td>";
    echo "<td>On Table</td>";
    echo "<td>Other</td>";
    echo "</tr>\n";
    
    while($row1 = mysql_fetch_row($resa))
    {
       echo "<tr>";
       foreach($row1 as $art)
            echo "<td>$art</td>";
    		echo "</tr>";
    }
    
    echo "</table>";
    
    Code (markup):
    This works for one column, but how do i populate the other 4 columns with other results from other queries? I have tried adding another
    while($row1 = mysql_fetch_row($resa))
    {
       echo "<tr>";
       foreach($row1 as $art)
            echo "<td>$art</td>";
    		echo "</tr>";
    }
    Code (markup):
    this does not work and when i have changed the code to look like this
    echo "<table border='1'>";
    echo "<tr>";
    echo "<td>Artwork</td>";
    echo "<td>Print</td>";
    echo "<td>Waiting</td>";
    echo "<td>On Table</td>";
    echo "<td>Other</td>";
    echo "</tr>\n";
    
    while($row1 = mysql_fetch_row($resa))
    {
       echo "<tr>";
       foreach($row1 as $art)
            echo "<td>$art</td>";
    		
    }
    
    while($row2 = mysql_fetch_row($resp))
    {
     
       foreach($row2 as $print)
            echo "<td>$art</td>";
    		echo "</tr>";
    }
    
    
    echo "</table>";
    Code (markup):
    It does start to populate the other column, Print, but everything starts getting scrambled.

    Help please, I really am lost here.
     
    thafroggy, Aug 8, 2008 IP
  2. payu

    payu Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this ...

    
    echo "<table border='1'>";
    echo "<tr>";
    echo "<td>Artwork</td>";
    echo "<td>Print</td>";
    echo "<td>Waiting</td>";
    echo "<td>On Table</td>";
    echo "<td>Other</td>";
    echo "</tr>\n";
    
    while (($row1 = mysql_fetch_row($resa)) && ($row2 = mysql_fetch_row($resp)))
    {
       echo "<tr>";
       echo "<td>{$row1['art']}</td>";
       echo "<td>{$row2['print']}</td>";
       echo "<td>{$row2['waiting']}</td>";
       echo "<td>{$row2['ontable']}</td>";
       echo "<td>{$row2['other']}</td>";
     	 echo "</tr>";
    }
    
    echo "</table>";
    
    
    PHP:
     
    payu, Aug 9, 2008 IP
  3. thafroggy

    thafroggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    well this seems to almost work

    echo "<table border='1'>";
    echo "<tr>";
    echo "<td>Artwork</td>";
    echo "<td>Print</td>";
    echo "<td>Waiting</td>";
    echo "<td>On Table</td>";
    echo "<td>Other</td>";
    echo "</tr>\n";
    
    while (($row1 = mysql_fetch_row($resa)) && ($row2 = mysql_fetch_row($resp)))
    {
       echo "<tr>";
       echo "<td>{$row1[0]}</td>";
       echo "<td>{$row2[0]}</td>";
       echo "</tr>";
    }
    
    echo "</table>";
    Code (markup):
    The problem now is that it only brings back the first two values from each resultset, there will be hundreds. What do you think could be causing this?
     
    thafroggy, Aug 9, 2008 IP
  4. payu

    payu Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ahhh .. sorry, i was hurry ..

    here is the code
    
    echo "<table border='1'>";
    echo "<tr>";
    echo "<td>Artwork</td>";
    echo "<td>Print</td>";
    echo "<td>Waiting</td>";
    echo "<td>On Table</td>";
    echo "<td>Other</td>";
    echo "</tr>\n";
    
    while (($row1 = mysql_fetch_assoc($resa)) && ($row2 = mysql_fetch_assoc($resp)))
    {
       echo "<tr>";
       echo "<td>{$row1['art']}</td>";
       echo "<td>{$row2['print']}</td>";
       echo "<td>{$row2['waiting']}</td>";
       echo "<td>{$row2['ontable']}</td>";
       echo "<td>{$row2['other']}</td>";
         echo "</tr>";
    }
    
    echo "</table>";
    
    PHP:
     
    payu, Aug 10, 2008 IP
  5. thafroggy

    thafroggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry I did not reply. I was a bit busy on some other things. I just wanted to say thanks.

    I will post my solution later when I have some more time. Once again, just saying thanks.
     
    thafroggy, Sep 6, 2008 IP