Saturday, 15 September 2012

php - Foreach loop does not compare -



php - Foreach loop does not compare -

i of programming noob, , stuck issue trying echo out few items of array instead of items in array.

so have products.php file contains array $products, trying phone call array index page, , want not phone call array, have products in array list lastly 4 products instead of of them.

so in index.php have created code:

$total_products = count($products); $position = 0; $list_view_html = ""; foreach($products $product_id => $product) { $position = $position + 1; if($total_product - $position < 4){ $list_view_html = $list_view_html . get_list_view_html($product_id,$product); } } echo $list_view_html;

now when save , load webpage, still see products listed , not 4 should seeing.

from understand, counting products in array, starting loop @ 0, looping through array , increment count +1. stating if products in loops position less 4 items want shown.

i know code might not best way efficiency wise, or has critiques. appreciate help, need figure out why cant work.

any help appreciated, less condescending better.

thanks guys

this typo. alter if to:

if($total_products - $position < 4){

note var's name $total_products . using $total_product without 's' , end.

php arrays loops foreach

No comments:

Post a Comment