Jump to content

Smarty loop with 2 arrays, help :(


Jérémie P.

Recommended Posts

Hello!!

 

Anyone could help me please in smarty because what I wanna do doesn't work, something missing but what?

 

In PHP, what I did and it works

 

 

foreach($categories AS $row)
{
$categories[] = intval($row['id_category']);
$categories[][intval($row['id_category'])] = $this->get_Products(intval($row['id_category']), intval($cookie->id_lang), $nb);
}

$smarty->assign(array(
'categories' => $categories,
'lang' => Language::getIsoById(intval($params['cookie']->id_lang)),
));

 

And in smarty:

 

{foreach from=$categories item=category name=homeCategories key="i"}
{assign var='categoryLink' value=$link->getcategoryLink($category.id_category, $category.link_rewrite)}

<div class="col">
<h6><a href="{$categoryLink}" title="{$categoryName}">{$category.name}</a></h6>
<ul>
{assign var=val value=$category.id_category}
{foreach from=$category[$val] item=produit}
<li><a href="{$produit.link}" title="{$produit.name|escape:'htmlall':'UTF-8'}">{$produit.name|escape:'htmlall':'UTF-8'|ucfirst}</a></li>
{/foreach}
</ul>
</div><!-- //col-->
{/foreach}

 

The second foreach doesn't work with $category[$val]

$val have the value '22' if I display it. When I replace it by the number 22 like $category[22] it works, I have my products listing.

 

Anyone knows where is the mistake?

 

Thanks a lot.

Link to comment
Share on other sites

You are using $categoryName but you did'nt assign it anywhere.

And you are adding row to your array (so you will have the content of your original $categories first).

I think you should learn how to use properly, this is not a PrestaShop knowledge but a simple php knowledge.

There are others mistakes in your code like {$produit.link} where you should use {$produit.link_rewrite}, {$categoryName} was not assigned, ...

 

Your code should look something like this :

 


foreach($categories AS $key => $row)
{
 // just fill the current array with productsList
 // I will assume your $nb var is already filled
 $categories[$key]['productsList'] = $this->get_Products(intval($row['id_category']), intval($cookie->id_lang), $nb);
}

$smarty->assign(array(
'categories' => $categories,
'lang' => Language::getIsoById(intval($params['cookie']->id_lang)),
));

 


{foreach from=$categories item=category}
 {assign var='categoryLink' value=$link->getcategoryLink($category.id_category, $category.link_rewrite)}

 <div class="col">
<h6><a href="{$categoryLink}" title="{$category.name}">{$category.name}</a></h6>
<ul>
  {foreach from=$category.productsList item=product}
	 <li><a href="{$product.link_rewrite}" title="{$product.name|escape:'htmlall':'UTF-8'}">{$product.name|escape:'htmlall':'UTF-8'|ucfirst}</a></li>
  {/foreach}
</ul>
 </div><!-- //col-->
{/foreach}

 

Edit 1 : I didn't try the code I wrote above but I think it should work.

Edit 2 : For your original php code, just add a print_r($categories); after your foreach, you will see what your mistake is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...