How to limit the number of pagination links in Zend_Paginator - php

When using Zend_Paginator, I don't want it to show me all the pagination links. Here's how I am implementing it:
$adapter = new Zend_Paginator_Adapter_DbSelect($result);
$paginator = new Zend_Paginator($adapter);
$page=$this->_getParam('page',1);
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
Now it is showing me all the links. E.g. there are 100 records and 10 rows per page, so it will show me 1 to 10 links. How can I have 5 links, 1 to 5?
Like this:
"start" "previous" 1 2 3 4 5 "Next" "End"
EDITED
<!--Number page links-->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?= $this->url(array('page' => $page)); ?>">
<span class="fg-button ui-button ui-state-default"><?= $page; ?></span>
</a>
<?php else: ?>
<span class="fg-button ui-button ui-state-default ui-state-disabled" ><?= $page; ?></span>
<?php endif; ?>
<?php endforeach; ?>
How can I change it so that it shows me only 5 links?
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);
$page=$this->_getParam('page',1);
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber($page);
$paginator->setPageRange(5);
$this->view->paginator=$paginator;

$paginator->setPageRange(5); works for me. However you may need to apply this feature in your paginator control.
This is what the page link section of my control looks like.
<!--Number page links-->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current) : ?>
<a href="<?php echo $this->url(array_merge($params,
array('page' => $page))) ?>">
<?php echo $page ?></a> |
<?php else: ?>
<?php echo $page ?> |
<?php endif; endforeach; ?>

<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?php if ($page <6): ?>
<span class="fg-button ui-button ui-state-default"><?= $page; ?></span>
<?php elseif($page <6): ?>
<span class="fg-button ui-button ui-state-default ui-state-disabled" ><?= $page; ?></span>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>

this is a good one. By default, Zend Paginator sets the default to 10 as you've seen. But the way to override it is as follows:
$adapter = new Zend_Paginator_Adapter_DbSelect($result);
$paginator = new Zend_Paginator($adapter);
$page=$this->_getParam('page',1);
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber($page);
$paginator->setPageRange(5);
$this->view->paginator=$paginator;
You can find a bit more information in the configuration methods for Zend Paginator

Related

how to create a simple pagination in zend framework?

I have a project on which i need to implement pagination. I tried searching various time but couldn't find any satisfactory result. That's why I'm posting my question here --
This is Controller file IndexController.php
This is my indexAction, in which I'm trying to implement pagination -
public function indexAction() {
$this->_helper->layout->setLayout('layout');
//pagination
$page = $this->_request->getParam('page');
if (empty($page)) {
$page = 1;
}
$paginator = $this->_objCoupon->getOnePageOfOrderEntries($page);
$this->view->paginator = $paginator;
//pagination ends
$coupons = $this->_objCoupon->getAllcoupon();
$storage = new Zend_Auth_Storage_Session();
$dataUser = $storage->read();
$id = $dataUser->id;
//$id = $storage->id;
$arrCoupon = array();
if(count($coupons) > 0) {
$i= 0;
foreach($coupons as $couponArray) {
$arrCoupon[$i]["pid"] = $couponArray['id'];
$arrCoupon[$i]["title"] = $couponArray['partner_name'];
$arrCoupon[$i]["img"] = $couponArray['companylogo'];
$arrCoupon[$i]["value"] = $couponArray['coupon_value'];
$i++;
}
}
$this->view->arrCoupon = $arrCoupon;
$this->view->selectedcheckbox = $this->selectCheckboxes;
$this->view->id = $id;
$this->view->asd = 'all';
}
This is model file CouponModel.php
And this is my model function
public function getOnePageOfOrderEntries($page=1) {
//$paginator = $this->_coupon->getAllcoupon();
$query = "SELECT * FROM rechargecoupon WHERE online_offer = 'N' ORDER BY id ASC";
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbTableSelect($query)
);
$paginator->setItemCountPerPage(20);
$paginator->setCurrentPageNumber($page);
return $paginator;
}
And in view I'm just trying to print paginator variable value for now.
This is View file index.phtml
<?php if (count($this->paginator)): ?>
<?php print_r($this->paginator); die; ?>
When I'm executing this file, I'm getting "Class Zend_Paginator_Adapter_DbTableSelect not found error".
You should have these variables in your action:
$currentPage = value obtained from URL, has to be >= 1
$itemsPerPage = hard coded value, ideally taken from configuration
$itemsCount = SELECT COUNT(*) FROM ... WHERE ...
$pagesCount = ceil($itemsCount / $itemsPerPage)
$items = SELECT * FROM ... WHERE ... LIMIT ($currentPage - 1) * $itemsPerPage, $itemsPerPage, these are items to be displayed, for example articles
When you set these variables in your action, you should pass them to your view and generate pagination from them, that means:
If pages count is larger than 1:
display pagination
generate pagination from 1 to $pagesCount = 1, 2, 3 ... 10
each generated pagination number should have associated URL to your action
If pages count is 1:
do not display pagination
First of all.
Correct to use
$query = $this->select()
->from('rechargecoupon')
->where('online_offer = ?', 'N')
->order('id ASC')
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query));
$paginator->setItemCountPerPage('50');
$paginator->setCurrentPageNumber($page);
This is in the model.
In view
<?php if ($this->paginator->getTotalItemCount() !== 0): ?>
// echo elements you need
<?php endif; ?>
<?php echo $this->paginationControl( $this->paginator, 'Sliding', 'pagination.phtml' ); ?>
pagination.phtml displays pagination.
in pagination.phtml
<?php if ($this->pageCount > 1): ?>
<div>
<!-- Previous page -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
<span>< <?php echo 'previous'; ?></span>
</a>
<?php else: ?>
<span class="disabled">< <?php echo 'previous'; ?></span>
<?php endif; ?>
<!-- Page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url(array('page' => $page)); ?>">
<span><?php echo $page; ?></span>
</a>
<?php else: ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if(!in_array($this->pageCount, $this->pagesInRange)): ?>
<a href="<?php echo $this->url(array('page' => $this->pageCount)); ?>">
<span><?php echo $this->pageCount; ?></span>
</a>
<?php endif;?>
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->next)); ?>">
<span><?php echo 'next'; ?> ></span>
</a>
<?php else: ?>
<span class="disabled"><?php echo 'next'; ?> ></span>
<?php endif; ?>
</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.

