We help IT Professionals succeed at work.

zend paginator problem: need help desperatly

derrida
derrida asked
on
1,045 Views
Last Modified: 2013-11-10
Hi
i have a form with 2 select menus: to choose a category and a limit (like only the last week).
on submit the requested records should appear inside a table.
and all that work great. i get the right results according to different choices.

so now i wanted to paginate the results with zend_paginator. so the queries results are passed to the paginator, and i added the paginator controls.
so on the first page it seem ok: the records per page is there and the links for the pagination is there. but when i press to see page 2 3 or whatever, i get a page without any records or the paginator controls.

the url on the first page is: http://thesite/admin/blog/
when i click on the links this is the url: http://thesite/admin/blog/index/page/2
which (as far as i understand) is the right url. i get no error message, but no table with records and no pagination controls.

i attach the code from the controller and the methods from the model and the pagination controls .

i am desperate since i have tried everything i can think of. hope someone can help.

best regards

ron
this is the controller code:
    public function indexAction()
    {
        // action body
        $categories = new Admin_Model_Category();
        $getcat = $categories->getAllCategories();
        $this->view->categories = $getcat;
        
        
       //take the form and send the chosen options to a query
       $category = $this->_request->getPost('category');
       $limit = $this->_request->getPost('limit');
       
        // paginating the posts
    	$paginationCount = $this->config->pagination->item->count->per->page;
    	$this->view->paginationCount = $paginationCount;
    	
    	//$theid = $this->_request->getParam('id');
    	$page = $this->_request->getParam('page');
       
       $post = new Admin_Model_Post();
       if ($limit == "all")
       {
          
           $this->view->posts = $post->getAllPosts($category,$paginationCount,$page);
 
       }
       
        if ($limit == 7)
       {
           $length = "DAY";
           $results = $post->getPostsByLimit($category,$limit,$length,$paginationCount,$page);
           
           $this->view->posts = $results;
       }
       
        if ($limit == 1)
       {
           $length = "MONTH";
           $results = $post->getPostsByLimit($category,$limit,$length,$paginationCount,$page);
           
           $this->view->posts = $results;
       }
       
         if ($limit == 2)
       {
           $length = "YEAR";
           $results = $post->getPostsByLimit($category,$limit,$length,$paginationCount,$page);
           
           $this->view->posts = $results;
       }
        
    
 
    }//end index action
 
these are the methods from the model:
   public function getPostsByLimit($category,$limit,$length,$paginationCount,$page)
    {
        
        $query = $this->select()->from(array(
        'posts'),array(
        'posts.id','posts.title','posts.date','posts.status'));
        $query->where("category_id = {$category} AND date BETWEEN DATE_SUB(CURDATE(),INTERVAL {$limit} {$length}) AND CURDATE()");
        
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query));
		$paginator->setItemCountPerPage($paginationCount);
		$paginator->setCurrentPageNumber($page);
		return $paginator;
        
    }
    
    public function getAllPosts($category,$paginationCount,$page)
    {
        $query = $this->select()->from(array(
        'posts'),array(
        'posts.id','posts.title','posts.date','posts.status'));
        $query->where('category_id = ?', $category);
        $query->order('date');
       
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query));
		$paginator->setItemCountPerPage($paginationCount);
		$paginator->setCurrentPageNumber($page);
		return $paginator;
        
        
    }
 
this code is in the view file:
 
<?php if (count($this->posts) > 0){?>
<table class="thetable tiger-stripe"  >
  <tr>
    <th>ID</th>
    <th>Title</th>
    <th>Date</th>
    <th>Status</th>
    
  </tr>
  
  <?php foreach ($this->posts as $post){?>
  <tr>
 	<td><?php echo $post->id;?></td>
 	<td><?php echo $post->title;?></td>
 	<td><?php echo $post->date;?></td>
 	<td><?php 
 	if ($post->status == 0)
 	{
 	    echo "edit mode";
 	}else{
 	    echo "published";
 	}
 	
 	?></td>
 	<td><a class="delete" href='/admin/blog/deletepost/postid/<?php echo $post->id;?>' title="Delete this file">Þ×ç</a></td>
 	<td><a href="/admin/blog/editpost/postid/<?php echo $post->id;?>">âèÕÚ</a></td>
    </tr>
    <?php }?>
</table>
 
<div id="thecontent">
<?php 
echo $this->paginationControl($this->posts,'Sliding','my_navpage.phtml');
echo $this->paginationControl($this->posts,'Sliding','my_pagination.phtml');
 
?>
</div>
<?php }?>
 
