Repeatitive Items on Level 2 - php

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.

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>

Show subcategories for selected parent category in search.php in Osclass

I am trying to figure out how can I show subcategories for selected parent category in search.php. I have code which shows subcategories of all categories, but how can I modify this code to show subcategories for only selected category?
this is my code:
<?php while(osc_has_categories()) { ?>
<?php if(osc_count_subcategories() > 0) { ?>
<ul>
<?php while(osc_has_subcategories()) { ?>
<li>
<strong><?php echo osc_category_name(); ?></strong></label>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
thank you
When in a loop while(osc_has_categories()), it allows you to use osc_category_* helpers.
You could do something like:
<?php while(osc_has_categories()) { ?>
<?php if(osc_category_id() === 98): ?>
<?php if(osc_count_subcategories() > 0) { ?>
<ul>
<?php while(osc_has_subcategories()) { ?>
<li>
<strong><?php echo osc_category_name(); ?></strong>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php endif; ?>
<?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);

For loop, runs once?

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 } ?>

menu by default should hidden

Hey guys i want the sensor to be hidden by default and it show when i click on the particular node..and should be the same on page reload
Here is my code
<ul >
<?php if(isset($nodes)): ?>
<?php $count = 0; ?>
<?php foreach($nodes as $node) { ?>
<?php $node_id=$node['node_id']; ?>
<?php $sensors = config_sensor_model::getsensors($node_id); ?>
<?php $count++; ?>
<li onclick="menu(<?php echo $count; ?>)"><?php echo $node['node_name']; ?> </li>
<ul id="<?php echo "sub_".$count; ?>">
<?php foreach($sensors as $sensorlog) { ?>
<li> <?php echo $sensorlog->sensor_name; ?></li>
<?php } ?>
</ul>
<?php } ?>
<?php endif; ?>
</ul>
</div>
this is the javascript presently i am using
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
function menu(count)
{
$("#sub_"+count).toggle("fast");
}
</script>
As mentioned in my comment just use CSS to hide the node initially like this:
<style type="text/css">
.hidden { display: none; }
</style>
<ul>
if(isset($nodes)):
$count = 0;
foreach($nodes as $node) {
$node_id = $node['node_id'];
$sensors = config_sensor_model::getsensors($node_id);
$count++;
?>
<li onclick="menu(<?php echo $count; ?>)"><?php echo $node['node_name']; ?> </li>
<ul id="<?php echo "sub_".$count; ?>" class="hidden">
<?php foreach($sensors as $sensorlog) { ?>
<li> <?php echo $sensorlog->sensor_name; ?></li>
<?php } ?>
</ul>
<?php }
endif; ?>
</ul>
</div>
When menu is clicked, the display value will be toggled by Javascript.
Set style='display:none' for the <ul>

Categories