0

I am trying to display a pie chart that shows the results of my query. In this sample I have 3 time periods (Breakfast, Lunch, Dinner) and count how many records I have for each period. When displaying the chart though, I need to have each dataset with a different color, which is why I added an IF statement while fetching results.

Naturally, the statement is not working and only returns the color for "breakfast". How can I refresh the statement to check each returned row for the Period name and then echo the respective color?

Thanks

while($row = mysqli_fetch_array($result)){
    $count = $row['period_count'];  
    $period = $row['period'];

    if ($period = "Breakfast") {
            $color = "#F38630";
        } elseif ($period = "Lunch") {
            $color = "#E0E4CC";
        } else {
            $color = "#69D2E7";
    }

    echo    "{"."\n";
    echo    "value : " . $row['period_count'] . ","."\n";
    echo    "color : \"" . $color ."\","."\n";
    echo    "label : \"" . $row['period']."\""."\n";
    echo    "},"."\n";

}
3
  • 4
    Your conditionals need the == operator. if ($period == "Breakfast") { Commented Apr 14, 2016 at 2:54
  • 2
    If you don't put double == it will assign the value of Breakfast to the variable of $period. If you do double (==) it compares them. Just wanted to add to larsAnders Commented Apr 14, 2016 at 2:56
  • took you guys a whole minute to figure this out, huh? ;) thanks a lot! Commented Apr 14, 2016 at 2:57

1 Answer 1

0

as pointed out by @larsAnders, the == operator is needed to compare values.

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

2 Comments

If you're making a habit of messing these up, flip your terms. "Breakfast" = $period will cause an immediate error rather than silently do the wrong thing. Over time you'll be able to spot = mistakes from a mile away.
You guys are fast! Everything I think to say pops up before I get a chance to say it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.