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

John Lukacs
John Lukacs
26,806 Points

I want to take a shotgun to both sides of my head

Is this just me or is this painfully difficult. But W H Y do you need to make one array keys another array values. Why not just make another array. Why do we want to sort this array isen't that what sql is for

Matthew Stroh
Matthew Stroh
3,307 Points

If you're reading data from a SQL database, YES you CAN sort the data coming in ( and it's generally a good practice ), however, what you're reading data from a file? What if you're reading data from a JSON API? An XML file? What about a comma-separated list of strings that you allowed the user to enter into a form? But lets say you're calling from a database and displaying it like a table as in the demo. It's common to allow sorting by clicking on a column heading. Are you going to call the database again, creating a different dynamic SQL string sorting by that new column, creating a full round-trip to the webserver and SQL database? That doesn't make sense from a performance perspective. You'd probably either do it in javascript ( with a similar technique as in this video ) and have a PHP fallback that deals with an array that you have cached in memory.

In short, SQL is not ONLY for sorting data. It's a human readable language mainly for creating, retrieving, updating and deleting data in a long-term storage area. Your business logic is best kept separate from your boilerplate SQL code... which acts more or less like plumbing. The less you mess with SQL in PHP, the fewer security risks you'll have as well.

The reason it is done this way with two arrays is for flexibility and short, terse code. Shorter code is more maintainable, more testable, and more flexible. You could easily read a query string parameter, for example.. like ?sortby=priority to allow the user to directly choose how to sort.

I feel you there brother.

Haruna Bulus
Haruna Bulus
2,006 Points

Hi everyone,

Please was there ever any response on simplifying(explaining/understanding) the contents of this video or we are generally left to sort ourselves out?

Thank you.

26 Answers

Leo Salinas
Leo Salinas
1,531 Points

I think I need to see this video like 30 more times...

thank you for saying it... I've watched this video like 3 times already and I don't understand why she's doing what she's doing and the code gets so messy.

Sebastian Karg
Sebastian Karg
28,705 Points

I've commented the lines of code while pausing the video after each sentence and thinking about what Alena said. This helped me to understand what's going on. Hope this will also help you to understand

<?php

include 'list.php';         //including tasks from the array $list at list.php


//if set to true see list of complete items; if set to false see list of uncomplete items; if set to 'all' see list of all items

$status = 'all';             //set $status

$field = 'priority';       //insert the tabel field as value of $sort that tasks will be sorted after


//check which tasks in $list should be displayed

$order = array();                           //create a new array called $order
 if ($status == 'all'){                      //check if status is all
  $order = array_keys($list);        //if status is 'all' display all tasks in our table
 } else {                                     //if status is NOT 'all' only display complete(status=true) or uncomlplete(status=false) tasks
    foreach ($list as $key => $item) {       //loop through $list and check each key => value pair
      if ($item['complete'] == $status) {    //if $list item key 'complete' matches $status
  $order[] = $key;                           //store the key(int) of the matching item as value into $order
    } 
  }
}

//sort tasks by table field

if($field) {                                 //if $field is set we sort by that field; if $field is not set we don't sort anything
  $sort = array();                           //create an array to store the sorted tasks
  foreach($order as $id) {                   //loop through $order and use $id as value in $sort  
    $sort[$id] = $list[$id][$field];         //$id = value of $sort = key of $field from $list
  }
  asort($sort);                              //sorting $sort by $field while keeping the key association the same as in $list
  $order = array_keys($sort);                //update the keys of $order 
}


/************************* 

if you var_dump $list, $sort, $order and view the source code you could see how the keys of $list are sorted by $field and become the values of 
$order

**************************/

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

//creating a html table

echo '<table>';                         
echo '<tr>';
echo '<th>Title</th>';
echo '<th>Priority</th>';
echo '<th>Due Date</th>';
echo '<th>Complete</th>';
echo '</tr>';

// looping through $order to display tasks in our todo-list

foreach ($order as $id) {                            //loop through $order and use $id to identify the key of the $list item to display 
  echo '<tr>';
  echo '<td>' . $list[$id]['title'] . "</td>\n";    //display titel of $list item
  echo '<td>' . $list[$id]['priority'] . "</td>\n"; //display priority $list item
  echo '<td>' . $list[$id]['due'] . "</td>\n";      //display due date $list item
  echo '<td>'; 
    if($list[$id]['complete']) {                    //check if task is complete
      echo 'Yes';                                   //if task is complete display Yes
    } else {
      echo 'NO';                                    //if task is NOT complete display NO
    }

//end html table

  echo "</td>\n";                         
  echo '</tr>';                                     
}
echo '</table>';

?>

hahaha..i guess i am not the only one who's brain got into propeller :D watched it like 5 times or so, definetely you should either break it in few seperate videos. If there is more then 2 people who finds this video difficult, then probably you should fix it. Didn't understand anything of this video..not the logic, not the relationship within the codes..aboslute "0". But one thing is good..it was a lot of fun for me to laughing at myself, so it's not so bad anyway. let's watch this "brainwasher'" few more times...go go go :D good luck everyone.

jim felli
jim felli
13,401 Points

it's been 4 years since you posted this, and nothing has changed.

Dazeran Zamri
Dazeran Zamri
5,992 Points

I second it John lol. The track was getting consistently harder at a pace i was comfortable with. But this particular part just wanna blow my brain haha =D

The part of this video that I can't seem to get to work is the check on status - it worked fine as true or false but once I factored in 'all' with the corresponding 'if' statement below it then it only showed all the items regardless of the check. I've tried a few times but still doesn't seem to like the code. From about 4:22 onwards in the video.

