If count equals number, append class - php

So I have the variable $count that will give me a count. What would be the best approach to append a class name to the <div class="column"> section?
Here is sort of what I'm looking for:
If count === 1, render <div class="column">
If count === 2, render <div class="column is-half">
If count === 3, render <div class="column is-one-third">
If count === 4+, render <div class="column is-one-quarter">
Here is the code:
<?php $count = count($sub_items) ?>
<div class="column">
<h1 class="title is-6 is-mega-menu-title mb-2"><?= $sub_item->get_title(); ?></h1>
<?php if ($sub_child_items = $sub_item->get_sub_items()): ?>
<?php foreach ($sub_child_items as $sub_child_item): ?>
<a class="navbar-item" href="<?= $sub_child_item->get_url(); ?>">
<?= $sub_child_item->get_title(); ?>
</a>
<?php endforeach; ?>
<?php endif; ?>
</div>
All help would be appreciated!

I'll do something like this using switch and please note the last case default:
<?php
$count = count($sub_items);
$className = "column ";
switch($count) {
case 2:
$className .= "is-half";
break;
case 3:
$className .= "is-one-third";
break;
case 1:
default:
$className = ($count >= 4) ? "column is-one-quarter" : "column";
}
?>
<div class="<?php echo $className; ?>">
<h1 class="title is-6 is-mega-menu-title mb-2"><?= $sub_item->get_title(); ?></h1>
<?php if ($sub_child_items = $sub_item->get_sub_items()): ?>
<?php foreach ($sub_child_items as $sub_child_item): ?>
<a class="navbar-item" href="<?= $sub_child_item->get_url(); ?>">
<?= $sub_child_item->get_title(); ?>
</a>
<?php endforeach; ?>
<?php endif; ?>
</div>
Also, I would suggest to use <?php echo $varName; ?> instead of using <?= $varName; ?> for compatibility purposes.

Related

PHP remove a condition in a foreach

