how to create a simple pagination in zend framework? - php

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; ?>

Related

"Read More" button stopped working in PHP, MySQL CMS

I want to display little amount of text when users come to my blog that's why I've made a "Read More" button to display few texts by cutting off the length of the text and the button was working fine until today when I add a new post via TinyMCE editor I saw that "Read More" button stopped working. While inspecting the error when I disable the function then the "Read More" button comes alive and the link to post is working fine but I'm unable to cut off the length please help me resolve this problem.
I'm using TinyMCE as editor for adding new posts.
This is index.php file.
<?php include("includes/db.php"); ?>
<?php include("includes/header.php"); ?>
<!-- Navigation -->
<?php include("includes/navigation.php"); ?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-8">
<?php
$per_page = 3;
if(isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page = "";
}
if($page =="" || $page == 1) {
$page_1 = 0;
}
else {
$page_1 = ($page * $per_page) - $per_page;
}
$post_query_count = "SELECT * FROM posts";
$find_count = mysqli_query($connection, $post_query_count);
$count = mysqli_num_rows($find_count);
$count = ceil($count / $per_page);
$query = "SELECT * FROM posts LIMIT $page_1, $per_page";
$select_all_posts_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_all_posts_query)) {
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$post_user = $row['post_user'];
$post_date = $row['post_date'];
$post_image = $row['post_image'];
$post_content = $row['post_content'];
$post_status = $row['post_status'];
if($post_status == 'published') {
?>
<!-- First Blog Post -->
<h2>
<?php echo $post_title; ?>
</h2>
<p class="lead">
by <?php echo $post_user; ?>
</p>
<p><span class="glyphicon glyphicon-time"></span> <?php echo $post_date; ?></p>
<hr>
<a href="post.php?p_id=<?php echo $post_id; ?>">
<img class="img-responsive" src="images/<?php echo $post_image; ?>" alt="">
</a>
<hr>
<?php echo string_length_cutoff($post_content, 320, '........'); ?>
<a class="btn btn-primary" href="post.php?p_id=<?php echo $post_id; ?>">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
<?php
} }
?>
</div>
<!-- Blog Sidebar Widgets Column -->
<?php include("includes/sidebar.php"); ?>
</div>
<!-- /.row -->
<hr>
<ul class="pager">
<?php
for ($i = 1; $i <= $count; $i++) {
if($i == $page) {
echo "<li><a class='active_link' href='index.php?page={$i}'>{$i}</a></li>";
}
else {
echo "<li><a href='index.php?page={$i}'>{$i}</a></li>";
}
}
?>
</ul>
<?php include("includes/footer.php"); ?>
This function below is stored in functions.php file
function string_length_cutoff($post_content, $limit, $subtext = '...') {
global $connection;
$query = "SELECT * FROM posts";
$select_all_posts_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_all_posts_query)) {
$post_content = $row['post_content'];
}
return (strlen($post_content) > $limit) ? substr($post_content, 0, ($limit-strlen('subtext'))).$subtext : $post_content;
}
Some Screenshots of the actual problem
Read More Button not working
Inspecting read more
read more inspection
I don't think database query is needed. Removed it.
Change your function to:
function string_length_cutoff($post_content, $limit, $subtext = '...') {
$post_content = strip_tags($post_content, '<img>');
return ((strlen($post_content) > $limit) ? substr($post_content, 0, $limit).$subtext : $post_content);
}

Fishpig get title, post content, post thumbnail and permalink

I am trying to get my code to return the last 3 posts from wordpress in Magento using the Fishpig extension. This is the code I have so far, but it appears to returning posts more than once. I also need it to just return posts, as it also returns pages at the moment.
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID ORDER BY p.post_date";
$results = $readConnection->fetchAll($query);
?>
<?php
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='/news/wp-content/uploads/'.($results1[0]['meta_value']);
?>
<div class="blog-post-image">
<img src="<?php echo $url; ?>">
</div>
<div class="blog-post-content">
<?php ?>
<h3> <?php echo $row['post_title'];?></h3>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
More Info
</div>
<?php endif; ?>
<?php
if($counter == 4)
{
break;
}
$counter++;
}
?>
To display 3 posts and display the post title, URL, image and post content (or excerpt), you would need the following code:
<?php $posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->addPostTypeFilter('post')
->setPageSize(3)
->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<h2>
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
</h2>
<?php if ($image = $post->getFeaturedImage()): ?>
<img src="<?php echo $image->getAvailableImage() ?>" alt="" />
<?php endif; ?>
<?php echo $post->getPostContent() ?>
<?php
/**
* You could also use:
* echo $post->getPostExcerpt(20)
* This would display the first 20 words of the post content
* or the manually entered excerpt
**/
?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This has been writte quickly by hand and hasn't been tested but should work fine.

2nd page not working pagination

