enrico-dev
enrico-dev

Reputation: 155

PHP compare query array to var and check it into a table

I have 3 table

+----+--------+----+  +----+--------+----+  +----+--------+
+ id +  name  +perc+  + id +  name  +perc+  + id +  name  +
+----+--------+----+  +----+--------+----+  +----+--------+
+ 12 + banana +100 +  + 2  + mario  +100 +  + 1  + apple  +
+ 5  + apple  + 50 +  + 5  + luigi  +100 +  + 2  + banana +
+ 7  + luigi  + 30 +  + 99 + apple  + 20 +  + 3  + input  +
+----+--------+----+  + 14 + input  + 10 +  + 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 first column there are all table3.name and in the 2nd,3rd columns i need to CHECK if the variables are at 100 like this: [V=check but not 100,X=not check,G=check with 100]

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

My code:

    $result = $data->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");
        while($line = mysql_fetch_array($result))
        {       
            echo '<tr><td>'.$line['name'].'</td>';
            if(!empty($line['t2_id'])) {
                echo 'tick'; //for each column
            } else {       //for each column
                echo 'cross';//for each column 
            }
        }   
        echo "</table>";

my query for the 'perc':

$perc_1 = $data->query("SELECT `perc`FROM `tab1`");
$globe1 = mysql_fetch_array($perc_1);
$perc_2 = $data->query("SELECT `perc`FROM `tab2`");
$globe2 = mysql_fetch_array($perc_2);

i have just try this but not work:

$compare=100;
while($line = mysql_fetch_array($result)){       
    echo '<tr><td>'.$line['name'].'</td>';
    if(!empty($line['t2_id'])) {
        if ($globe1 == $compare){
            echo 'gold'; 
        } else {     
          echo 'trick';
        }
     } else {
         echo 'cross';
     }

edit:correct some problems.but that don't resolve my problem.I don't have a PHP issue,but i don't know how to check if my var is 100

Upvotes: 2

Views: 105

Answers (2)

Alma Do
Alma Do

Reputation: 37365

You can get your data directly from SQL, like:

SELECT
  table3.name,
  CASE
    WHEN table1.perc = 100 THEN 'G'
    WHEN table1.perc!= 100 THEN 'V'
    WHEN table1.perc IS NULL THEN 'X'
  END AS tab1,
  CASE
    WHEN table2.perc = 100 THEN 'G'
    WHEN table2.perc!= 100 THEN 'V'
    WHEN table2.perc IS NULL THEN 'X'
  END AS tab2
FROM
  table3
    LEFT JOIN table1 
      ON table3.name=table1.name 
    LEFT JOIN table2 
      ON table3.name=table2.name

-so you'll have name, tab1, tab2 columns in result row set as you've described and all you'll need is to output it to your HTML.

Upvotes: 1

Scalpweb
Scalpweb

Reputation: 1971

Did you try to enable php error display to see if there is any PHP error ?

http://php.net/manual/fr/function.error-reporting.php

I can see that your MySQL query is not escaped as a string, this is a problem. Instead of this:

$result = $data->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);

You should have this:

$result = $data->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");

Also, on the last line, you have this:

echo 'cross'

Should be this:

echo 'cross';

Anyway, please activate error reporting and tell us if you have any error shown.

Upvotes: 2

Related Questions