zend framework - paginator

Pagination

 

All over the Internet we can find sites that uses pagination to give users easy way to read a large amount of content. The most known pagination is probably Google`s one since there are a lot of search results  the pagination let us go from one page to another. Each page display a limited amount of results and so to see other results we use the pagination.

Zend framework (Zend framework) is a library of code that makes the developer work easier. It is a PHP framework and it is build on the MVC pattern with a lot of components so that whenever the developer need to use one he can do it easily. One of its benefits is that it is very flexible so there is no one way to do things.

 

Today i want to check the pagination component Zend offer.

In our example we want to paginate records from a database. We will paginate all the posts that belongs to a certain category.

 

The controller

We will not discuss the MVC pattern and the role the controller do within it. You can read about that in the like i provided earlier.

In general terms, the process that Zend framework expect from us when we implement the paginator if as follow:

  1. defining the necessary variable for the paginator.
  2. create a query to the database.
  3. sending the result of the query with the variables to the paginator
  4. then send the paginator to the view

So lest begin: phase one

In the controller we will create the variables that we need.

 

 

 $paginationCount = $this->config->pagination->item->count->per->page;


$page = $this->_request->getParam('page');

 

The Model

Since we deal with the MVC pattern the query itself will be written in the model:

 

	public function getPostsByCategory($category,$page = 1, $order = "date",$paginationCount)


	{

//the query 

        $query = $this->select()->from(array('posts'),array( 

        'posts.id','posts.title','posts.date','posts.short_desc','posts.text','posts.category_id','posts.status','posts.author' 


        )); 

        $query->where("category_id = ? ", $category); 

        $query->where('status = ?', 1); 

        $query->order('date DESC'); 




                // Create a paginator based on query

                $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query));

                $paginator->setItemCountPerPage($paginationCount);

                $paginator->setCurrentPageNumber($page);

        


        

                return $paginator;

        

	}




As you can see the method create the query and send its result to the zend paginator component. We use the variables from the controller here. So now we can go back to the controller and from there to the view:

 

$post = new Bloging_Model_Post();

$this->view->posts = $post->getPostsByCategory(1,$page,"date",$paginationCount);





In the first line: we create a new instance of the model and then we send the right variables. Note that the call to the method already send the paginator to the view.

 

The View

Now that we have the paginator and its contents we need to display it to the users. This is where the view comes into play.

In the view file we loop the contents and display what Zend calls "pagination-controls" which are the back and forward buttons. Sadly, i have problems displaying HTML code without executing it so in essence all that is left to do is the display part.

We are done. Hope that the basic idea was passed clearly. You can expand this issue here.

 

I must say i found one issue problematic with Zend paginator: i cannot use the paginator if i cache the database results. If anyone know how to do it i`ll appreciate it a lot.

No votes yet

a question

Hi

first, thanks for this tutorial, it hellped me a lot.

but i have the same issue with the cache. Have you found an answer to the problem?

Hi

Sadly the answer is no.

The thing is that as your content get larger you would need a cache for paginated content and it seem you cannot do that in ZF at this moment.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.