Yii Framework Forum: pagination example - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

pagination example Rate Topic: ***** 1 Votes

#1 User is offline   imehesz 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 708
  • Joined: 13-June 09
  • Location:Jacksonville, FL

Posted 23 June 2009 - 02:16 PM

hello

is there a good (usable) pagination example somewhere?

thanks,
--iM
0

#2 User is offline   yoshi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 28-February 09
  • Location:Germany

Posted 23 June 2009 - 02:59 PM

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
0

#3 User is offline   imehesz 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 708
  • Joined: 13-June 09
  • Location:Jacksonville, FL

Posted 23 June 2009 - 03:48 PM

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
1

#4 User is offline   krak3n 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 17-February 09
  • Location:London, United Kingdom

Posted 24 June 2009 - 03:37 AM

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
0

#5 User is offline   imehesz 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 708
  • Joined: 13-June 09
  • Location:Jacksonville, FL

Posted 24 June 2009 - 07:03 AM

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
0

#6 User is offline   yoshi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 28-February 09
  • Location:Germany

Posted 28 June 2009 - 08:04 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users