With a short list of records, the basic view is good enough, however, once the number of records exceeds 50 or so, it is helpful to use CakePHP's built in pagination to display pages on multiple pages.
First edit
/app/controllers/users_controller.php
and replace this line
$this->set('users',$this->User->find('all'));
with this one
$this->set('users', $this->paginate());
Save and upload the file. Now if you browse to
http://drug-ed.com/ it still works because the data pulled from the database is the same, we just haven't taken advantage of the pagination features yet.
Edit
/app/views/users/index.ctp
and replace
<th>ID</th>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
with
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('username');?></th>
<th><?php echo $this->Paginator->sort('first_name');?></th>
<th><?php echo $this->Paginator->sort('last_name');?></th>
<th><?php echo $this->Paginator->sort('email');?></th>
Then after the users table within the users division add
<p>
<?php
echo $this->Paginator->counter(array('format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)));
?>
</p>
<div class="paging">
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
| <?php echo $this->Paginator->numbers();?>
| <?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>
Save and upload the file then browse to
http://drug-ed.com/ to verify that it works. You can now click on the headers of each column to sort the users, as well as view them on multiple pages, though it requires more than 20 records for a new page.
No comments:
Post a Comment