How to increment $var value without a loop in PHP? - php

I have this issue where i want to increase a value with 1 and apply this to my HTML, but i can't use a for() or while() loop (at least i think i can't). I'm customizing an e-merchandise program (opencart) and my php knowledge isn't enough to tackle the problem.
There is this function which displays the categories from the store. It uses a variable that is constantly updated via $var .= "value".
I'm so far that i know how many sub-categories there are, but i don't know how to apply this range to my HTML.
I'm working towards a situation like below
<ul id="cats">
<li id="cat1">Cat
<ul id="sub1">
<li>item</li>
<li>item</li>
</ul>
</li>
<li id="cat2">Cat
<ul id="sub2">
<li>item</li>
<li>item</li>
</ul>
</li>
</ul>
I don't have a clue how to increase the count of the second unordered lists. Below the code where the second unordered lists are generated.
[..]
$cPiD = strlen($parent_id);
if ($results) {
if ($parent_id == 0) {
$output .= '<ul id="cats">';
} else {
$output .= '<ul id="sub'.$cPiD.'">';
}
}
[..]
The variable $cPiD holds the total amount of sub categories (in this case 2). I want this variable to automatically apply the correct number to the unordered list (so apply id="sub1" to the first unordered list and id="sub2" tot he second one (as in my example above)).
The problem is that i can't use a for() loop after the else part, because in my HTML i wil get two <ul> tags instead of one.
Below the PHP code where it all happens
$category_id = array_shift($this->path);
$output = '';
$results = $this->model_catalog_category->getCategories($parent_id);
$count = 0;
$cPiD = strlen($parent_id);
if ($results) {
if ($parent_id == 0) {
$output .= '<ul id="cats">';
} else {
$output .= '<ul id="sub'.$cPiD.'">';
}
}
foreach ($results as $result) {
$count++;
if (!$current_path) {
$new_path = $result['category_id'];
$output .= '<li id="cat'.$count.'">';
} else {
$new_path = $current_path . '_' . $result['category_id'];
$output .= '<li>';
}
$children = '';
$children = $this->getCategories($result['category_id'], $new_path);
$output .= $result['name'];
$output .= $children;
if (!$current_path) {
$output .= '</li>';
} else {
$output .= '</li>';
}
}
if ($results) {
if ($parent_id == 0) {
$output .= '</ul>';
} else {
$output .= '</ul>';
}
}
Does anybody maybe have an idea how to solve this?
EDIT:
Oh, i tries adding the following construction in the foreach() loop, but that gave problems when a certain categories don't have any sub categories.
if (!$current_path) {
$output .= '$result['name'] . ' <ul id="sub'.$count.'">';
}else{
$output .= $result['name'];
}

You can use this:
// at the top of your code (ouside of the loop)
$cPiD = 1;
// inside the loop you need to increment the parameter
$output .= '<ul id="sub'.$cPiD++.'">';
After each time the item is used, its value will be incremented by 1. (after already using it)

Related

Remove Icon Arrow if Category haven't Sub-category in Laravel

