Creaven
Creaven

Reputation: 319

php if statement on mysqli fetch array

I have my PHP code : from after the sql query...

$pieData = array();

} 

while($row = mysqli_fetch_array($result))
{
$pieData[] = array('value' =>(int)$row['cnt'], 'color' => '#222222');

Now this works and is successful when I pass through json_encode(). I want to dynamically change the color.

From my database $result I access $row['Delaytype'] and want do something like this.

while($row = mysqli_fetch_array($result)){

if($row['Delaytype'] === "engineering"){

$pieData[] = array('value' =>(int)$row['cnt'], 'color' => '#222222');
}elseif($row['Delaytype'] === "something else"){

$pieData[] = array('value' =>(int)$row['cnt'], 'color' => '#888888');

 }

The code works but it accepts the first true statement and carries on and all my colors have the same value and so does my resulting pie chart. I have tried a switch statement, if and attemtped to loop through the array but my understanding of this is limited.

Any help is really appreciated.

The table is called delays:

It is like 
Delaytype   | Delayhours
engineering |     3
engineering |     2
human error |     4 

The SQL query :

"SELECT Delaytype, SUM('Delayhours') as cnt FROM delays GROUP BY
Delaytype;"

If I echo a table with 

<tr><td>$row[delaytype]</td><td>$row[cnt]</td></tr> //forgive formatting

I will get from table above:

engineering 5
human error 4  

strcasecmp also not working.

if i do if(1=1) it works so it is something to do with the comparison but it is correct the output is lowercase engineering but it is not seeing it when its tested?

I now have this working. Odd all I did was change "human error" to " human error " and "engineering" to " engineering ". I have looked at my html input values and found no leading spaces ? No idea why it works with the space. I am going to search through my code for a reason. Thanks for your help and other suggestions. strcasecmp works as well as without. I mark the awnser correct as everything you said did work... + it will show up awnsered and more likely get viewed for a similar problem. Thank for you help

Upvotes: 1

Views: 2116

Answers (2)

Jeremiah Winsley
Jeremiah Winsley

Reputation: 2557

A slightly more robust version of your loop would be:

if ($row['Delaytype'] === 'engineering') {
    $color = '#222222';
} elseif ($row['Delaytype'] === 'human error') {
    $color = '#888888';
} else {
    $color = '#ffffff';
}
$pieData[] = array(
    'value' => (int)$row['cnt'],
    'color' => $color,
);

This checks each of the values in your sample, but also has a fallback value in case it doesn't match either. It moves the $pieData assignment outside of that loop, so that you aren't duplicating the code there.

Upvotes: 1

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

Use your compare condition using == instead of ===, because the later one compare & match data type, so try this way and see here for more info. http://php.net/manual/en/language.operators.comparison.php

while($row = mysqli_fetch_array($result))
 {
   if($row['Delaytype'] == "engineering"){
     $pieData[] = array('value' =>(int)$row['cnt'], 'color' => 
      '#222222');
    }elseif($row['Delaytype'] == "human error"){ 
      $pieData[] = array('value' =>(int)$row['cnt'], 'color' => 
      '#888888');  
 }

Other Way:

while($row = mysqli_fetch_array($result))
     {
       if(strcasecmp($row['Delaytype'],"engineering")==0){
         $pieData[] = array('value' =>(int)$row['cnt'], 'color' => 
          '#222222');
        }elseif(strcasecmp($row['Delaytype'],"human error")==0){ 
          $pieData[] = array('value' =>(int)$row['cnt'], 'color' => 
          '#888888');  
     }

Note: Field names returned by this function are case-sensitive.

Upvotes: 1

Related Questions