在这里,有些时间我们需要自定义分页而不是 Magento 默认分页。
所以,在这里是代码段的代码示例将帮助您创建分页与在 Magento 的自定义模块上的自定义集合。
第 1 步: 首先将以下代码粘贴在你块类 (例如 testimonial.php)
<?php
private $_itemPerPage = 2;
private $_pageFrame = 8;
private $_curPage = 1;
public function getCollection($collection = 'null')
{
if($collection != 'null'){
$page = $this->getRequest()->getParam('p');
if($page) $this->_curPage = $page;
$collection->setCurPage($this->_curPage);
$collection->setPageSize($this->_itemPerPage);
return $collection;
}
}
public function getPagerHtml($collection = 'null')
{
$html = false;
if($collection == 'null') return;
if($collection->count() > $this->_itemPerPage)
{
$curPage = $this->getRequest()->getParam('p');
$pager = (int)($collection->count() / $this->_itemPerPage);
$count = ($collection->count() % $this->_itemPerPage == 0) ? $pager : $pager + 1 ;
$url = $this->getPagerUrl();
$start = 1;
$end = $this->_pageFrame;
$html .= '<ol>';
if(isset($curPage) && $curPage != 1){
$start = $curPage - 1;
$end = $start + $this->_pageFrame;
}else{
$end = $start + $this->_pageFrame;
}
if($end > $count){
$start = $count - ($this->_pageFrame-1);
}else{
$count = $end-1;
}
for($i = $start; $i<=$count; $i++)
{
if($i >= 1){
if($curPage){
$html .= ($curPage == $i) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
}else{
$html .= ($i == 1) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
}
}
}
$html .= '</ol>';
}
return $html;
}
public function getPagerUrl() // You need to change this function as per your url.
{
$cur_url = mage::helper('core/url')->getCurrentUrl();
$new_url = preg_replace('/\&p=.*/', '', $cur_url);
return $new_url;
}
?>
步骤 2: 现在你必须修改你的前端 phtml 页面。(例如 testimonial.phtml)
//pagination on top of contents
<?php if($this->getPagerHtml($this->getProductCollection())):?> // pass the default collection as parameter
<div class="pages">
<span><?php echo $this->__('Page : ');?></span>
<?php echo $this->getPagerHtml($this->getProductCollection());?>
</div>
<?php endif;?>
<?php
$_collections = $this->getProductCollection(); // Default Collection on which you'll implement pagination.
$_productCollection = $this->getCollection($_collections); // calling the function that have been created in block page.
foreach ($_productCollection as $_product):
------------------ Your code here ------------
endforeach;
?>
//pagination on bottom of contents
<?php if($this->getPagerHtml($this->getProductCollection())):?> // pass the default collection as parameter
<div class="pages">
<span><?php echo $this->__('Page : ');?></span>
<?php echo $this->getPagerHtml($this->getProductCollection());?>
</div>
<?php endif;?>
希望 !你会喜欢:-) !!!
(责任编辑:好模板) |