I solved this problem, explained elsewhere. https://teamtreehouse.com/community/todo-app-status-all-part

You're right it's a mistake in the video. In the video the code is: if($status == 'all') but it needs to be if($status === 'all')

Thanks

Vladislav Mavrin
PLUS
Vladislav Mavrin
Courses Plus Student 3,125 Points

I also find it quite difficult to understand without a graphic scheme. I'm getting lost keeping track of 3 arrays and values we are grabbing from them. A scheme showing what is being grabbed from what array would be very useful

Yeah, this video was a bit much haha

Sulaiman Noh
Sulaiman Noh
14,538 Points

Just explained to us the code like you explained to a children. When Elena go with all the big word and explained what goes with what without a help from a graphic, my mind just process that she are babbling things that herself can understand. Give us example why you do certain things Elena. I don't want to follow things without understand why should I do this.

Why the instructor not making the note as a skeleton first like before? it become so confusing.

Anne Donald
Anne Donald
9,847 Points

Thank the Lord I'm not the only person to think that this video should have been split into shorter and simpler videos.. My brain hurts... and my eyes are now swirling in my head! I really need it to be explained to me like a child!

Simon Coates
Simon Coates
28,694 Points

um, array copy by default is an actual copy, I think. SO creating new arrays might seem simpler, but might burn up resources. And this approach might be simpler to think about than custom sorting (likely uses callbacks). It's difficult to know if this is an artificial exercise (that might represent an ungainly work-around to avoid techniques that haven't been introduced yet) and how useful arrays of indexes (or indices, whatever) are generally.

Still for an introductory course, ouch!

Mike B
Mike B
10,424 Points

Guys, it's been over a year since the first person issued a cry for help and nothing has been done to help this video become even slightly clearer. From the moment Alena introduces $order[] = $key, I'm lost and that is only 1/4 of the way into this video. My brain feels like it's been run through a blender.

Martin Park
Martin Park
12,792 Points

I also found this video painfully hard to follow :(

Daniel Fitzhugh
Daniel Fitzhugh
9,715 Points

Yes this is extremely annoying, and it's a pattern with her videos.

She explains the basic principles then moves straight into a complicated process where she applies the principles.

But the problem is that she doesn't explain her goals and her desired result beforehand.... she just goes into process, process, process. So she needs to say at the start: "Here is the result I want. And here is the process I'm going to follow". And she needs to keep referring back to the desired result as she is following the process.

Cliff Jackson
Cliff Jackson
2,887 Points

This is the reason I stopped using this course, it’s not explained where and why the code might be used. Ive found another course online which go’s into detail about how the language is used and is also up to date unlike some of the content on this course.

This is the worst video I've seen on this entire site.

Amy Dollar
Amy Dollar
2,562 Points

sooo glad i'm not the only one lol, I hope this isn't the way the rest of the course is going to go! I was getting it all up to now... I'm gonna fast forward to the next lesson and forget this ever happened ;)

Don't pull the trigger my friend.

I thought I was the only one, I really don't understand, I'm either too tired or this is really hard to grasp, I'm taking a break for now maybe then I can understand this clearly

This made me feel so much better. Usually I can watch the videos at 1.5x speed and this one I SLOOOOWED down and I'm still scratching my head...

Umy Ikem
Umy Ikem
21,383 Points

I actually had an inkling that the video was going to be difficult before it even started just jumping from Foreach loops to Todo app didn't seem right. Wayyyyy tooooo much info to grasp, even though i already had experience with multi dimensional arrays.

I think the trick is watching the video multiple times & also following her in all the videos in this section using Workspaces or your local IDE, this way its less confusing and a bit clearer. I still think the videos need to be split up though.

Cliff Jackson
Cliff Jackson
2,887 Points

I am yet to watch this video but like many here i have the problem of the videos being too long with too much info to try and process at once. Loops i found difficult to grasp but i also have a PHP book which is actually a lot better in parts than watching the videos. I get the feeling the tutor try's to over complicate things by showing too many variations on a topic which leaves you confused. Well after watching this i am left baffled by all the various add on bits and the way the code runs. You would think that Alena would of seen the comments here and tried to amend or just redo the whole video again

Yohan Park
Yohan Park
7,148 Points

I don't think anyone's still reading at this point but I've commented each line so I can follow along and understand what she's doing. I've done that three times but I still lose track of what she's doing.

Just reading this gives me a migraine.

if($field) {                                 //if $field is set we sort by that field; if $field is not set we don't sort anything
  $sort = array();                           //create an array to store the sorted tasks
  foreach($order as $id) {                   //loop through $order and use $id as value in $sort  
    $sort[$id] = $list[$id][$field];         //$id = value of $sort = key of $field from $list
  }
  asort($sort);                              //sorting $sort by $field while keeping the key association the same as in $list
  $order = array_keys($sort);                //update the keys of $order 
}

yes this video was absolutely insane.

i love the slow pace from other videos, but this one suddenly blew up to only make sense for 30 year + developers.

sigh..

Ker Sing Tan
Ker Sing Tan
10,573 Points

Who else is still watching this on 2020 ? I feel like only genius with super high IQ can understand this. The explanation of the process should be more beginner friendly.

Nick Mortensen
Nick Mortensen
2,104 Points

This information might've been better split into several shorter lessons. That was a lot to throw at a person -- probably too much at once.

Kevin Skoglund would never do that to the client.

Dan Avramescu
Dan Avramescu
11,286 Points

Sebastian Karg

$order = array_keys($sort);                //update the keys of $order 

this comment should be

//update the values of $order to be the keys of $sort

if I'm right (?)