pagination example

hello

is there a good (usable) pagination example somewhere?

thanks,

–iM

Hi,

there is one in the yii blog demo, isn't it?!

And if you use the CRUD command every admin view will have a link pager.

What's your trouble with the pagination?

Greets

i'm executing a "hand-made" SQL query and just wanted to see how CPagination and CLink… works.

(i’m trying not to use yiic so I understand how the framework REALLY does it’s magic ;)

–iM

I don't know how CPagination works with a manual query and not active record, but I can show you how an AR CPagination script could work:

Action Code:



<?php


// Note: Posts is a Posts model just pulling data from a posts database table.


// Setup Criteria


$criteria = new CDbCriteria;


$criteria -> order = 'date';


// Count total records


$pages = new CPagination(Posts::model() -> count());


// Set Page Limit


$pages -> pageSize = 50;


// Apply page criteria to CDbCriteria


$pages -> applyLimit($criteria);


// Grab the records


$posts= Posts::model() -> findAll($criteria);


// Render the view


$this -> controller -> render('list', array('posts' => $posts, 'pages' => $pages));


View Code (Just something basic)



<?php $this->widget('CLinkPager',array('pages'=>$pages, "cssFile" => false)); ?>


<ul>


<?php foreach($posts as $x => $post): ?>


  <li><?php echo $post -> title ?></li>


<?php endforeach; ?>


</ul>


<?php $this->widget('CLinkPager',array('pages'=>$pages, "cssFile" => false)); ?>


Hope that helps you out :)

Chris

well,

here is how I did it. (I know that it kills performance, but I couldn't come up with a better idea)

so I execute the hand made SQL query, called it groups_for_pagination, I count the results, so I know the total number of rows. With that, I create a new CPagination object. at this point I know the pageSize and the current page.

Then, I execute the same query but with an extended LIMIT, which gives me back the results I want to display on the page.

so I just pass these in the controller:

        $this->render(


            'list', array(


                            'groups' => $groups,


                            'pages' => $pages,


                          )


        );

and here is the view part for the actual pagination (missing preview,  next, first, last etc … but it can be easily added)



<ul class="yiiPager">


<?php for( $i=0; $i < $pages->getPageCount(); $i++ ): ?>


    <li class="page <?= $i == $pages->getCurrentPage() ? 'selected' : '';?>">


        <a href="<?= $pages->createPageUrl( $this, $i ); ?>"><?= ($i+1) ?></a>


    </li>


<?php endfor; ?>


</ul>


i know it’s very hacky, but again this is a hand-crafted-SQL-query, I couldn’t come up with anything else :(

–iM

Hi,

in Yii with active records it's performed the same way: first make a COUNT() query to get the total number of matched records, create a CPagination object with this value and then add LIMIT and OFFSET to the query to get the actual list of records.

But why are you creating your own pager inside your view instead of using one of the pagers Yii offers? Using one of them works the same way with a hand made sql query as with active records.

Greets