enter code hereDoes any one can advice me how to remove the icon for main category havent's sub-categories.
I have pass to show sub-categories under main category.
Here is my code:
[enter image description here][1]
My view:
[blade View][2]
[1]: https://i.stack.imgur.com/MbDuB.png
[2]: https://i.stack.imgur.com/ieAqL.png
My Update with code
public static function showCategories($categories, $parent_id = 0)
{
$class ='';
$html = ""; //init
$flag = false;
foreach ($categories as $row) {
if ($row['parent_id'] == $parent_id) {
//we have a category
$link =self::linkCategory($row['category_id'],$row['name']);
$html .= '<ul id="accordion" class="accordion">
<li>
<div class="link accordion-title">'.$row['name'].'<i class="fa fa-chevron-down"></i></div>';
$html .= "<ul class='submenu'>";
foreach ($categories as $subcat) {
$link =self::linkCategory($subcat['category_id'],$subcat['name']);
if ($subcat['parent_id'] ==$row['category_id']){
//we have a subcategory
$html .= '<li>'.$subcat['name'].'</li>';
}
}
$html .= "</ul>";
$html .= "</li>";
$html .= "</ul>";
}
}
return $html;
//call it blade
$menu = URL::showCategories($itemCategory);
first of all its a bad practice that you use HTML in control, you have to use HTML only in blade sec you can do that by making the relation between categories and itself to get the sub cat to every cat then if your blade while you are looping it u can easily use Count() to get number of sub cat if it bigger than 0
add icon if don't add it
#if(Count($cat->sub) > 0)
<i class="icon-class">
#endif

Magento diplaying category tree dynamically

I need to display my category tree as a list in a responsive menu.
The idea is to display the highest level categories. and create dynamically a list that will be displayed for each category that has children.
I stumbled upon a code that helped me a bit, but i can't figure how to get the job done.
Here is the code:
<?php
$rootCatId = Mage::app()->getStore()->getRootCategoryId();
function getTreeCategories($parentId, $isChild){
$allCats = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active','1')
->addAttributeToFilter('include_in_menu','1')
->addAttributeToFilter('parent_id',array('eq' => $parentId));
$class = ($isChild) ? "sub-cat-list" : "cat-list";
$html .= '<ul class="'.$class.'">';
$children = Mage::getModel('catalog/category')->getCategories(7);
foreach ($children as $category) {
{
$html .= '<li>'.$category->getName()."";
$subcats = $category->getChildren();
if($subcats != ''){
$html .= getTreeCategories($category->getId(), true);
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
$catlistHtml = getTreeCategories($rootCatId, false);
echo $catlistHtml;
?>
Thank you in advance.
you can use this to create category tree:
<?php
$rootCatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootCatId);
$output= '<ul>';
foreach($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$output .= '<li>' . '' . $category->getName() . "";
if ($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$array .= get_categories($children);
}
$output .= '</li>';
}
echo $output . '</ul>';
?>
For the effects of showing / hiding categories and subcategories, you can use plain css or jQuery / Prototype.

Speed up PHP recursive function

I am trying to create multi-level dynamic PHP menu by fetching values from database using recursion. But the page speed is getting slower and slower as the no. of records in database increases. Currently there are only around 15 records in DB.
Here is the code:
<div id="nav">
<?php
$menu_html='';
function render_menu($parent_id){
$con=mysqli_connect("localhost","root","","test");
global $menu_html;
$result=mysqli_query($con, "select * from menu where parent_id=$parent_id");
if(mysqli_num_rows($result)==0) return;
if($parent_id==0)
$menu_html .= "<ul>";
else
$menu_html .= "<ul>";
while($row=mysqli_fetch_array($result)){
$menu_html .= "<li>{$row['label']}";
render_menu($row['id']);
$menu_html .= "</li>";
}
$menu_html .= "</ul>";
return $menu_html;
}
echo render_menu(0);
?>
</ul>
</div>
This is how my db looks like:
Please help and let me know how I can optimize the page speed.
Thanks
You're doing multiple database queries which can be avoided.
Fetch everything in one go and loop over the rows to build the menu. You'll have everything you need to build a nested array using the parent IDs.
If it's still slow after that you can look into caching the database result, or even the entire rendered menu.
You probably are better off caching it in a file or to memory and specifically removing the cached item when you edit a menu item or add a new one to the database, so that the cache can be updated. Then you need not worry too much about how long you're caching for, you can just cache it indefinitely.
I would like to recommend you to learn nested set tree: Managing Hierarchical Data in MySQL, that enables only 1 select/query for building your menu, without recursive function calls.
Try this:
<div id="nav">
<?php
$menu_html='';
function render_menu()
{
global $menu_html;
$con=mysqli_connect("localhost","root","","test");
$result=mysqli_query($con, "SELECT * FROM menu ORDER BY parent_id ASC;");
$menu = array();
while($row=mysqli_fetch_array($result))
{
if($row['parent_id'] == 0)
{
$menu[$row['id']] = array
(
'label' => $row['label'],
'link' => $row['link'],
'children' => array()
);
}
else
{
$menu[$row['parent_id']]['children'][] = array
(
'label' => $row['label'],
'link' => $row['link']
);
}
}
$menu_html = '<ul>';
foreach($menu as $menu_item)
{
$menu_html .= '<li>';
$menu_html .= ''.$menu_item['label'].'';
if((bool)$menu_item['children'])
{
$menu_html .= '<ul>';
foreach($menu_item['children'] as $child)
{
$menu_html .= '<li>';
$menu_html .= ''.$child['label'].'';
$menu_html .= '</li>';
}
$menu_html .= '</ul>';
}
$menu_html .= '</li>';
}
$menu_html .= '</ul>';
return $menu_html;
}
echo render_menu();
?>
</div>
Thanks to all for your guidance and showing me the correct path to proceed.
I followed this link and its done.
http://wizardinternetsolutions.com/articles/web-programming/dynamic-multilevel-css-menu-php-mysql
Thanks

Make a php function recursive

I have written a function for a multilevel wordpress menu, but I'd like it to work for any number of levels, at the moment it is written to work for 3 levels.
//only gets the top level items
$top_level_pages = get_pages('parent=0&sort_column=menu_order&exclude=129,2,13');
foreach($top_level_pages as $page){
//print_r($top_level_pages);
$p_id = $page->ID;
// gets all pages and subpages in one array
$children = get_pages("child_of=$p_id&sort_column=menu_order");
$immediate_children = get_pages("child_of=$p_id&parent=$p_id&sort_column=menu_order");
//print_r($immediate_children);
if($children) {
print '<li class="page_item page-item-'.$page->ID.'"><span class="first-level">'.$page->post_title;
print '</span><ul>';
foreach($immediate_children as $child){
$c_id = $child->ID;
//gets a preformatted menu
$grandchildren = wp_list_pages('depth=1&echo=0&parent='.$c_id.'&sort_column=menu_order&title_li=');
if($grandchildren) {
print '<li class="page_item page-item-'.$child->ID.'"><span class="second-level">'.$child->post_title;
print '</span><ul>';
print $grandchildren;
print '</ul></li>';
}
else {
print '<li class="page_item page-item-'.$child->ID.'">'.$child->post_title.'</li>';
}
}
print '</ul></li>';
}
else {
print '<li class="page_item page-item-'.$page->ID.'">'.$page->post_title.'</li>';
}
}
All we need to do to make your function recursive is move most of the loop into a recursive function:
<?php
//only gets the top level items
$top_level_pages = get_pages('parent=0&sort_column=menu_order&exclude=129,2,13');
foreach($top_level_pages as $page) {
//print_r($top_level_pages);
$p_id = $page->ID;
recursiveFunction($p_id);
}
function recursiveFunction($p_id){
$children = get_pages("child_of=$p_id&sort_column=menu_order");
$immediate_children = get_pages("child_of=$p_id&parent=$p_id&sort_column=menu_order");
//print_r($immediate_children);
if($children) {
print '<li class="page_item page-item-'.$page->ID.'"><span class="first-level">'.$page->post_title;
print '</span><ul>';
foreach($immediate_children as $child) {
recursiveFunction($child->ID);
}
print '</ul></li>';
}
else {
print '<li class="page_item page-item-'.$page->ID.'">'.$page->post_title.'</li>';
}
}
?>
The hard part is making the "first-level", "second-level" stuff work. I'd just change it "level-1", "level-2", etc. And then you can just start with $x = 1; and pass $x+1 each time you call the function recursively.
Something like this should probably do the trik (written in the stackoverflow textbox and untested).
function listChildren($parentID, &$menu)
{
static $options = 'parent=0&sort_column=menu_order&exclude=129,2,13';
static $level = 1;
foreach(get_pages(sprintf('child_of=%d&%s', $parentID, $options ) as $page)
{
$menu .= sprintf('<li class="level-%d">%s',
$level
get_page_link($page->ID),
$page->post_title);
if ($page->hasChildrenOrWhateverWPUses())
{
$level ++;
$menu .= sprintf('<ul class="menu-level-%d">', $level);
listChildren($page->ID, $menu);
$menu .= '</ul>';
$level --;
}
$menu .="</li>"
}
}
used like:
$menu = '<ul class="menu-tree">';
listChildren($rootPageID, $menu);
$menu .= '</ul>';
echo $menu;

Drupal Adding Span inside A tags in Nice Menus

I am trying to add drop down menus to a drupal theme which uses text sliding door CSS rounding.
The current version uses a primary links injection of the span into the a tags, which works fine. But doesn't support drop down menus.
Working code:
<?php print theme('links', $primary_links, array('class' => 'links primary-links')) ?>
In the template with a template.php file addition:
<?php
// function for injecting spans inside anchors which we need for the theme's rounded corner background images
function strands_guybrush_links($links, $attributes = array('class' => 'links')) {
$output = '';
if (count($links) > 0) {
$output = '<ul'. drupal_attributes($attributes) .'>';
$num_links = count($links);
$i = 1;
foreach ($links as $key => $link) {
$class = $key;
// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ' first';
}
if ($i == $num_links) {
$class .= ' last';
}
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
$class .= ' active';
}
$output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
if (isset($link['href'])) {
$link['title'] = '<span class="link">' . check_plain($link['title']) . '</span>';
$link['html'] = TRUE;
// Pass in $link as $options, they share the same keys.
$output .= l($link['title'], $link['href'], $link);
}
else if (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = '';
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
}
$i++;
$output .= "</li>\n";
}
$output .= '</ul>';
}
return $output;
}
?>
So I have added the Nice Menu module which works well and allows the drop down menu functions for my navigation which is now addressed from the template using:
<?php print theme_nice_menu_primary_links() ?>
The issue is that the a tags need to have spans inside to allow for the selected state markup. I have tried every angle I could find to edit the drupal function menu_item_link which is used by nice menus to build the links.
E.g. I looked at the drupal forum for two days and no joy.
The lines in the module that build the links are:
function theme_nice_menu_build($menu) {
$output = '';
// Find the active trail and pull out the menus ids.
menu_set_active_menu_name('primary-links');
$trail = menu_get_active_trail('primary-links');
foreach ($trail as $item) {
$trail_ids[] = $item['mlid'];
}
foreach ($menu as $menu_item) {
$mlid = $menu_item['link']['mlid'];
// Check to see if it is a visible menu item.
if ($menu_item['link']['hidden'] == 0) {
// Build class name based on menu path
// e.g. to give each menu item individual style.
// Strip funny symbols.
$clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'), '', $menu_item['link']['href']);
// Convert slashes to dashes.
$clean_path = str_replace('/', '-', $clean_path);
$class = 'menu-path-'. $clean_path;
$class .= in_array($mlid, $trail_ids) ? ' active' : '';
// If it has children build a nice little tree under it.
if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
// Keep passing children into the function 'til we get them all.
$children = theme('nice_menu_build', $menu_item['below']);
// Set the class to parent only of children are displayed.
$class .= $children ? ' menuparent ' : '';
// Add an expanded class for items in the menu trail.
$output .= '<li id="menu-'. $mlid .'" class="'. $class .'">'. theme('menu_item_link', $menu_item['link']);
// Build the child UL only if children are displayed for the user.
if ($children) {
$output .= '<ul>';
$output .= $children;
$output .= "</ul>\n";
}
$output .= "</li>\n";
}
else {
$output .= '<li id="menu-'. $mlid .'" class="'. $class .'">'. theme('menu_item_link', $menu_item['link']) .'</li>'."\n";
}
}
}
return $output;
}
As you can see the $output uses menu_item_link to parse the array into links and to added the class of active to the selected navigation link.
The question is how do I add a span inside the a tags OR how do I wrap the a tags with a span that has the active class to style the sliding door links?
If you want to wrap the a tags with a span, you can overwrite the theme_nice_menu_build and add your span to the output. If you want to inside the a tag you need to overwrite the menu_item_link.
You can overwrite a theme funciton by creation a function call your_theme_name_function_name and Drupal will use that function to render the markup instead of the default one. That way you can alter the markup any way you want. This function should be in your theme's template.php file.
A good way to start is to copy the function you want to overwrite and just alter to your likings.
A lot has happened since Drupal 4.7, I don't hope you use that. It's quite easy to insert span tags:
function your_theme_name_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
$link['localized_options']['html'] = TRUE;
return l('<span>' . $link['title'] . '</span>', $link['href'], $link['localized_options']);
}
I tested this and it works just fine.

Categories