cakephp paging sorting

巴扎黑
Release: 2016-11-23 15:45:03
Original
1196 people have browsed it

Pagination in cakephp is still very simple. Let’s review the following example

1 Data table

  CREATE TABLE IF NOT EXISTS `users` ( 
  `id` int(11) NOT NULL AUTO_INCREMENT, 
  `firstname` varchar(32) NOT NULL, 
  `lastname` varchar(32) NOT NULL, 
  `email` varchar(32) NOT NULL, 
  `username` varchar(32) NOT NULL, 
  `password` varchar(32) NOT NULL, 
  PRIMARY KEY (`id`) 
)
Copy after login

2 In app/models/user.php, the code is:

 
Copy after login

3 In app/controllers/users_controller.php

function view_users(){
    
        $this->paginate = array(
        'limit' => 2
    );
    
   //users用于在前端页面中显示 
    $this->set('users', $this->paginate('User'));
}
Copy after login

4 page template file
app/views/users/view_users.ctp

Users
"; //title //this 'add new user' button will be used for the next tutorial echo "
"; $url = "add/"; echo $form->button('Add New User', array('onclick' => "location.href='".$this->Html->url($url)."'")); echo "
"; echo "
"; if( sizeOf( $users ) > 0 ){ //check if there are user records returned ?> "; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } ?>
sort('Firstname', 'firstname'); ?> sort('Lastname', 'lastname'); ?> sort('Email', 'email'); ?> sort('Username', 'username'); ?> Action
"; echo "{$user['User']['firstname']}"; echo "{$user['User']['lastname']}{$user['User']['email']}{$user['User']['username']}"; //'Edit' and 'Delete' link here will be used for our next tutorials echo $html->link('Edit', array('action'=>'edit/'.$user['User']['id']), null, null); echo " / "; echo $html->link('Delete', array('action'=>'delete/'.$user['User']['id']), null, 'Are you sure you want to delete this record?'); echo "
"; //第一页 echo $paginator->first('First'); echo " "; //前一页 if($paginator->hasPrev()){ echo $paginator->prev('<<'); } echo " "; //指定页数 echo $paginator->numbers(array('modulus' => 2)); echo " "; if($paginator->hasNext()){ echo $paginator->next('>>'); } echo " "; //最后一页 echo $paginator->last('Last'); echo "
"; }else{ //if there are no records found, display this echo "
No Users found.
"; } ?>
Copy after login



Related labels:
php
source:php.cn
Previous article:PHP mysql transaction processing example Next article:PHP7 parameters, arrays and Zvals
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!