sorry for pasting so much code but i want you to have the full info for helping mt:)

Open in new window

Comment
Watch Question

Commented:
Hi Ron,

The problem is that your url did not contain either "category" or "limit" values. Thus your query in  getPostsByLimit and getAllPosts will return noting.

To be able to provide "category" and "limit" value you need to do:
1/ In the controller share with view category and limit values. After rows:

$category = $this->_request->getPost('category');
$limit = $this->_request->getPost('limit');
add
$this->view->category = $category;
$this->view->limit = $limit;

2/ In the view change the paginationcontroll to:
echo $this->paginationControl($this->posts,'Sliding','my_navpage.phtml', array('category'=>$this->category, 'limit'=>$this->limit));
echo $this->paginationControl($this->posts,'Sliding','my_pagination.phtml', array('category'=>$this->category, 'limit'=>$this->limit));

3/ in both pager files my_navpage.phtml and my_pagination.phtml
set the url as (note that @action@, @controller@ and @module@ needs to be define to match your app)
$this->url(@action@, @controller@, @module@, array('page'=>$this->page, 'category'=>$this->category, 'limit'=>$this->limit);


This should make your script works. If you feel problems in applying the patch above, post your files and I'll make the changes for you.

Author

Commented:
Hi
first thanks for answering ,i really appriciate any help:)
i have put this in the my_navigation_posts.phtml file (changes the name only for the testing):

<?php foreach ($this->pagesInRange as $page):
if ($page != $this->current): ?>
<a href="<?php echo $this->url(@index@,@blog@,@admin@,array('page' => $page,'category' => $this->category,'limit' => $this->limit));?>"><?php echo $page;?></a>
<?php else:

echo $page;?>
<?php endif;?>
<?php endforeach;?>
 
and made all the other changes. i get this error:
Parse error: syntax error, unexpected '@' in C:\apache\htdocs\mysite\application\modules\admin\views\scripts\my_pagination_posts.phtml on line 34
 
ron

 

Commented:
Ron,

The url should be


$this->url('index','blog','admin',array('page' => $page,'category' => $this->category,'limit' => $this->limit))


Regards

Author

Commented:
hi
 
now i get:
Catchable fatal error: Argument 1 passed to Zend_View_Helper_Url::url() must be an array, string given in C:\apache\htdocs\shtigliz\library\Zend\View\Helper\Url.php on line 46

 
ron

Commented:
Sorry Ron,

I was using my own URL helper. To be able to use Zend url helper you need to change to

$this->url(array('action'=>'index','controller'=>'blog','module'=>'admin','page'=>$page, 'category'=>$this->category,'limit'=>$this->limit));

Author

Commented:
Hi
its ok:)
but now i get the same result as before: no errors but i get the results only on the first page but not on the other pages.
now the url for the first page is:
http://shtigliz/admin/blog/
and the url on page 2 (for example):
http://shtigliz/admin/blog/index/page/2/category/1/limit/all
 
any idea?
 
ron

Commented:
Can you post your files here? (Controller, view and pager script)

Author

Commented:
hi
sadly they did not accepted the php and phtml extensions, so i converted all to .txt, just change them as follow:
my_pagination_posts.phtml
index.phtml
and the others to .php
 
ron

paginationfiles.zip
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi
 
ho my god i`m so dumn. its so reasonable now and i totally missed it.
 
thank you so much for your great help:)

Commented:
You're welcome Ron.

Thanks for the rating

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.