cakephp的分页排序
在PHP学习过程中你是否感到困惑?以下是百分网小编精心为大家整理的`PHP教程,希望对大家有所帮助!更多内容请关注应届毕业生网!
cakephp中的分页还是很简单的,下面例子复习下
1 数据表
1
2
3
4
5
6
7
8
9
|
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`) ) |
2 在app/models/user.php 中,代码为:
1
2
3
4
|
<?php class User extends AppModel{ var $name = 'User' ; ?> |
3 app/controllers/users_controller.php中
1
2
3
4
5
6
7
8
9
|
function view_users(){ $this ->paginate = array ( 'limit' => 2 ); //users用于在前端页面中显示 $this ->set( 'users' , $this ->paginate( 'User' )); } |
4 页面模版文件中
app/views/users/view_users.ctp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<?php echo "<p class='page-title'>Users</p>" ; //title //this 'add new user' button will be used for the next tutorial echo "<p style='float:right;'>" ; $url = "add/" ; echo $form ->button( 'Add New User' , array ( 'onclick' => "location.href='" . $this ->Html->url( $url ). "'" )); echo "</p>" ; echo "<p style='clear:both;'></p>" ; if ( sizeOf( $users ) > 0 ){ //check if there are user records returned ?> <table> <tr> <!--第一个参数是表格列的label,第一个参数是排序中实际数据库的字段--> <th style= 'text-align: left;' ><?php echo $paginator ->sort( 'Firstname' , 'firstname' ); ?></th> <th><?php echo $paginator ->sort( 'Lastname' , 'lastname' ); ?></th> <th><?php echo $paginator ->sort( 'Email' , 'email' ); ?></th> <th><?php echo $paginator ->sort( 'Username' , 'username' ); ?></th> <th>Action</th> </tr> <tr> <?php foreach ( $users as $user ){ //we wil loop through the records to DISPLAY DATA echo "<tr>" ; echo "<td>" ; echo "{$user['User']['firstname']}" ; echo "</td>" ; echo "<td>{$user['User']['lastname']}</td>" ; echo "<td>{$user['User']['email']}</td>" ; echo "<td>{$user['User']['username']}</td>" ; echo "<td style='text-align: center;'>" ; //'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 "</td>" ; echo "</tr>" ; } ?> </tr> </table> <?php //分页开始 echo "<p class='paging'>" ; //第一页 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 "</p>" ; } else { //if there are no records found, display this echo "<p class='no-records-found'>No Users found.</p>" ; } ?> |
【cakephp的分页排序】相关文章:
word怎样自动分页10-12
PHP简单的分页过程与原理09-25
C++ 排序插入排序详解10-04
PHP数组的排序09-09
java的常见排序方法11-25
c语言的排序算法10-05
C语言中使用快速排序算法对元素排序的实例12-01
PHP 多维数组的排序问题08-24