For loop, runs once? - php

I'm using OpenCart and I'm trying to achieve rendering out the meta_description (used to identify the comic publisher) and use it to make a drop-down list with sub-categories, or to give the illusion of it. Here is my code now, it's an adopted version of the current OpenCart code. ['class'] is how I grab the child categories meta_description.
Basically, the second for statement doesn't work - it only does the first one. I would appreciated any kind of support on this.
<div class="menu">
<div id="top"></div>
<span>
<ul id="nav">
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<div class="subs">
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<h3>DC Comics</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "DC Comics"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
<h3>Marvel</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "Marvel"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
</span>
</div>

Use different variables in loops, in you code $i is used in main loop and incremented in inner loops you can use foreach loop like ,
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if (is_array($category['children']) and isset($category['children'])) { ?>
<div class="subs">
<div>
<ul>
<?php
$li1='<li><h3>DC Comics</h3><ul>';
$li2='<li><h3>Marvel</h3><ul>';
foreach($category['children'] as $child)
{
if($child['class'] == "DC Comics")
{
$li1.='<li>'.$child['name'].'</li>';
}
if($child['class'] == "Marvel")
{
$li2.='<li>'.$child['name'].'</li>';
}
}
$li1.='</ul></li>';
$li2.='</ul></li>';
echo $li1;
echo $li2;
?>
</ul>
</div>
</div>
<?php } ?>
</li>
<?php } ?>

Related

Opencart Display subcategories

on each products page I currently have displayed the products subcategory, but only one level deep. So if in categories I have Brother > MFC > B4564 > TN450.... and I am on the TN450's product page, down below it will only show BN4564 . I need it also show The mfc and the brother part. I am not sure how to do this.. any ideas? here is my current code.
.TPL file
<!-- Display product categories -->
<?php foreach ($catprod as $catp)
{
?><?php echo $catp['name']; ?> <?php
}`
controller .php file
$data['catprod'] = array();
$product_category = $this->model_catalog_product->getCategories($this->request->get['product_id']);
foreach ($product_category as $prodcat)
{
$category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
if ($category_info)
{
$data['catprod'][] = array('name' => $category_info['description'],'href' => $this->url->link('product/category', 'path=' . $category_info['category_id']));
}
}`
which version of opencart?
this should work for version 1:
open catalog/view/theme/default/template/module/category.tpl
remove its contents and insert:
<div class="box" id="categories-menu">
<div class="box-heading"><?php echo $heading_title; ?></div>
<div class="box-content">
<ul class="box-category" id="box-category">
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<a href="<?php echo $category['href']; ?>" class="active list-group-item" ><?php echo $category['name']; ?></a>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul class="submenu">
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
- <?php echo $child['name']; ?>
<?php } else { ?>
- <?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>

How to show multilevel dropdown menu in opencart category module?

I want to Show Multilevel Dropdown menu in opencart 2.0 category module
Menu>submenu>submenu1>submenulevel2>level3>
Can you please explain if you want this in the Admin area or the customer front end?
In which case in category.tpl just modify or write something like this:
<?php if ($categories) { ?>
<?php if (count($categories) <= 5) { ?>
<div class="col-sm-3 nosidepadding">
<ul>
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?></li>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<?php foreach (array_chunk($categories, ceil(count($categories) / 4)) as $categories) { ?>
<div class="col-sm-3 nosidepadding">
<ul>
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?></li>
<?php } ?>
</ul>
</div>
<?php } ?>
<?php } ?>
<?php } ?>

I want to limit this foreach() loop in PHP to 2 outputs

This is the loop I want to limit this to 2 outputs and create another loop for further outputs please help.. I am a beginner
Enter code here
<?php if ($informations) { ?>
<div class="column">
<h3>
<?php echo $text_information; ?>
</h3>
<ul>
<?php foreach ($informations as $information){ ?>
<li><a href="<?php echo $information['href']; ?>">
<?php echo $information['title']; ?>
</a></li>
<?php } ?>
</ul>
</div>
<?php } ?>
Try this. .
<?php if ($informations) { ?>
<div class="column">
<h3><?php echo $text_information; ?></h3>
<ul>
<?php
$i=1;
foreach ($informations as $information){
if($i==2)
{
break;
}
?>
<li><?php echo $information['title']; ?></li>
<?php
$i++;
} ?>
</ul>
</div>
<?php } ?>
For other values . .
<?php if ($informations) { ?>
<div class="column">
<h3><?php echo $text_information; ?></h3>
<ul>
<?php
$i=1;
foreach ($informations as $information){
if($i<2)
{
continue;
}
?>
<li><?php echo $information['title']; ?></li>
<?php
$i++;
} ?>
</ul>
</div>
<?php } ?>
Bit vague but I guess you are looking for array_chunk,
$new_array = array_chunk($informations, 2, true);

