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.

PHP

Jake White
Jake White
41,721 Points

Array_reverse

Just curious Randy Hoyt, I have a product page that shows a large list of designs. Ive followed your courses on how to show the four newest on the home page. Heres the issue that I'm having though. The way the code works that you've showed us, it displays the last four on the list of products as the newest. The way Im building my site however, the newest designs are the first four, that way when users go to the product page, they see the newest designs first. Ive tried to use array_reverse but I've found that all that does is reverse the order of the last four on the home page. Do you have any suggestions?

8 Answers

Julien Gascard
Julien Gascard
12,818 Points

Just replace

if ($total_products - $position < 4) {

by

if($position < 4) {

Than you should get the elements number 0, 1, 2 and 3 of your array, instead of last, last - 1, last - 2 and last - 3.

Hope it helps !

Jake White
Jake White
41,721 Points

This almost worked. All I had to do was change the 4 to a 5. Thanks for the help. While building it out originally, I changed it to if ($total_products - $position > 4) {

The problem with that was that every time I added a product, I would have to increase the integer by one. Something had to change. Thanks for the help everyone

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

(A) This conditional is true for the last four elements in the array:

if ($total_products - $position < 4) {
    $recent[] = $still;
}

(B) This conditional is true for the first four elements in the array:

if ($position <= 4) {
    $recent[] = $still;
}

That checks if position is less than or equal to 4. Change the conditional in your code from A to B.

Does that help?

Are you using MYSQL? If you are then you could simply sort in a query. Haven't gone through the PHP course here so I'm not 100% of the route that they take. Can you reply with which lesson you've gotten up to in PHP?

Jake White
Jake White
41,721 Points

No I've been following the php courses. We've put all the products in arrays and use functions to display them

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,361 Points

Hey Jake,

If you're using the Treehouse php courses and following them exactly, without looking at your code, the only thing I can suggest trying would be to use array_reverse on the products array in the get_products_all function.

function get_products_all() {
// your product code
return array_reverse($products);
}

Hope this helps.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Would you be able to share this block of code so we can see how it's different than the project code?

On the home page, we have a foreach loop that goes through all of the shirts. That foreach loop has a counter that knows the position of the current shirt. Inside the foreach loop, we have a conditional that checks if the shirt's position subtracted from the total number of shirts is less than four; that will display the last four shirts.

In your case, you want to display the first four shirts. Is that right? You can change the conditional ... I think checking if the shirt's position is less than or equal to 4 should work.

Jake White
Jake White
41,721 Points

Here's the code My get_products_recent function function get_products_recent(){

$recent = array();
$all = get_products_all();
$total_products = count($all);
$position = 0;
foreach($all as $still) {
    $position = $position + 1;
    if ($total_products - $position < 4) {
        $recent[] = $still;
    }
}

return $recent;

};

Let's assume that i have ten stills, each named in order (1,2,3,etc.) and 1 being the newest. When I call on my home page this code <?php include('inc/still.php'); $recent = get_products_recent();?> <ul class="newestProducts"> <?php $list_view_html = ""; foreach($recent as $still) { $list_view_html = $list_view_html . get_list_view_html($still); }

                                echo $list_view_html;
                              ?>                                
                            </ul>    

...it give stills 7-10. I want stills 1-4 to show. Ive tried the the array_reverse when returning $recent in the get_products_recent function, but it returns 10-7.

Jake White
Jake White
41,721 Points

Sorry about how my code appears. It always seems to mess up, even with doing the 4 spaces.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

(PS: For better code formatting in this forum, you can fence your code blocks with backticks instead of using four spaces. Check out GitHub's Markdown documentation for an example: [https://help.github.com/articles/github-flavored-markdown#fenced-code-blocks].)