Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Steven Griffiths3,165 Points
How does changing the last four shirts' order work?
This is more for my reference than anything else, but if it helps someone else like me then great.
As mentioned in the other posts, when the if statement in the loop returns "true" for the first time it means we have one of the last four shirts from the array.
The get_list_view_html function returns the HTML for that particular shirt and stores it in the $list_view_html variable where it is held and then the loop runs again.
After the next loop the get_list_view_html function returns the HTML for the next shirt in the array and that is held in the $list_view_html variable also. We keep looping and storing the HTML in the $list_view_html variable until we reach the end of the array.
Now we have all the HTML for the last four shirts in the $list_view_html variable.
The order in which the shirts display depends on whether you concatenate the get_list_view_html function after (to the right of) or before (to the left of) the $list_view_html variable.
I think many people, particularly those who have the mind of a natural coder will find this an extremely basic concept to grasp, that a child could understand without too much explaining, but for others like me and the guy in the other post who compared it to Voodoo it's completely bewildering, it took me quite a bit of time to figure it out and I had to draw pictures and everything!
It helped me to visualise it this way. Think of those children's wooden blocks with a letter of the alphabet printed on each one representing one single return value from the get_list_view_function.
You take the first one "A" and place it on its own.
The alphabet reads from left to right so for next block "B" you place it to the right of "A".
We now have two blocks together and these multiple blocks will from now on represent the $list_view_html variable.
The next block "C" is added to the right side and now the multiple blocks read A, B, C.
We do the same for "D" and now we have "A", "B", "C", "D".
If at the beginning we instead placed the "B" to the LEFT of "A" and then placed the "C" to the LEFT of "B" and so on, then the blocks would read "D", "C", "B", "A".
This is exactly the same with the way the $list_view_html variable and the return value of the get_list_view_html function is being concatenated
Zenil Shroff3,410 Points
There are many ways to achieve our desired result, in this case TTH chose the easiest way to show us as the video was made for beginners, there are many ways of reversing array and while working with databases query of order can be reversed, so this is just a simple way to show how things work nothing else.
The trick shown to us works only when there are few elements in an array, when there are thousands or millions and to find last four, this is a very bad idea. Nobody would want their loop to run till end, instead reverse the order and get the desired first 4 results.