Jian Short
Jian Short

Reputation: 135

Displaying Smarty associative array

What I am trying to achieve is this but I can't think of how to display it:


Product name: example of the product name

Description: example of product description

Price: $10.00

Discount: 10%


This is how I assign it in my PHP file

$array = array();

while ( $row = mysqli_fetch_array( $result ) ){

    $array['product_name'][] = $row['product_name']; //array('product_name' => array( [0] => 'productname1' [1] => 'productname2' ))
    $array['description'][] = $row['description'];
    $array['unit_available'][] = $row['unit_available'];
    $array['price'][] = $row['price'];
    $array['discount'][] = $row['discount'];
    $array['discount_status'][] = $row['discount_status'];
}

return $array;


$smarty->assign('thumbnail', $thumbnail);

This is the part where I am stucked:

{foreach from=$thumbnail key=key item=item} 

{/foreach}

Please help me.

Upvotes: 0

Views: 51

Answers (1)

Borgtex
Borgtex

Reputation: 3245

Your array mapping is wrong, you have to nest the properties of the product under the same index, not create unrelated keys for each property in the same level

So it should be:

$products[]=array(
   'product_name'=>$row['product_name'],
   'description'=>$row['description'],
   ...and so on...
   );

then in smarty is as simple as

{foreach from=$products key=key item=item}

  Product name: {$item.product_name}<br/>
  Description: {$item.description}<br/>
  ...
{/foreach}

Upvotes: 1

Related Questions