Yuval
Yuval

Reputation: 25

echo 2 arrays to 1 table

I am holding 2 arrays of SQL results: $result and $result1 (for the example it is the same queries).

I need to add the code that: if flag = 1 I want that rows in $result1 will be echo under the rows of the table that I did echo to the $result.

In the table will be the rows of $result and if flag = 1 in the same table will be also the rows of $result1.

$query = "select Username,First_Name from users";
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 

$query1 = "select Username,First_Name from users";
$result1 = mysql_query($query1) or die('Query failed: ' . mysql_error());
$flag = 0;

echo '<table align="center" border="1" cellpadding="5" cellspacing="5" style="border-collapse: collapse; background-color: #FF66AE66"><tr align="center">
<td><b>Username</b></td><td><b>First Name</b></td><td></tr>';
    while($first = mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>".$first["Username"]."</td>";
        echo "<td>".$first["First_Name"]."</td>";
        echo "</tr>";       
    } // end While

Upvotes: 0

Views: 74

Answers (2)

Felippe Duarte
Felippe Duarte

Reputation: 15131

You can do two loops, but I think you should find a solution using SQL, that gives you a proper result set. Anyway:

while($first = mysql_fetch_array($result)){
    while($second = mysql_fetch_array($second)){
        echo "<tr>";
        echo "<td>".$first["Username"]."</td>";
        echo "<td>".$first["First_Name"]."</td>";
        echo "</tr>";       
        if($first['flag'] == 1) {
            echo "<tr>";
            echo "<td>".$second["Username"]."</td>";
            echo "<td>".$second["First_Name"]."</td>";
            echo "</tr>";
        }
    }
} /

Upvotes: 0

Dhara Parmar
Dhara Parmar

Reputation: 8101

Try this:

echo '<table align="center" border="1" cellpadding="5" cellspacing="5" style="border-collapse: collapse; background-color: #FF66AE66"><tr align="center">
<td><b>Username</b></td><td><b>First Name</b></td><td></tr>';
    while($first = mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>".$first["Username"]."</td>";
        echo "<td>".$first["First_Name"]."</td>";
        echo "</tr>";       
    } // end While
if($flag == 1){
    while($last= mysql_fetch_array($result1)){
                echo "<tr>";
                echo "<td>".$last["Username"]."</td>";
                echo "<td>".$last["First_Name"]."</td>";
                echo "</tr>";       
            } // end While
    }

Upvotes: 1

Related Questions