alternate class to each LI foreach - php

I am trying to give an alternate class to each LI foreach.
i.e.
<li class="odd">
text
</li>
<li class="even">
text
</li>
<li class="odd">
text
</li>
<li class="even">
text
</li>
This is my code:
<ul>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
Text here
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Please can anyone help...
Thanks

try :
<?php $count = 0; // need to set first value
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="<?php echo (++$count % 2) ? "odd" : "even"; ?> type <?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
Text here
</li>
<?php endif; ?>
<?php endforeach; ?>
Link to pre/post increment docs if needed

I would do it that way .. it's key independent and you better see what the li-class will look like.
<ul>
<?php
foreach ($this->item->extra_fields as $key=>$extraField) {
if($extraField->value) {
$toggle = ($toggle=="odd"?"even":"odd");
echo '<li class="',$toggle,' type',ucfirst($extraField->type),' group', $extraField->group,'">';
echo 'Text here';
echo '</li>';
}
}
?>
</ul>

jQuery can easily do this if that's an option for you. It's :odd and :even selectors should do just what you're looking for without too much hassle.

I know this is old, but I humbly present a cleaner solution:
<ul>
<?php
$odd = true;
foreach ( $this->item->extra_fields as $key => $extraField ) {
// Output template.
$template = '<li class="%1$s">%2$s</li>';
// Create our classes.
$classes = [
( $odd ) ? 'odd' : 'even',
'type' . ucfirst($extraField->type),
'group' . $extraField->group
];
// Create our class string.
$class_string = implode( ' ', $classes );
// Print our content out.
printf( $template, $class_string, 'Text here' );
// Flip the classname for next time.
$odd = ! $odd;
}
?>
</ul>
It could of course be cleaner still, by using fewer variables. This is just my style.

Related

Add third level to php menu in WP?

