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 trialAndrew Phythian
19,747 PointsTodo app - $status = 'all' part
After following through the video for the PHP Arrays and Control Structures > Todo App I've come up against a problem.
The part of the code that filters between
if ($value['complete'] == true) {
and
if ($value['complete'] == false) {
correctly displayed the Todo list between completed and incomplete tasks. I introduced the $status and after setting to true and false it continued to work fine.
if ($value['complete'] == $status) {
I then followed the instructions and set it to 'all' adding the section of code to filter in ALL keys if $status = 'all'. At this point while it correctly displayed ALL tasks, it stopped filtering for just complete tasks when $status = true. It did however filter for incomplete tasks when $status = false.
Here's what I have...
$status = false;
$action = 'week';
$order = array();
if($status == 'all') {
$order = array_keys($list);
} else {
foreach ($list as $key => $value) {
if ($value['complete'] == $status) {
$order[] = $key;
}
}
}
7 Answers
Andrew Phythian
19,747 PointsProblem solved with respect to my original block of code
if($status === 'all') {
I needed a triple equal === instead of just a double. My mistake if that was what was in the video, I'd have to check to confirm.
Andrew Blackwell
13,264 PointsTo explain what's happening here: $status == 'all' is trying to compare a boolean with a string. In this case it's only an 'equal to' comparison and not an 'identical with' comparison, so the two sides are compared even if they are not equal data types. But to be able to compare apples to oranges, php converts oranges into apples, i.e. what it's doing here is converting the string into a boolean, and as long as the string is not empty, it translates to 'true'. So what it's checking for here is true == 'all', which is true == true, so it resolves the same as $status == true. That's why it is showing all to do items.
If, however, the comparison operator is changed to ===, as Andrew has already explained, then the data types are not changed when the two sides are compared, and 'all' is not translated by php into 'true' to be able to do the comparison. That's why the outcome is different here.
Juan Ignacio Lambardi
3,943 Points<?php
include 'list.php';
$order = array();
$status = false;
foreach ($list as $key => $item) { // This foreach display specicif keys depending of the condition is true or false.
if ( $item ['complete'] == $status ) {
$order[] = $key;
}
}
//var_dump($order); This values the key
//var_dump($list); This display each values of the $list array.
echo '<table>';
echo '<tr>';
echo '<th> Title </th>';
echo '<th> Priority </th>';
echo '<th> Due Date </th>';
echo '<th> Complete </th>';
echo'</tr>';
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 ( $item['complete']) {
echo 'Yes';
} else {
echo 'NO';
} "</td>\n>";
echo '</tr>';
}
echo '</table>';
Juan Ignacio Lambardi
3,943 PointsHi, Andrew. It should work. I added some comments in the file, maybe it's useful.
Andrew Phythian
19,747 PointsI tried it and $status = 'all' still doesn't work. Also, incomplete tasks are now showing as 'Yes', meaning completed.
Juan Ignacio Lambardi
3,943 Points<?php
include 'list.php';
$order = array();
$status = false;
foreach ($list as $key => $item) { // This foreach display specicif keys depending of the condition is true or false.
if ( $item ['complete'] == $status ) {
$order[] = $key;
}
}
Juan Ignacio Lambardi
3,943 Points$status = false; NOT $status = all Try this block of code.
coskun olcucu
5,340 Points$status=true; works fine on my code,but if i switch $status to false my output isn't the same as the video. all of the rows on complete column is "YES". Why is that happening?
Andrew Phythian
19,747 PointsAndrew Phythian
19,747 PointsThe problem is basically in this part of the code which I have trimmed for clarity...
"All" is displayed when $status is BOTH 'all' and true; it only displays FILTER when $status = false.