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
don bencilao
4,153 Pointssort 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
3 Answers
Welby Obeng
20,340 PointsTry 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>
?>
Welby Obeng
20,340 Pointsare you looking for
foreach($products as $product_id => $product){
$display = $product_id." ".$product."<br>".$display;
}
echo $display;
?
don bencilao
4,153 PointsHi welby,
how is this going to work? sorry I don't get it.
don bencilao
4,153 PointsHi 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"
);
```
Scott Evans
4,236 PointsScott Evans
4,236 PointsWhat sort of data is held in the
$productvariable?Is the product data retrieved from a database?