enrico-dev
enrico-dev

Reputation: 155

PHP mysql Check variables from multiple tables

I have 3 tables in my db

+----+--------+  +----+--------+  +----+--------+
+ id +  name  +  + id +  name  +  + id +  name  +
+----+--------+  +----+--------+  +----+--------+
+ 12 + banana +  + 2  + mario  +  + 1  + apple  +
+ 5  + apple  +  + 5  + luigi  +  + 2  + banana +
+ 7  + luigi  +  + 99 + apple  +  + 3  + input  +
+----+--------+  + 14 + input  +  + 4  + luigi  +
                 +----+--------+  + 5  + mario  +
                                  +----+--------+

The 3rd table was created from the 1st and the 2nd. In my HTML file there is a table that get all 'name' from table 3.
In the firs column there are all table3.name and in the 2nd,3rd columns i need to CHECK like this:

+--------+------+------+
+ name   + tab1 + tab2 +
+--------+------+------+
+ apple  +   V  +   V  +
+ banana +   V  +   X  +
+ input  +   X  +   V  +
+ luigi  +   V  +   V  +
+ mario  +   X  +   V  +
+--------+------+------+

I create the whole structure of the table in html file, but I can not do the "check".

$result = $data->query("SELECT * FROM `table3` ORDER BY `table3`.`name` ASC  ");
while($line = mysql_fetch_array($result))
{       
    echo '<tr><td>'.$line['name'].'</td>';

    echo '<td></td>';
    echo '<td></td>';
    echo '<td></td>';
    echo '<td></td>';
    echo '<td></td>';
    echo '<td></td></tr>';
    }     
echo "</table>";

Someone can help me?

Upvotes: 0

Views: 193

Answers (1)

Raja Amer Khan
Raja Amer Khan

Reputation: 1519

Here's how it will work:

Query:

SELECT t3.name,t2.id t2_id,t1.id t1_id
FROM table3 t3
LEFT JOIN table2 t2 ON t2.name=t3.name
LEFT JOIN table1 t1 ON t1.name=t3.name

In code:

if(!empty($line['t2_id'])) {
  echo 'tick'; 
} else {
  echo 'cross'; 
}

do the same for table 1 ticks and crosses.

Upvotes: 1

Related Questions