I'm trying to edit a WordPress plugin which sorts the streams from the platform.
I just want to show any stream from the ID.
This is the link to the page controller (bootstrap)
<?php
$streamHeading = get_option('pixiehuge-stream-heading', 'Our streams');
$streamsection = get_option('pixiehuge-stream-section-enable');
$streams = pixiehuge_streams();
global $sectionNum;
?>
<?php if($streamsection && !empty($streams)): ?>
<section id="streams"<?php echo (!empty($sectionNum) && $sectionNum == 1) ? ' class="firstWithBg"' : '' ?>>
<?php
$sList = [
'twitch' => pixiehuge_streams(false, 'twitch'),
'youtube' => pixiehuge_streams(false, 'youtube'),
'mixer' => pixiehuge_streams(false, 'mixer'),
];
$active = false;
?>
<div class="container">
<div class="section-header">
<article class="topbar">
<h3>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="7px" height="8px"><path fill-rule="evenodd" fill="rgb(57, 191, 253)" d="M-0.000,0.435 C-0.000,0.805 -0.000,7.292 -0.000,7.546 C-0.000,7.877 0.338,8.123 0.672,7.930 C0.940,7.775 6.293,4.649 6.750,4.381 C7.050,4.205 7.045,3.786 6.750,3.611 C6.421,3.415 1.048,0.272 0.658,0.054 C0.373,-0.106 -0.000,0.071 -0.000,0.435 Z"></path></svg>
<?php echo esc_attr($streamHeading); ?>
</h3>
</article>
<!-- /TOP-BAR -->
<article class="bottombar">
<ul>
<?php foreach($sList as $id => $items): ?>
<?php if(!empty($items)):?>
<li<?php echo !$active ? ' class="active"' : '' ?>>
<?php echo esc_attr($id) ?>
</li>
<?php
if(!$active) {
$active = $id;
}
endif; // If not empty ?>
<?php endforeach; // Get categories ?>
</ul>
</article>
<!-- /BOTTOM-BAR -->
</div>
<!-- /SECTION-HEADER -->
<div class="tab-content content">
<?php foreach($sList as $id => $items): ?>
<?php if(!empty($items)): $i = 1; ?>
<div id="<?php echo esc_attr($id) ?>" class="list<?php echo ($active == $id) ? ' active' : '' ?>">
<?php foreach($items as $item): ?>
<?php if($i == 1): ?>
<div class="left">
<article class="streamBox large" style="background-image: url('<?php echo esc_url($item['thumbnail']) ?>');">
<a href="<?php echo esc_url(get_home_url(null, 'stream/' . $item['slug'])) ?>" class="playBtn">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10px" height="12px"><path fill-rule="evenodd" fill="rgb(255, 255, 255)" d="M10.000,6.000 C10.000,6.412 9.789,6.773 9.473,6.968 L9.474,6.968 L1.629,11.835 C1.626,11.837 1.623,11.839 1.620,11.841 L1.616,11.843 L1.616,11.843 C1.457,11.942 1.270,12.000 1.071,12.000 C0.480,12.000 -0.000,11.496 -0.000,10.875 L-0.000,10.875 L-0.000,1.124 L-0.000,1.124 C-0.000,0.503 0.480,-0.000 1.071,-0.000 C1.270,-0.000 1.457,0.057 1.616,0.156 L1.616,0.156 L1.620,0.159 C1.623,0.160 1.626,0.162 1.629,0.164 L9.474,5.031 L9.473,5.031 C9.789,5.227 10.000,5.587 10.000,6.000 Z"></path></svg>
</a>
<div class="details">
<span class="stream <?php echo esc_attr($id) ?>"><?php echo esc_attr($id) ?></span>
<?php echo esc_attr($item['title']) ?>
<h6><?php echo esc_attr($item['author']) ?></h6>
</div>
</article>
</div>
<!-- /LEFT -->
<?php else: // Large thumbnail ?>
<?php if($i == 2): ?>
<div class="right">
<?php endif; ?>
<?php if($i == 4): ?>
<div class="small">
<?php endif; ?>
<article class="streamBox" style="background-image: url('<?php echo esc_url($item['thumbnail']) ?>');">
<div class="details on-hover">
<?php echo esc_attr($item['title']) ?>
<a href="<?php echo esc_url(get_home_url(null, 'stream/' . $item['slug'])) ?>" class="cta-btn">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="9px" height="8px"><path fill-rule="evenodd" fill="rgb(255, 255, 255)" d="M4.688,0.182 C4.437,0.442 4.437,0.865 4.688,1.126 L6.805,3.326 L0.643,3.326 C0.288,3.326 -0.000,3.625 -0.000,3.993 C-0.000,4.362 0.288,4.661 0.643,4.661 L6.805,4.661 L4.688,6.861 C4.437,7.122 4.437,7.544 4.688,7.805 C4.939,8.066 5.346,8.066 5.597,7.805 L8.811,4.466 C8.928,4.345 9.000,4.178 9.000,3.993 C9.000,3.809 8.928,3.642 8.811,3.521 L5.597,0.182 C5.346,-0.079 4.939,-0.079 4.688,0.182 Z"></path>
</svg>
</a>
</div>
</article>
<?php if($i == count($items) || $i == 3): ?>
</div>
<?php endif; ?>
<?php endif; ?>
<?php $i++; endforeach; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<!-- /CONTENT -->
</div>
<!-- /CONTAINER -->
</section>
<?php endif; ?>
I don't like PHP Object so I'm quite lost. I know I have to remove $sList Lines 12 > 16 since I don't want to sort it from Youtube...
But then I don't know how to edit the foreach line 51 foreach($sList as $id => $items) to do a foreach elements ID with no conditions anymore. Just a foreach of any elements in the TABLE.
I tried to do that from scratch:
line 51
<div class="tab-content content">
<?php foreach($stream as $streams){ ?>
<p><?php echo $stream['title']; ?></p>
<?php } ?>
</div>
But it didn't work... And I can't tell you the error
EDIT
This is working but I can't order it by ID DESC and limit it to 4 results...
<div class="tab-content content">
<?php foreach($streams as $key => $stream): ?>
<p><?php echo $stream['title']; ?></p>
<?php endforeach; ?>
</div>
Thank you for your help
This is working "fine"
<div class="tab-content content">
<?php foreach($streams as $key => $stream): ?>
<p><?php echo $stream['title']; ?></p>
<?php endforeach; ?>
</div>

ACF / PHP Repeater count

