Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Arrays and Control Structures PHP Loops Todo App

Obayanju Damilare
Obayanju Damilare
3,383 Points

Please, $status = true; issue

why on earth does it show all 5 complete entries when I set $status = true; instead of only showing the last entry!!!! and when I set $status to false, it works.

this is my code

<?php
include 'list.php';

//the status variable represents true or false
//this allows us to make changes to what our script will display
//right at the top of the file
$status = true;
//we want to show a list of keys in a particular order, so we use this
$order = array();//we use an indexed array for this

if($status == 'all'){
    $order = array_keys($list);
}else{
    foreach ($list as $key => $item) {
        //we use a conditional statement to check the completion status
        if ($item['complete'] === $status) {
            //we add the key to our order array
            $order[] = $key;
        }
    }
}

//var_dump($order);
//var_dump($list);


echo '<table>';
echo '<tr>';
echo '<th>Title</th>';
echo '<th>Priority</th>';
echo '<th>Due Date</th>';
echo '<th>Completion</th>';
echo '</tr>';
//we use $id because this identifies the list item we are going to use
foreach ($order as $id) {
    echo '<tr>';
    echo '<td>' . $list[$id]['title'] . "</td>\n";
    echo '<td>' . $list[$id]['priority'] . "</td>\n";
    echo '<td>' . $list[$id]['due'] . "</td>\n";
    echo '<td>';
    if($list[$id]['complete']){
        echo 'Yes';
    }else{
        echo 'No';
    }
    echo "</td>\n";
    echo '</tr>';
}

echo '</table>';
 ?>

5 Answers

Waqar Mohammad
Waqar Mohammad
2,914 Points

Its actuallty a mistake I have used the following code and it works in all conditions

$order = array();
if ($status === "all") {
        $order = array_keys($list);     
 } else {
    foreach ($list as $key => $item) {
        if ($item['complete'] == $status) {
            $order [] = $key;
        }
    }
}

notice the use of the "===" instead of "=="

Waqar

Mark Warren
Mark Warren
19,252 Points

Thanks Waqar (and Obayanju!) - I was wondering the same thing!

Alexandre Babeanu
Alexandre Babeanu
10,947 Points

it's because true == 'all'

When status is set to true then status it is set as a boolean. when comparing a boolean to a string with == php will look either for an empty string (false) or for not an empty string (true).

Thats why you have to use ===

this should be the top answer - thank you for the explanation Alexandre!

You check if the $status variable is even with the string 'all' but you have said that this variable can contain boolean values like TRUE or FALSE, so you have to check if it's true or false.

Obayanju Damilare
Obayanju Damilare
3,383 Points

I guess I would try and do that but why does their(Treehouse) code work. Thanks for your reply, Marian. Also I wanted to add that when you put $status = false; it works but doesn't with true.

Michael Richards
Michael Richards
3,742 Points

Their code doesn't work, it's just never shown with "true" again.