Repeatitive Items on Level 2

I have a page that displays an organizational tree however in level 2 there are repeated items. What I wanted to is to avoid the duplicates...
Here's the example display:
<ul id="org" style="display: none;">
<li>Brit School
<ul>
<li>Amy Winehouse
<ul>
<li>Carina Round</li>
</ul>
</li>
<li>Adele Adkins
<ul>
<li>Kreayshawn K</li>
<li>Leona Lewis</li>
</ul>
</li>
<li>Adele Adkins
<ul>
<li>Kreayshawn K</li>
<li>Leona Lewis</li>
</ul>
<li>Arctic Monkey
<ul>
<li>PJ Harvey</li>
</ul>
</li>
</ul>
</li>
I wanted to omit the 2nd record Adele this is my example code in PHP
<ul id="org" style="display: none;">
<?php foreach ($lvl1 as $genesA) {?>
<li>
<?php echo $genesA->LevelFullName1?>
<?php if($genesA->lvlMemF) {?>
<ul>
<?php if($genesA->lvlMemF) {?>
<?php foreach ($lvl2 as $genesB) {?>
<?php if($genesB->lvlMemS) {?>
<li><?php echo $genesB->LevelFullName2 ?>
<?php if($genesB->lvlMemS) {?>
<ul>
<?php if($genesB->lvlMemS) {?>
<?php foreach ($lvl3 as $genesC) {?>
<?php if($genesB->lvlMemS == $genesC->referrerLvl3) {?>
<li>
<?php echo $genesC->LevelFullName3?>
</li>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } else { ?>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
<?php }?>
</li>
<?php } ?>
I'm not so sure if I am heading on the right track I just want a dirty fix that's all :)
that should do it:
<?php
$seen = array();
?>
<ul id="org" style="display: none;">
<?php foreach ($lvl1 as $genesA) {?>
<li>
<?php echo $genesA->LevelFullName1?>
<?php if($genesA->lvlMemF) {?>
<ul>
<?php if($genesA->lvlMemF) {?>
<?php foreach ($lvl2 as $genesB) {?>
<?php if($genesB->lvlMemS) {?>
<li><?php echo $genesB->LevelFullName2 ?>
<?php if($genesB->lvlMemS) {?>
<ul>
<?php if($genesB->lvlMemS) {?>
<?php foreach ($lvl3 as $genesC) {?>
<?php if($genesB->lvlMemS == $genesC->referrerLvl3) {?>
<?php if(!in_array($genesC->LevelFullName3, $seen)?>
<li>
<?php echo $genesC->LevelFullName3?>
<?php $seen[] =$genesC->LevelFullName3;?>
</li>
<?php }?>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } else { ?>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
<?php }?>
</li>
</ul>
you might want to check if my if is at the right place there because it could be that you get empty ul blocks if all the names already were seen.

how to change values between loop

I extracted the values of the array on the foreach loop, but I want when it retrieves a certain 'br_title' changed href link to <a href="http://noorresults.moe.sa..how to do that with simple code?..
<ul class="riMenu">
<?php foreach ($last_branches as $last) { ?>
<li> <?php if (!empty($last['br_title'])) echo $last['br_title'] ?></li>
<?php }; ?>
</ul>
</div>
</div><!-- Menu E
You can change your code as below
<?php
$total_to_show = 5; // take offset
$count = 1; //counter until it reach to offset
foreach ($last_branches as $last) { if($count > $total_to_show){?>
<li> <?php if(!empty($last['br_title'])) echo $last['br_title'] ?></li>
<? }else{?>
<li> <?php if(!empty($last['br_title'])) echo $last['br_title'] ?></li>
<?php }
$count++;
} ?>

Categories