I'm attempting to add a for loop to a php menu that will add a tertiary level to a currently existing WP menu. Unfortunately, the only editable amount of content is that posted below, so please bear with me on this one.
I'm just wondering, before I attempt to implement this code, if anyone can tell me if it will actually work, of if you assume it'll break everything.
What I've done is to take the same semantic forloop that was written for the secondary (or 'sub_nav') menu items, and repeat it in attempts to dynamically generate third level (or 'tert_nav') menu items.
<div class="main-menu-sub-navigation">
<?php
$main_nav_items = wp_get_nav_menu_items(2);
$sub_nav = array();
foreach ($main_nav_items as $item) {
if($item->menu_item_parent !== '0'){
$parent_id = $item->menu_item_parent;
$sub_nav[$parent_id][] = array('item' => $item);
}
}
$tert_nav = array();
foreach ($sub_nav_items as $item) {
if($item->menu_item_parent !== '0'){
$parent_id = $item->menu_item_parent;
$tert_nav[$parent_id][] = array('item' => $item);
}
}
?>
<div class="sub-nav-inner">
<?php foreach ($sub_nav as $key => $value) { ?>
<ul class="nav sub-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><a href="<?php print $item['item']->url; ?>"><?php print $item['item']->title; ?>
<?php foreach ($tert_nav as $key => $value) { ?>
<ul class="nav tert-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</a></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><a href="<?php print $item['item']->url; ?>"><?php print $item['item']->title; ?>
<?php foreach ($sub_nav as $key => $value) { ?>
<ul class="nav tert-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</a></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</div>
Any help would be so very greatly appreciated. Thank you!!

Php split value into different li classes

I use this code to display user entered data.
But all data is displayed on 1 single line separate by comma.
I want to display all separate data in an individual < li > class
How can I achieve that?
<?php if ( $this->showPros() ): ?>
<div class="pros">
<h4><?php echo $this->__('Pros:') ?></h4>
<ul class="pros-ul">
<li class="pros-li">
<?php $isFirst = true ?>
<?php foreach( $this->getProsCollection() as $pros )
{
$name = $this->__( $pros->getName() );
echo ( $isFirst ? $name : ( ', '.$name ) );
$isFirst = false;
} ?><br />
</li>
</ul>
</div>
<?php endif ?>
Just wrap the $name in li tags:
<?php if ( $this->showPros() ): ?>
<div class="pros">
<h4><?php echo $this->__('Pros:') ?></h4>
<ul class="pros-ul">
<?php foreach( $this->getProsCollection() as $pros )
{
$name = $this->__( $pros->getName() );
echo ('<li class="pros-li">'.$name.'</li>');
}
?>
</ul>
</div>
<?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.

Unable to apply foreach loop properly in PHP

I want two posts at each slide. but getting only one slide. I am new in programming, please help me.
$widget_id = $widget->id.'-'.uniqid();
$settings = $widget->settings;
$navigation = array();
$captions = array();
$i = 0;
?>
<div id="slideshow-<?php echo $widget_id; ?>" class="wk-slideshow wk-slideshow-revista-articles" data-widgetkit="slideshow" data-options='<?php echo json_encode($settings); ?>'>
<div>
<ul class="slides">
<?php foreach ($widget->items as $key => $item) : ?>
<?php
$navigation[] = '<li><span></span></li>';
$captions[] = '<li>'.(isset($item['caption']) ? $item['caption']:"").'</li>';
/* Lazy Loading */
$item["content"] = ($i==$settings['index']) ? $item["content"] : $this['image']->prepareLazyload($item["content"]);
?>
<li>
<article class="wk-content clearfix"><?php echo $item['content']; ?></article>
</li>
<?php $i=$i+1;?>
<?php endforeach; ?>
</ul>
<?php if ($settings['buttons']): ?><div class="next"></div><div class="prev"></div><?php endif; ?>
<?php echo ($settings['navigation'] && count($navigation)) ? '<ul class="nav">'.implode('', $navigation).'</ul>' : '';?>
<div class="caption"></div><ul class="captions"><?php echo implode('', $captions);?></ul>
</div>
</div>
http://i.stack.imgur.com/sy1ih.png
you're missing the { after the foreach and at the end of the loop.
<?php foreach ($widget->items as $key => $item) {
$navigation[] = '<li><span></span></li>';
$captions[] = '<li>'.(isset($item['caption']) ? $item['caption']:"").'</li>';
/* Lazy Loading */
$item["content"] = ($i==$settings['index']) ? $item["content"] : $this['image']->prepareLazyload($item["content"]);
?>
<li>
<article class="wk-content clearfix"><?php echo $item['content']; ?></article>
</li>
<?php
$i=$i+1;
}
?>
Your foreach syntax looks fine. That syntax is a lot easier for some people to read when embedded in HTML than the traditional braces.
Can I ask what the $this variable refers to? I don't see this instantiated anywhere in your code? Is it supposed to be $item instead?
$settings['index'] never changes within the loop, however $i does.
Change to: ($i<2)

Looping through HTML code in PHP/JavaScript

Instead of writing:
<ul class="tabs">
<li>1-50</li>
<li>51-100</li>
<li>101-150</li>
<li>151-200</li>
<li>201-250</li>
<li>251-300</li>
<li>300-350</li>
<li>351-400</li>
<li>401-500</li>
</ul>
until 950-1000 which will be tab 20 - is there a way to using a PHP/JavaScript for loop to create more compact code?
I think this should do it for you:
<ul class="tabs">
<?php
$end_at = 1000;
$group_by = 50;
for($i=0;$i<$end_at/$group_by;$i++) {
echo '<li>', $i * $group_by + 1, '-', ($i+1) * $group_by, "</li>\n";
}
?>
</ul>
Example output
Or this:
<ul class="tabs">
<?php for($i=1;$i<=1000;$i++): ?>
<?php if($i % 50 == 0): ?>
<li><?php echo $i-49 ?>-<?php echo $i; ?></li>
<?php endif; ?>
<?php endfor; ?>
</ul>

Categories