Adding a class to the last row in a foreach loop - php

HI all,
This code works perfectly printing all 5 results with borders on the bottom of each list item through CSS.
However the last item id like there to be no border. How could i add a class to the list item on the last iteration?
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php foreach ($last_articles as $article): ?>
<li>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php endforeach; ?>
</ul>
Thanks for your help.

<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
BTW, this adds the class "last-item" to the last item processed.
And yes, you should restructure your code so its readable.

You can use the :last-child CSS modifier:
ul.latest-news:last-child { border-bottom: none; }
But it's not so widely supported (yet), so adding an explicit .last class as you're about to do is probably best.

Related

Magento get CMS pages based on layout

So I'm building a custom breadcrumb nav to list all fundraising options. The pages are using a unique layout called "fundraising_page." Is there a way to grab the pages only if they have the "fundraising_page" layout? So far I have this, which is grabbing every page regardless of the template it is using.
So what I need is only to list the pages that are using the "fundraising_page" template.
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()->where('is_active = 1'); ?>
<ul>
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php if($PageData['identifier']!='no-route'){ ?>
<li>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
Here's some well formatted code and using proper methods.
<?php
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1)
->addFieldToFilter('root_template', 'fundraising_page');
?>
<?php foreach ($collection as $page): ?>
<?php if ($page->getIdentifier() != 'no-route'): ?>
<li>
<?php echo $page->getTitle() ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
Instead of if($PageData['identifier']!='no-route') try
if($PageData['root_template']=='fundraising_page')

How to show 3 <li> in one row

I need to show 3 images in each row in <ul> tag. I have multiple <ul> tags and in each <ul> tag, I am showing 6 images. Now i have to show 3 <li> in first row and next 3 <li> in second row. Then same for next <ul>. I need to break row after 3 <li>. This is my code :
<div class="sets">
<?php foreach ($sets as $set => $items) : ?>
<ul class="set test-set">
<?php $i=0; foreach ($items as $thumb) : ?>
<?php
/* Prepare Image */
$content = '<img src="'.$thumb['cache_url'].'" width="'.$thumb['width'].'" height="'.$thumb['height'].'" alt="'.$thumb['filename'].'" />';
?>
<?php if($i === 0):
echo '<li><div>'; ?>
<?php endif; ?>
<?php echo $content; ?>
<?php if($i === 2): $i = 0; ?>
<?php else: $i++; endif; ?>
</div></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</div>
http://jsfiddle.net/z4Q48/
li{
float: left;
}
li:nth-child(3n+4){
clear: both;
}
see http://css-tricks.com/how-nth-child-works/

Magento PHP if statement "load both blocks" , else load only one?

maybe it is something simple but i am not really good with php
With this code i am able to load the static blocks styled in top of category in Magento and the subcategories in the bottom when the top category has child
What i am trying to do is that when there is no child category it should only load the block
otherwise because of styling it will hold the place that the subcat had position.
Please have a look on my code and maybe you understand:
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<div class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></div>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
$_catHtml = $this->getChildHtml('catalog.inline.subcat');
?>
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
// load the blocks styled with ul and il backgrounds
<ul class="inline-categories">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
<?php foreach ($_categories as $_category): ?>
<li<?php if($_i == 1):?> class="first"<?php endif ?>><a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
<?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)</li>
<?php endforeach ?>
<?php endif; ?>
</ul>
// here i need something like an "else" because the if($count) allready understand that this is not on count so should only load this that is without style //
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
//end of this else thing, now should only load the cms/block alone without anything / end of page we are done...
You can use PHP's if/else statements like this:
<?php if ($_count): ?>
stuff if $_count is non-zero
<?php else: ?>
other stuff if $_count is zero
<?php endif; ?>

How to Separate PHP Generated List Item - Magento

I have some php generated magento top-links with a screenshot below:
My goal is to separate the "Log In" link and have it floated to the left of the same row.
I am hoping for an efficient way to select the last generated list item element and apply some CSS to it.
The code is below:
<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
<?php endif;?>
<?php if (! $_link->getIsLast()):?>|<?php endif;?>
<?php endforeach; ?>
</ul>
Any ideas would be greatly appreciated!
CSS offers a way to style the last-child of a collection, no tinkering with PHP required.
http://tinker.io/926d2
ul.links.pull-right :last-child {
margin-left: 2em;
}
I answered a similar question not long ago. This adds the class "last-item" to the last item processed.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
Evening All,
Id try and limit the amount of business logic you are adding to your templates. As what you are looking to achieve is custom to this instance of magento I would create a very basic module. I would then look to either implement a new block or just a helper function that will return the data you want.
If your working on the block function ensure your class extends the Magento navigation class. ( Sorry I have not checked what this is ) Then create the action: E.g.
public function getNavigation()
{
$links = $this->getLinks();
$linkArray = array();
$linkCount = count($links);
$i;
foreach($links as $link) {
if($i == $linkCount) {
$last = true;
} else {
$last = false;
}
$linkArray[] = 'link' => $link->getLink()->toHtml(),
'isLast' => $last
$i++;
}
return $linkArray();
}
Your block would then have minimal logic applied. Mainly just iterating through the result set.
Hope that makes sense, If not let me know and I will get you what you require.

Remove "," at end of foreach loop

I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>

Categories