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

sort list by latest

Hi, I want to know how can I display a product list starting from the last product id.

      <?php foreach($products as $product_id => $product){
                echo product_list($product_id,$product);
                                }  
    ?>

in the build a simple php app tutorial, the guy limits the view with only four latest item. I don't want to limit the items with only four items, I want to display them all starting with the latest items.

thanks, don

What sort of data is held in the $product variable?

Is the product data retrieved from a database?

3 Answers

Try this

<?php

$products = array();

$products[101] = array( "id" => "1", "name" => "joe" );

$products[102] = array( "id" => "2", "name" => "nick" );

$products[103] = array( "id" => "3", "name" => "john" );


   foreach($products as $product){

                        $display = $product["id"]." ".$product["name"]."<br>".$display;
                                 // 1  Joe break
                         // 2  nick break (1  Joe <br>)
                         // 3  john break (2  nick <br> (1  Joe <br>))
                    }  

                    echo $display; //3  john <br>, 2  nick <br>, 1  Joe <br>
    ?>

are you looking for

   foreach($products as $product_id => $product){
                        $display = $product_id." ".$product."<br>".$display;
                    }  
                    echo $display; 

?

Hi welby,

how is this going to work? sorry I don't get it.

Hi Scott,

no, it's in a collection of array.

like this

$products = array();

$products[101] = array(
   "id" => "1",
   "name" => "joe"
);

$products[102] = array(
   "id" => "2",
   "name" => "nick"
);
            ```