1

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

3
  • 2
    Your code should work. Please provide a few samples of the 'Delaytype' column so that we can reproduce the issue. Commented Jan 24, 2015 at 3:09
  • 2
    If your only two delay types are engineering and human error, then your comparison in the elseif branch with "something else" doesn’t make any sense – it would lead to $pieData being filled with only the records with engineering, the ones with human error would not be added to the array at all, since neither the if nor the elseif condition match … Commented Jan 24, 2015 at 3:53
  • There is more than two conditions. Code shortened for demonstration. Thanks for your comments Commented Jan 24, 2015 at 5:34

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that the mysqli driver always returns strings, so matching the type is not a problem here. php.net/manual/en/mysqli-result.fetch-array.php
Even with the correct assignmaent operators and correct case it still fails and I get the else code my json looks like [{"value":21,"color":else"},{"value":"99","color":"else value"}] - else value not on example. Put in to test.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.