I have a repeater using Advanced Custom fields, which when a count reaches a total (of 3) based on an iterative count, I would like to output a div, then reset the counter.
Here's the code, at the moment it's not outputting as it should and I don't know enough about math to get it to work.
Appreciate your assistance :-)
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=1;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $count; ?>
<hr/>
</div>
<?php } if(get_sub_field('column_width') == "Two columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
Count is <?php echo $counter; ?>
<hr/>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
Think I'd got my logic mixed up a bit — I wasn't using the else clauses correctly. Here's the final working code in case anyone stumbles across this in the future:
<?php if(have_rows('flexible_row')) : ?>
<div class="row">
<?php if(empty($count)){ $count=0;} while(have_rows('flexible_row')) : the_row(); ?>
<?php if(get_sub_field('column_width') == "One Column") { $count = $count+1; ?>
<div class="col-sm-4">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } elseif(get_sub_field('column_width') == "Two Columns") { $count = $count+2; ?>
<div class="col-sm-8">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } else { $count = $count+3; ?>
<div class="col-sm-12">
<?php the_sub_field('title');?>
<?php the_sub_field('content');?>
</div>
<?php } if ($count == 3) { ?></div><div class="row"><?php $count = 0; } ?>
<?php endwhile; ?>
</div>
You could of course use switch statements too..

Display nav list of content is available with php

I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.

Magento Grouped Product Not showing products / prices

When building a new grouped product using already created and in stock simple products the finished grouped product not show the table listing the product titles and prices on the right side column when using my custom template.
When the custom template is turned off and the default Magento template is in use the template displays the titles and prices correctly.
There is an issue with the grouped product template code that is preventing this portion of information from being displayed but the code of /app/design/frontend/mytemplate/default/template/catalog/product/view/type/grouped.phtml is the same as the default template of Magento.
Any one know how to resolve this?
It's hard to tell, but perhaps the template file isn't used. You could check if the layout file in your template has a block that refers to grouped.phtml in the PRODUCT_TYPE_grouped node. Also check if the view.phtml makes a call like
<?php echo $this->getChildHtml('product_type_data') ?>
to refer to the block.
Compare your template files to:
app\design\frontend\base\default\template\catalog\product\view.phtml
app\design\frontend\base\default\layout\catalog.xml
to see what I mean.
<form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<?php echo $this->getBlockHtml('formkey'); ?>
<div class="no-display">
<input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
<input type="hidden" name="related_product" id="related-products-field" value="" />
</div>
<?php
//Product collaterals
$section = array();
//Related products. $section['related'] is set only if related products (or replacement) exist
$replaceRelated = $theme->getCfg('product_page/replace_related');
if ($replaceRelated == 1) //don't replace with static block
{
if ($tmpHtml = trim($this->getChildHtml('catalog.product.related')))
$section['related'] = $tmpHtml;
//$section['related'] = trim($this->getChildHtml('catalog.product.related'));
}
elseif ($replaceRelated == 2) //if related is empty, replace with static block
{
if ($tmpHtml = trim($this->getChildHtml('catalog.product.related')))
$section['related'] = $tmpHtml;
else //related empty
if ($tmpHtml = $this->getChildHtml('block_product_replace_related'))
$section['related'] = '<div class="block_product_replace_related">'. $tmpHtml .'</div>';
}
elseif ($replaceRelated == 3) //replace with static block
{
if ($tmpHtml = $this->getChildHtml('block_product_replace_related'))
$section['related'] = '<div class="block_product_replace_related">'. $tmpHtml .'</div>';
}
//Up-sell products. $section['upsell'] is set only if up-sell products (or replacement) exist
$replaceUpsell = $theme->getCfg('product_page/replace_upsell');
if ($replaceUpsell == 1) //don't replace with static block
{
if ($tmpHtml = trim($this->getChildHtml('upsell_products')))
$section['upsell'] = $tmpHtml;
//$section['upsell'] = $this->getChildHtml('upsell_products');
}
elseif ($replaceUpsell == 2) //if upsell is empty, replace with static block
{
if ($tmpHtml = trim($this->getChildHtml('upsell_products')))
$section['upsell'] = $tmpHtml;
else //upsell empty
if ($tmpHtml = $this->getChildHtml('block_product_replace_upsell'))
$section['upsell'] = '<div class="block_product_replace_upsell">'. $tmpHtml .'</div>';
}
elseif ($replaceUpsell == 3) //replace with static block
{
if ($tmpHtml = $this->getChildHtml('block_product_replace_upsell'))
$section['upsell'] = '<div class="block_product_replace_upsell">'. $tmpHtml .'</div>';
}
//Assign related products to selected position
if (isset($section['related']))
{
$relatedPosition = $theme->getCfg('product_page/related_position');
//TODO:remove: $relatedPosition = 11;
switch ($relatedPosition)
{
case 10:
$p['secondaryCol'][0] = $section['related'];
break;
case 11:
$p['secondaryCol'][1] = $section['related'];
break;
case 20:
$p['collatSecondaryCol'] = $section['related'];
break;
}
}
//Width (in grid units) of product page sections
$imgColUnits = $theme->getCfg('product_page/image_column');
$primaryColUnits = $theme->getCfg('product_page/primary_column');
$secondaryColUnits = $theme->getCfg('product_page/secondary_column');
$container2ColUnits = $imgColUnits + $primaryColUnits;
$collatPrimaryColUnits = $imgColUnits + $primaryColUnits;
$collatSecondaryColUnits = $secondaryColUnits;
//If no secondary column
if (empty($secondaryColUnits))
{
$primaryColUnits += 12 - ($imgColUnits + $primaryColUnits);
$container2ColUnits = $imgColUnits + $primaryColUnits;
$collatPrimaryColUnits = 9;
$collatSecondaryColUnits = 3;
}
//If no related products, stretch collateral primary column
if (!isset($section['related']))
{
$container2ColUnits = 12;
$collatPrimaryColUnits = 12;
$collatSecondaryColUnits = 0;
}
elseif (!isset($p['secondaryCol']))
{
$container2ColUnits = 12;
}
//Grid classes
$imgColGridClass = 'grid12-' . $imgColUnits;
$primaryColGridClass = 'grid12-' . $primaryColUnits;
$secondaryColGridClass = 'grid12-' . $secondaryColUnits;
$container2ColGridClass = 'grid12-' . $container2ColUnits;
$collatPrimaryColGridClass = 'grid12-' . $collatPrimaryColUnits;
$collatSecondaryColGridClass = 'grid12-' . $collatSecondaryColUnits;
if (empty($secondaryColUnits))
{
$secondaryColGridClass = '';
}
?>
<div class="product-img-column <?php echo $imgColGridClass; ?>">
<?php echo $this->getChildHtml('media') ?>
<?php //Product labels
echo $this->helper('ultimo/labels')->getLabels($_product);
?>
</div>
<div class="product-shop <?php echo $primaryColGridClass; ?>">
<div class="product-name">
<h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
</div>
<?php echo $this->getReviewsSummaryHtml($_product, false, true); //reviews ?>
<?php if ($_product->getShortDescription()): ?>
<div class="short-description">
<div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
</div>
<?php endif;?>
<?php if ($theme->getCfg('product_page/sku')): ?>
<div class="sku"><span><?php echo $this->__('SKU'); ?>: </span><?php echo $_product->getSku(); ?></div>
<?php endif; ?>
<?php if ($extrahint_html = $this->getChildHtml('extrahint')): //qty increments ?>
<div class="extrahint-wrapper"><?php echo $extrahint_html; ?></div>
<?php endif; ?>
<div class="product-type-data<?php echo $productTypeClasses; ?>"><?php echo $this->getChildHtml('product_type_data'); ?></div>
<?php echo $this->getTierPriceHtml(); //tier prices ?>
<?php if (!$this->hasOptions()): //add to cart when no options ?>
<?php if($_product->isSaleable()): ?>
<div class="add-to-box">
<?php echo $this->getChildHtml('addtocart') ?>
</div>
<?php endif; ?>
<?php echo $this->getChildHtml('extra_buttons') ?>
<?php endif; ?>
<?php if ($_product->isSaleable() && $this->hasOptions()): ?>
<?php if ($container1_html = $this->getChildChildHtml('container1', '', true, true)): ?>
<div class="container1-wrapper"><?php echo $container1_html; ?></div>
<?php endif; ?>
<?php endif;?>
<?php if ($addtoLinksHtml = $this->getChildHtml('addto')): //compare, wishlist, to friend ?>
<div class="action-box clearer">
<?php echo $addtoLinksHtml; ?>
</div>
<?php endif; ?>
<?php echo $this->getChildHtml('alert_urls'); //alerts ?>
<?php echo $this->getChildHtml('other'); ?>
<?php if ($tmpHtml = $this->getChildHtml('block_product_primary_bottom')): ?>
<div class="feature-wrapper top-border block_product_primary_bottom"><?php echo $tmpHtml; ?></div>
<?php endif; ?>
<?php if ($tmpHtml = $this->getChildHtml('product_primary_bottom_placeholder')): //Placeholder for extensions ?>
<div class="feature-wrapper top-border"><?php echo $tmpHtml; ?></div>
<?php endif; ?>
</div> <!-- end: product-shop -->
<?php if ($secondaryColUnits): //(!empty($secondaryColUnits)): ?>
<div class="product-secondary-column <?php echo $secondaryColGridClass; ?> custom-sidebar-right">
<div class="inner">
<?php //Placeholder for extensions ?>
<?php if ($tmpHtml = $this->getChildHtml('product_secondary_top_placeholder')): //Placeholder for extensions ?>
<div class="margin-bottom"><?php echo $tmpHtml; ?></div>
<?php endif; ?>
<?php if (isset($p['secondaryCol'][0])): ?>
<?php echo $p['secondaryCol'][0]; ?>
<?php endif; ?>
<?php if ($tmpHtml = $this->getChildHtml('block_product_secondary_bottom')): ?>
<div class="feature-wrapper top-border block_product_secondary_bottom"><?php echo $tmpHtml; ?></div>
<?php endif; ?>
<?php if (isset($p['secondaryCol'][1])): ?>
<div class="margin-top"><?php echo $p['secondaryCol'][1]; ?></div>
<?php endif; ?>
<?php /*?>
<?php if (isset($section['related'])): ?>
<div class="margin-top"><?php echo $section['related']; ?></div>
<?php endif; ?>
<?php */?>
</div>
</div> <!-- end: product-secondary-column -->
<?php endif; ?>
<?php if ($_product->isSaleable() && $this->hasOptions()): ?>
<?php if ($container2_html = $this->getChildChildHtml('container2', '', true, true)): ?>
<div class="box-additional <?php echo $container2ColGridClass; ?>">
<div class="container2-wrapper"><?php echo $container2_html; ?></div>
</div>
<?php endif; ?>
<?php endif; ?>
</form>

Get Magento Categories with Especific Sort of Admin Panel

I wrote the code below to put together a customized menu of categories. Everything works fine, but would like the order of the categories were the same order as defined in the administrator panel where there drap and drop functionality.
<?php
$subCats = Mage::getModel('catalog/category')->load(76)->getChildren();
$dispositosCatIds = explode(',',$subCats);
?>
<ul class="menu">
<?php $controleNum = 0; ?>
<?php foreach($dispositosCatIds as $dispositoCatId): ?>
<?php $aparelhoCat = Mage::getModel('catalog/category')->load($dispositoCatId); ?>
<?php if($aparelhoCat->getIsActive()): ?>
<li class="<?php print $controleNum ? '' : 'submenu first'; ?>"><a class="drop" href="<?php echo $aparelhoCat->getUrl(); ?>"> <span><?php echo $aparelhoCat->getName(); ?></span></a> <!--Begin 6 column Item -->
<div class="dropdown_6columns">
<div class="inner"><span class="title"><?php echo $aparelhoCat->getName(); ?></span>
<div class="col_2">
<div class="col_2 firstcolumn"><img src="<?php echo $aparelhoCat->getImageUrl(); ?>" alt="<?php echo $aparelhoCat->getName(); ?>" /></div>
</div>
<div class="col_4" style="margin-bottom: 20px;">
<?php echo $aparelhoCat->getDescription(); ?>
</div>
<div class="col_2 categorias-super"><span class="title_col">Produtos para <?php echo $aparelhoCat->getName(); ?></span>
<?php $subSubCats = $aparelhoCat->getChildrenCategories();?>
<?php if (count($subSubCats) > 0): ?>
<?php //$controleNumLI = 0; ?>
<ul style="list-style: none; float: none !important;">
<?php foreach($subSubCats as $_subcategory): //Rodando entre as categorias de Um dispositivo ?>
<?php if($_subcategory->getIsActive()): ?>
<li class="level1 <?php //print $controleNumLI ? '' : 'first'; ?>"> <?php echo $_subcategory->getName(); ?></li>
<?php //$controleNumLI += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
</li>
<?php $controleNum += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I tried to use other modes (based here) can do this, but I could not. The problem that the function returns getChildren() is a string with IDs in ascending order.
Some Ideas?
This is the code I use to display category in a dropdown box in the order of the admin... the key is setOrder('path','ASC')
$categories = array();
$_categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('path','ASC')
->addAttributeToSelect('name')->load();
foreach($_categories as $cat){
$level = $cat->getLevel() - 1;
$pad = str_repeat("----", ($level > 0) ? $level : 0);
$categories[] = array('value' => $cat->getEntityId(), 'label' => $pad . ' ' . $cat->getName());
}
print_r($categories);
You could do something like this: create array tree from array list
I got it:
$dispositovosCategoryId = 76;
$dispositovosCategoryIds = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$dispositovosCategoryId))
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('position','ASC')
->addAttributeToSelect('name','url','image')
->load();

Categories