I am using a foreach loop to create a brands page for our Magento store. In the loop every 5 items are put into an un-ordered list. They are also split up by letters of the alphabet (A - D, E - H, I - L, M - P, Q - U, V - Z). I need to insert a header before each set of lists. For example:
<h2>A - D</h2>
<ul>
<li>Brand 1</li>
<li>Brand 2</li>
<li>Brand 3</li>
<li>Brand 4</li>
<li>Brand 5</li>
</ul>
This is how I am getting the brands and splitting them up:
<?php foreach($brands as $brand) : ?>
<?php $name = $brand->getName(); ?>
<?php if((substr($name, 0, 1) == 'A') || (substr($name, 0, 1) == 'B') ||
(substr($name, 0, 1) == 'C') || (substr($name, 0, 1) == 'D')) : ?>
<?php if($count % 5 == 0) : ?>
</ul><ul>
<?php endif; ?>
<li><a href="<?php echo $brand->getUrl(); ?>">
<?php
$id = $brand->getId();
$imageUrl = Mage::getModel('catalog/category')->load($id)->getThumbnailUrl();
?>
<img src="<?php echo $imageUrl; ?>" alt="<?php echo $name; ?>">
</a></li>
<?php $count++; ?>
<?php endif; ?>
<?php endforeach; ?>
I thought I could insert the header into the right place by counting how many brands there are for each set of 4 or 5 letters then doing the same as what I have done to put in the 's. But it doesn't work.
Is there any way I can maybe tell the header to insert only on the first run of the loop?
Try this
$ascci_val = ord( strtolower(sub_str($name, 0, 1)) );
if( ($ascci_val >= 97) && ($ascci_val <= 100) ){
// brand name stats with a letter between A-D / a-d
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.