I'm searching for a way to sort the frontend display of categories in my navigation.
This is the code for my navigation:
<div id="menu-accordion" class="accordion">
<?php
foreach ($this->getStoreCategories() as $_category): ?>
<?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?>
<h3 class="accordion-toggle"><?php print $_category->getName();?></h3>
<div class="accordion-content">
<ul>
<?php foreach ($_category->getChildren() as $child): ?>
<li>
<span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span>
<?php print $child->getName();?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach ?>
</div>
I tried using asort() to sort $this->getStoreCategories(), but it resolved to an error 500, so I guess that it's not an array, but an object (which seems obvious for the objectoriented programming of magento). I tried finding a solution for object, but failed and now I'm a bit stuck.
Thanks for your help.
The call to $this->getStoreCategories() does not return an array. But you can build up your own array and use the key of the array as the element to sort on (assuming you want to sort by name of the category):
foreach ($this->getStoreCategories() as $_category)
{
$_categories[$_category->getName()] = $_category;
}
ksort($_categories);
Now instead of iterating over $this->getStoreCategories() you iterate over the $_categories array. So your code would look something like:
<div id="menu-accordion" class="accordion">
<?php
$_categories = array();
foreach ($this->getStoreCategories() as $_category)
{
$_categories[$_category->getName()] = $_category;
}
ksort($_categories);
foreach ($_categories as $_category): ?>
<?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?>
<h3 class="accordion-toggle"><?php print $_category->getName();?></h3>
<div class="accordion-content">
<ul>
<?php foreach ($_category->getChildren() as $child): ?>
<li>
<span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span>
<?php print $child->getName();?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach ?>
</div>
Related
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'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 } ?>
Ive got the follow PHP:
<div class="slide-background">
<div class="slide">
<?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
<?php if (12 / $cols == 1):?>
<div class="col-md-12">
<?php else: ?>
<div class="col-md-<?php echo 12 / $cols; ?>">
<?php endif; ?>
<ul>
<?php foreach($items as $submenu): ?>
<?php echo $submenu; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
basically it calculates how many links to display and how many columns, but i now need to place the links in <div class="slide"></div>, but based on the columns.. so basically i need to say if $cols = 2 place two div's in a div and close.. so its basically how many every $cols it should place so many div's in that div..
Its Confusing for me to even explain.. I think Ive explained it rather well above.. If not place say so and ill try again..
Any Help Greatly Appreciated..
UPDATE:
thanks to Hans ive now have the following:
<?php $linksPerColumn = ceil($linkCount / $cols); $linkCounter = 0;?>
<div class="slide-background">
<div class="slide">
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php foreach ($items->submenu as $link): ?>
<?php $linkCounter++;?>
<?php if($linkCounter % $linksPerColumn == 0):?>
</ul>
</div>
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php endif; ?>
<?php echo $link; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
only problem is when there's only one column and i need 2 links and then for it to close the div and the ul and start new ones.. right now it does that except for everyone and not for every two links...
You could use modulus for this one. You should calculate how many items you need per column. And then create a close and open div for example:
<?
$linksPerColumn = 4;
if ($linkCount > 4){
$linksPerColumn = ceil ($linkCount / $amountOfColums);
}
$linkCounter = 0;
?>
<div class="slide-background">
<div class="slide">
<?
foreach ($links as $link)
{
$linkCounter++;
?>
// Do your HTML Here.
<?
if($linkCounter % $linksPerColumn = 0)
{
?>
</div>
<div class="slide">
<?
}
?>
</div>
</div>
// Rest of the HTML here.
I think this should do the trick for you.
Hello :) I realy need your help here. I dynamically generate list of items but instead of putting each item in separate <li> I want to get something like this:
<ul>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
</ul>
Here is code I have:
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i++%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
</li>
<?php endforeach; ?>
</ul>
But as the result I get structure like this:
<ul>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
</ul>
Thank you for help
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
<?php if ($i%$itemCount==2): ?>
</li>
<?php endif; $i++; ?>
<?php endforeach; ?>
</ul>
You can try this.
<ul class="some-ul-class">
<?php $itemCount = 3;
$i=0;
foreach ($p->getItems() as $pt):
if ($i%$itemCount==0):
echo '<li class="item">';
endif;
echo "<div>$pt</div>";
if ($i%$itemCount==2):
echo '</li>';
endif; $i++;
endforeach; ?>
</ul>
Try something like this:
<ul class="some-ul-class">
<?php $itemCount = 4; ?>
<li>
<?php $i = 1; foreach ($p->getItems() as $pt): ?>
<?php if ( $i % $itemCount == 0): ?>
</li><li>
<?php endif; ?>
<?php $i++; ?>
<div><?php echo $pt; ?></div>
<?php endforeach; ?>
</li>
</ul>
This generates:
<ul class="some-ul-class">
<li>
<div>1</div>
<div>2</div>
<div>3</div>
</li><li>
<div>4</div>
<div>5</div>
<div>6</div>
</li>
</ul>
Demo
Your code will not achieve nested DIVS in LI because you need a multidimensional array to nest the items within the container. The solution is to break the initial DB result set in to chunks with array chunk.
This just splits your array (1,2,3,4,5,6) to ([0] => array(1,2,3), [2] => array(4,5,6)
Run through the loop below you will get two LI with 3 nested DIV. The code is not tested but should be something like operational.
<?php
$items = array(1,2,3,4,5,6,8,9,10,11,12,13,14,15);
// Your initial item array
$rows = 3;
// Number of rows in each li
$items = array_chunk($items, $rows);
// Final nested array in blocks of 3
if ($items) {
echo "<ul class='some-ul-class'>\n";
foreach ( $items as $item ) {
echo "<li class='items'>\n";
foreach ($item as $divs) {
echo "<div>{$divs}</div>\n";
}
echo "</li>\n";
}
echo "</ul>\n";
}
?>
I am attempting to make a sortable list out of list items populated from the database using the jQuery plug in but the effect is only applied to the first item presented:
<?php if(isset($bookmarks)) : foreach($bookmarks as $row) :?>
<div id="makeDrag">
<?php $fixed = preg_replace('#^[^:/.]*[:/]+#i', '', $row->URL); ?>
<li>
<div class="well">
<div><?php echo anchor('http://'.$fixed, $row->Name); ?></div>
<div><strong>Comments:</strong> <?php echo $row->Comments; ?></div>
<h4 class="btn-small">
<?php echo anchor("site/delete/$row->id", "Delete"); ?>
</h4>
</li>
</div>
<?php endforeach; ?>
I can kind of see where this is going wrong but do not know how to fix it. I would obviously like the effect to affect all the populated li not just the first one. Any help would be great. Sorry if I am unclear, I can try and rephrase things if this is confusing.
The cause is likely because you have
$('#makeDrag').sortable();
but you also have a foreach statement that creates multiple #makeDrag elements thus making your HTML invalid.
To fix this:
<?php if(isset($bookmarks)) : ?>
<ul id="makeDrag">
<?php foreach($bookmarks as $row) : ?>
<?php $fixed = preg_replace('#^[^:/.]*[:/]+#i', '', $row->URL); ?>
<li>
<div class="well">
<div><?php echo anchor('http://'.$fixed, $row->Name); ?></div>
<div><strong>Comments:</strong> <?php echo $row->Comments; ?></div>
<h4 class="btn-small"><?php echo anchor("site/delete/$row->id", "Delete"); ?></h4>
</div>
</li>
<? endforeach; ?>
</ul>
<?php endif; ?>
HTH