Pagination in Zend not working

I have an array of items. Items are files paths. I have set up pagination but when I click on next it does not change. I want to display 1 pdf per page. Below is my code
class IndexController extends Zend_Controller_Action
{
public function init(){}
public function indexAction()
{
if($_SERVER['SERVER_NAME']=="portal-dev"){
$location = "/data/scan/invoice";
$listOfFiles = $this->readFiles($location);
$paginator = Zend_Paginator::factory($listOfFiles);
$paginator->setItemCountPerPage(1);
$this->view->paginator = $paginator;
}
}
public function readFiles($location){
$pattern="(\.pdf$)"; //valid image extensions
$thelist = array();
if ($handle = opendir($location)) {
while (false !== ($file = readdir($handle)))
{
if (eregi($pattern, $file)) //if this file is a valid image
{
$thelist[] = $file; //insert files into array
$max = count($thelist) - 1;
$max1 = count($thelist);
}
}
closedir($handle);
}
return $thelist;
}
}
index.phtml
<div id="main">
<?php if(!$this->paginator){
echo "No Files to process";
}else{
?>
<table>
<tr>
<td rowspan=2></td>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
<li><?php echo $item; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php echo $this->paginationControl($this->paginator,'Sliding','partial/my_pagination_control.phtml'); ?>
<?php }?>
</div>
pagination is found in directory partial
<?php if ($this->pageCount): ?>
<div
class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
Previous </a> <span class="bar"> | </span>
<?php else: ?>
<span class="disabled"> Previous</span> <span class="bar"> | </span>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url(array('page' => $page)); ?>"> <?php echo $page; ?>
</a> <span class="bar"> | </span>
<?php else: ?>
<?php echo $page; ?>
<span class="bar"> | </span>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->next)); ?>"> Next
</a>
<?php else: ?>
<span class="disabled">Next </span>
<?php endif; ?>
</div>
<?php endif; ?>
Try after setting the current page number in indexAction
$paginator->setCurrentPageNumber($this->_getParam('page'));
Like this
$paginator = Zend_Paginator::factory($listOfFiles);
$paginator->setItemCountPerPage(1);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$this->view->paginator = $paginator;

Magento - check if cms page

