Jump to content

[SOLVED] Banging my head against a wall... Nested foreach and multidimensional arrays


cableguy1115

Recommended Posts

Hello,

 

I have this code:

<ul>	{assign var='all_categories' value=Category::getCategories(Context::getContext()->language->id)}	{foreach from=$all_categories name=outer item=pagecat key=key1}			{foreach from=$pagecat name=mid item=subcat key=key2}					{foreach from=$subcat name=inner item=innercat key=key3}								{assign var="idca" value=$innercat.id_category}				{assign var="idpa" value=$innercat.id_parent}				{assign var="name" value=$innercat.name}				{assign var="lirw" value=$innercat.link_rewrite}				{assign var="lvdp" value=$innercat.level_depth}								{if ($idpa == $id_category && $idpa != $idca) || ($idpa == $id_category_parent && $idpa != $id_category && $lvdp > 2)}					{if $lirw neq 'man' && $lirw neq 'woman'}						<li><a href="/{$lang_iso}/{$idca}-{$lirw}"><span>{$name}</span></a></li> 					{/if}				{else}				{/if}							{/foreach}					{/foreach}			{/foreach}</ul>

It's supposed to explode the "all_categories" array:

Array(    [0] => Array        (            [1] => Array                (                    [infos] => Array                        (                            [id_category] => 1                            [id_parent] => 0                            [id_shop_default] => 1                            [level_depth] => 0                            [nleft] => 1                            [nright] => 58                            [active] => 1                            [date_add] => 2015-10-21 16:51:55                            [date_upd] => 2015-10-21 16:51:55                            [position] => 0                            [is_root_category] => 0                            [id_shop] => 1                            [id_lang] => 1                            [name] => Raíz                            [description] =>                             [link_rewrite] => raiz                            [meta_title] =>                             [meta_keywords] =>                             [meta_description] =>                         )                )        ))

It works perfectly until you get to the page that displays the 3rd subcategory, then it goes blank.

 

Sample: http://www.bustomshoes.com/es/12-modelos

HINT: Select the model on the left, then on the right select the gender.

 

I've been trying for two days now to create a third condition, something like "OR IF the SECOND parent of THIS category is the same same, display all categories, etc.". Basically, what I've been trying is to find a way to get the second parent (grandfather) of the curent category, then I could just simply echo all the entries that fulfill that condition.

 

Helpppp! :((

 

Edited by cableguy1115 (see edit history)
Link to comment
Share on other sites

[sOLVED]

 

My final code to be able to get the grandfather category ID (parent parent ID):

 

In Category.tpl :

 

Notice $parents[1] array. $parents[0] would give the parent ID, [1] grandparent, [2] great grand parent, and so on....

<ul>
	<li><span>Models</span>
		<ul>
			{assign var=gf value=$parents[1].id_parent} // <-- THIS LINE stores grandparent ID (delete this comment)
			{assign var='all_categories' value=Category::getCategories(Context::getContext()->language->id)}
			
			{foreach from=$all_categories name=outer item=pagecat key=key1}
			
				{foreach from=$pagecat name=mid item=subcat key=key2}
				
					{foreach from=$subcat name=inner item=innercat key=key3}
					
					{assign var="idca" value=$innercat.id_category}
					{assign var="idpa" value=$innercat.id_parent}
					{assign var="name" value=$innercat.name}
					{assign var="lirw" value=$innercat.link_rewrite}
					{assign var="lvdp" value=$innercat.level_depth}
					
					{if ($idpa == $id_category && $idpa != $idca) || ($idpa == $id_category_parent && $idpa != $id_category && $lvdp > 2) || ($idpa == $gf && $lvdp == '3')}
						{if $lirw neq 'man' && $lirw neq 'woman'}
							<li><a href="/{$lang_iso}/{$idca}-{$lirw}"><span>{$name}</span></a></li> 
						{/if}
					{/if}
					
					{/foreach}
					
				{/foreach}
				
			{/foreach}
			
		</ul>
	</li>
</ul>

In CategoryController.php, modify the initContent() function and add the lines:

$array_parent = $this->category->getParentsCategories();   // <-- THIS LINE (delete this comment)
		
        $this->context->smarty->assign(array(
            'category'             => $this->category,
            'description_short'    => Tools::truncateString($this->category->description, 350),
            'products'             => (isset($this->cat_products) && $this->cat_products) ? $this->cat_products : null,
            'id_category'          => (int)$this->category->id,
            'id_category_parent'   => (int)$this->category->id_parent,
	    'parents' 		   => $array_parent,   // <-- THIS LINE (delete this comment)
	    'return_category_name' => Tools::safeOutput($this->category->name),
            'path'                 => Tools::getPath($this->category->id),
            'add_prod_display'     => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
            'categorySize'         => Image::getSize(ImageType::getFormatedName('category')),
            'mediumSize'           => Image::getSize(ImageType::getFormatedName('medium')),
            'thumbSceneSize'       => Image::getSize(ImageType::getFormatedName('m_scene')),
            'homeSize'             => Image::getSize(ImageType::getFormatedName('home')),
            'allow_oosp'           => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK'),
            'comparator_max_item'  => (int)Configuration::get('PS_COMPARATOR_MAX_ITEM'),
            'suppliers'            => Supplier::getSuppliers(),
            'body_classes'         => array($this->php_self.'-'.$this->category->id, $this->php_self.'-'.$this->category->link_rewrite)
        ));
Edited by cableguy1115 (see edit history)
  • Like 1
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...