I have a problem using codeigniter pagination. The problem is when I open the 2nd page during paging it is showing the error 404 page not found. I do not know why but for the 1st page it is working.
Model:
public function home($limit='',$segment=''){
$data = array();
$this->db->where(array('produk.status_produk'=>'1'))->limit($limit,$segment);
$i=0;
foreach ($this->get_all() as $item){
$data[$i] = $item;
$this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
if($this->db->get('foto_produk')->num_rows() > 0){
$this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
$foto = $this->db->get('foto_produk')->result();
foreach ($foto as $pic) {
$data[$i]->image = $pic->image;
$data[$i]->thumb = $pic->thumb;
}
} else {
$data[$i]->image = '';
$data[$i]->thumb = '';
}
$i++;
}
return $data;
}
Controller:
$cari = $this->input->post('search');
$this->db->like('deskripsi_produk', $cari);
$this->db->or_like('nama_produk', $cari);
$this->db->order_by('id_produk','desc');
$t = $this->produk_m->home();
$this->data->search = $this->input->post('search');
$this->load->library('pagination');
$perPage = 2;
$uri_segment = 2;
$total = count($t);
$config['base_url'] = site_url('search');
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = '2';
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$this->data->pagination = $this->pagination->create_links();
$this->db->like('deskripsi_produk', $cari);
$this->db->or_like('nama_produk', $cari);
$this->db->order_by('id_produk','desc');
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));
$this->template->set_judul('PT Bintang Sriwijaya')
->set_css('style')
->set_parsial('sidebar','sidebar_view',$this->data)
->set_parsial('topmenu','top_view',$this->data)
->render('search',$this->data);
}
View:
<h2>Hasil Pencarian</h2><br />
<?php if($produk):?>
<h3><center><?php echo $pagination;?></center></h3><br />
<?php foreach($produk as $item): ?>
<div class="produk-wrap">
<div class="image-wrap">
<div class="image-iner">
<a href="<?php echo base_url().'index.php/produk/'.$item->nama_kategori.'/'.$item->url_produk;?>">
<?php if($item->thumb == ''): ?>
<div class="no-image">
<span>Belum ada Gambar</span>
</div>
<?php else: ?>
<img src="<?php echo base_url().$item->thumb ?>" />
<?php endif; ?>
<?php if ($item->stok == 0): ?>
<div class="trans">
<span>Stok Habis</span>
</div>
<?php endif; ?>
</div>
</div>
<div class="produk-name">
<?php echo $item->nama_produk;?>
</div>
<?php if($item->harga_baru != 0): ?>
<div class="harga-lama">
Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
</div>
<div class="harga-jual">
Rp. <?php echo $this->cart->format_number($item->harga_baru) ?>
</div>
<?php else: ?>
<div class="harga-jual">
Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
</div>
<?php endif; ?>
</a>
</div>
<?php endforeach; ?>
<?php else:
echo "Data yang dicari belum ada";
endif;?>
please change these lines
$uri_segment = 2;
to
$uri_segment = 3;
and
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));
to
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(3));
and try beacause segment 1 is for controller, and 2 for methods in it, 3 may be the pagination element.

How to limit the number of pagination links in Zend_Paginator

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

PHP pagination does not recognize $page

I have downloaded the pagination code and used it for my website running on my local machine. After modifying and testing, there was a problem where it does not recognize the variable $page.
Please refer to the following code:
<?php
$rpp = 3; // results per page
$adjacents = 4;
$page = intval($_GET["page"]);
if($page<=0) $page = 1;
$reload = $_SERVER['PHP_SELF'];
$sql = "SELECT * FROM ".TABLE_IMAGE." ORDER BY id ASC";
$qry = mysql_query($sql, $con);
// count total number of appropriate listings:
$tcount = mysql_num_rows($qry);
// count number of pages:
$tpages = ($tcount) ? ceil($tcount/$rpp) : 1; // total pages, last page number
$i = 1;
$count = 0;
$j = ($page-1)*$rpp;
while(($result = mysql_fetch_array($qry)) && (($count<$rpp) && ($j<$tcount))){
$id = $result['id'];
$img = $result['path'];
$title = $result['title'];
$detail = $result['detail'];
$priority = $result['priority'];
$active = $result['isActive'];
?>
<div id="block-image-slider" class="<?php echo(($i%2==0)?'even':'odd')?>">
<h2><?php echo $title ?></h2><span class="operation">[កែប្រែ|លុប]</span>
<div class="block-slider-image-body">
<div class="left">
<ul>
<li>លេខរៀងទី<span class="space"><?php echo $id ?></span></li>
<li>កំនត់អទិភាពទី<span class="space"><?php echo $priority ?></span></li>
<li>ត្រូវបានបង្ហាញអោយឃើញ<span class="space"><?php echo (($active==1)?'បង្ហាញ':'មិនបង្ហាញ')?></span></li>
<li>អត្ថបទពេញ<div class="detail"><?php echo $detail ?></div></li>
</ul>
</div>
<div class="right">
<img src="<?php echo '../../image/Slider/'.$img ?>" alt="<?php echo $title ?>" width="170" height="100" />
</div>
<div style="clear:both;"></div>
</div>
</div>
<?php
mysql_data_seek($qry,$j);
$i++;
$j++;
$count++;
}//end of while loop
include("../include/paginate.php");
echo paginate_three($reload, $page, $tpages, $adjacents);
?>
This is the error it generated:
Notice: Undefined index: page in C:\wamp\www\1. Chirst Joy Church Website\common\admin\index.php on line 87
How can I fix this error?
This won't work unless your URL ends with ?page=3 (where 3 would be the current page number). In order to fix it you should change this line
$page = intval($_GET["page"]);
to
$page = (!empty($_GET["page"]) ? intval($_GET["page"]) : 1);

Categories