i want to check via php if a page is a cms_page in Magento. I need diffrent breadcrumbs for cms pages, so im trying to this with a condition, but i have no idea how to or where to look at.
Heres my breadcrumbs.phtml so far.
<?php if(this is a cms page): ?>
<p>some content</p>
<?php else: ?>
<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
<ul>
<?php $charsges = 0; ?>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<?php
$charsges = strlen($_crumbInfo['label']) + $charsges;
if($charsges > 40){
$chars = 18;
if(strlen($_crumbInfo['label']) > $chars){
$_crumbInfo['label'] = substr($_crumbInfo['label'], 0, $chars);
$_crumbInfo['label'] = $_crumbInfo['label'].'..';
}
}
?>
<li class="<?php echo $_crumbName ?>">
<?php if($_crumbInfo['link']): ?>
<?php echo $this->htmlEscape($_crumbInfo['label']) ?>
<?php elseif($_crumbInfo['last']): ?>
<strong><?php echo $this->htmlEscape($_crumbInfo['label']) ?></strong>
<?php else: ?>
<?php echo $this->htmlEscape($_crumbInfo['label']) ?>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span> > </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
greets rito
The following should give you what you want
//from a block or phtml script
$this->getRequest()->getModuleName()
When this returns the string 'cms', you're on a CMS page.
When Magento's frontend and admin routers can't find a match on your URL, the CMS router takes over. If the CMS router finds a match (based on the CMS pages you've setup), it hands off the request to the cms module and Mage_Cms_IndexController controller.

Magento: Display sub-category list

I'm building a Magento store and want to be able to display a list of categories and have each category link to its own page.
I have a 'Brands' category with an ID of 42 and I want to display a list of the sub-categories and ensure that each one links to the designated URL key in the CMS.
Has anyone had experience of doing this with Magento?
If you're comfortable editing your theme, this code snippet will bring you a list of all sub-categories of the current category (from the session, so this should work anywhere in your theme). I typically use this in app/design/frontend/default/theme_name/template/catalog/category/view.phtml
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<ul>
<?php foreach ($collection as $cat):?>
<?php if($_category->getIsActive()):?>
<?php
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getImageUrl();
?>
<li>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
<cite><?php echo $cat->getName();?></cite>
</a>
</li>
<?php endif?>
<?php endforeach;?>
</ul>
If You want to Display top level categories and subcategories U can do Like This..
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
For Displaying Top Level Categories and Current Categories SubCategories you can Do like ....
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This question requires a long answer. I will point you to the right places.
1) Best solution is to use the free extension. I haven't tried it, but it will suit the purpose. You will have to do some CSS to achieve the right look and feel.
http://www.magentocommerce.com/extension/1562/magento-easy-catalog-images
Demo: http://extension01.templates-master.com/gb/electronics.html
2) I do not trust in modules as it might become difficult to upgrade if the vendor decided to stop supporting it. I have used the information from the following forum thread to create a vew sites. Have a look... Might not be straight forward. You might have to make some copies of core files to the local directory.
http://www.magentocommerce.com/boards/viewthread/3770/P30/
Hopefully this will be of help to you :)
I made this little video on how I create custom category listing blocks with Magento.
I am sure there are better ways of achieving this or even something I could have done better, but it’s just my method. I only created this it in hopes that it helps explain somethings to some people out there.
Magento Custom Category Listing Block
Thanks!
after looking at all the solutions on the magento site, i found that wookiehangover's solution above worked and took about 8 seconds to implement.
creates a UL that you can style. thanks.
After creating static block you can get any list of the subcategories by this script:
$_helper = Mage::helper('catalog/category');
$_category = Mage::getModel('catalog/category')->load(5);
$_subcategories = $_category->getChildrenCategories();
if (count($_subcategories) <= 0) { return; }
$count = 0;
foreach($_subcategories as $_category) {
$category = Mage::getModel('catalog/category')->load($_category->getId());
$ret->{"object_".$count} ->url = $_helper->getCategoryUrl($_category);
$ret->{"object_".$count} ->name = $_category->getName();
$ret->{"object_".$count} ->id = $_category->getId();
$ret->{"object_".$count} ->image = $category->getImageUrl();
$count++;
}
return $ret;
}
$list = list_subcategories(5);
echo "<pre>"; print_r($list); echo "</pre>";
?>
How about listing only the categories belonging to the current item. Not all the categories on the page.
But in a tree like view.
CATEGORIE - sub cat 1
CATEGORIE 2 - sub cat 1 - sub sub cat 1
BR Cveto

Categories