How to use joomla pagination of my custom foreach loop data - php

How to use joomla pagination in my custom foreach loop data.
//My code
<?php foreach ($items as $item) : ?>
<h2><?= $item->name ?></h2> // data
<?php endforeach; ?>
//This code show pagination but not working
$total = count($items);
jimport('joomla.html.pagination');
$pagination = new JPagination($total, 0 , 1);
echo $pagination->getPagesLinks();
echo $pagination->getPagesCounter();
Can you give a best solution?

I don't have any idea to use joomla pagination.
So I create custom pagination.
<?php //my code
$nItemsPerPage = 1;
$nOfPage=intval(count($items)/$nItemsPerPage)+1;
$page = isset($_GET['page'])?intval($_GET['page']-1):0;
foreach(array_slice($items, $nItemsPerPage*($page-1), $nItemsPerPage) as $item):
echo $item->title; //output
endforeach;
?>
//pagination
<ul>
<?php
for($i=1;$i<$nOfPage;$i++) { ?>
<li><a href='?page=<?php echo $i; ?>'><?php echo $i ?></a></li>
<?php } ?>
</ul>
~~~~~~~~~~~~~Thank You~~~~~~~~~~

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')

Wordpress Loop For Even/Odd Posts

I have two columns and I want one post type to get distributed to each column evenly. So it’s two side-by-side divs and I want:
Div1 = Post1, Post3, Post5
Div2 = Post2, Post4, Post6
So basically get odd/even posts. Not exactly sure how to do it.
<?php query_posts('post_type=post-type'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="column1">
<?php
//Get Odd Posts
?>
</div>
<div class="column2">
<?php
//Get Even Posts
?>
</div>
<?php endwhile; ?>
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>
You want to use the modulus operator something like this:
<?php
$i = 0;
for ($i = 0; $i <20; $i++){
$class = $i % 2 == 0 ? "even" : "odd";
echo "<div class='" . $class . "'>";
echo "</div>";
}
?>
To do what you want, first you have to store the results somewhere (as even/odd), then display them.
Though you should really target these posts with CSS, not PHP, as it's hackish at best.
<?php query_posts('post_type=post-type'); ?>
<?php if (have_posts()) : ?>
<?php
$i = 0;
while (have_posts())
{
$key = $i & 1 ? 'odd' : 'even';
$post[$key] = array(get_the_title() => get_the_content());
$i++;
}
?>
<div class="column1">
<?php foreach ($post['even'] as $title => $content) : ?>
<?php echo $title; ?>
<?php echo $content; ?>
<?php endforeach; ?>
</div>
<div class="column2">
<?php foreach ($post['odd'] as $title => $content) : ?>
<?php echo $title; ?>
<?php echo $content; ?>
<?php endforeach; ?>
</div>
<?php else : ?>
//Something that happens when a post isn’t found.
<?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.

OpenCart: Add categories to main menu?

I've just installed OpenCart for the first time, deleted all the dummy products and categories it comes with and added my own. However, now nothing appears in my menu - no product categories. I can't seem to fine where to add items to menus.
Can anyone point me in the right direction? Thanks
UPDATE:
THis is the php in my header tpl file:
?php if ($categories) { ?>
<div id="menu">
<ul>
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
I have not acutally changed any of it from the original
Create a new parent category and sub categories in Catalog->Category->insert then add the new product Catalog->product->insert. Product should be appear in frond end. for more details see this video tutorials : http://www.opencart.com/index.php?route=documentation/screencast

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)

Categories