Chinaunix
标题:
cakephp的分页排序
[打印本页]
作者:
feiyang10086
时间:
2012-01-04 16:07
标题:
cakephp的分页排序
cakephp的分页排序
cakephp中的分页还是很简单的,下面例子复习下
1 数据表
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 中,代码为:
<?php
class User extends AppModel{
var $name = 'User';
?>
复制代码
3 app/controllers/users_controller.php中
Java代码
1.function view_users(){
2.
3. $this->paginate = array(
4. 'limit' => 2
5. );
6.
7. //users用于在前端页面中显示
8. $this->set('users', $this->paginate('User'));
9.}
function view_users(){
$this->paginate = array(
'limit' => 2
);
//users用于在前端页面中显示
$this->set('users', $this->paginate('User'));
}
复制代码
4 页面模版文件中
app/views/users/view_users.ctp
Java代码
1.<?php
2.echo "<div class='page-title'>Users</div>"; //title
3.
4.//this 'add new user' button will be used for the next tutorial
5.echo "<div style='float:right;'>";
6. $url = "add/";
7. echo $form->button('Add New User', array('onclick' => "location.href='".$this->Html->url($url)."'"));
8.echo "</div>";
9.echo "<div style='clear:both;'></div>";
10.
11.if( sizeOf( $users ) > 0 ){ //check if there are user records returned
12.?>
13.<table>
14. <tr>
15.
16. <!--第一个参数是表格列的label,第一个参数是排序中实际数据库的字段-->
17. <th style='text-align: left;'><?php echo $paginator->sort('Firstname', 'firstname'); ?></th>
18. <th><?php echo $paginator->sort('Lastname', 'lastname'); ?></th>
19. <th><?php echo $paginator->sort('Email', 'email'); ?></th>
20. <th><?php echo $paginator->sort('Username', 'username'); ?></th>
21. <th>Action</th>
22. </tr>
23. <tr>
24. <?php
25. foreach( $users as $user ){ //we wil loop through the records to DISPLAY DATA
26. echo "<tr>";
27. echo "<td>";
28. echo "{$user['User']['firstname']}";
29. echo "</td>";
30. echo "<td>{$user['User']['lastname']}</td>";
31. echo "<td>{$user['User']['email']}</td>";
32. echo "<td>{$user['User']['username']}</td>";
33. echo "<td style='text-align: center;'>";
34. //'Edit' and 'Delete' link here will be used for our next tutorials
35. echo $html->link('Edit', array('action'=>'edit/'.$user['User']['id']), null, null);
36. echo " / ";
37. echo $html->link('Delete', array('action'=>'delete/'.$user['User']['id']), null, 'Are you sure you want to delete this record?');
38. echo "</td>";
39. echo "</tr>";
40. }
41. ?>
42. </tr>
43.</table>
44.
45.<?php
46. //分页开始
47. echo "<div class='paging'>";
48.
49. //第一页
50. echo $paginator->first('First');
51. echo " ";
52.
53. //前一页
54. if($paginator->hasPrev()){
55. echo $paginator->prev('<<');
56. }
57.
58. echo " ";
59. //指定页数
60. echo $paginator->numbers(array('modulus' => 2));
61. echo " ";
62.
63.
64. if($paginator->hasNext()){
65. echo $paginator->next('>>');
66. }
67.
68. echo " ";
69. //最后一页
70. echo $paginator->last('Last');
71.
72. echo "</div>";
73.
74.}else{ //if there are no records found, display this
75. echo "<div class='no-records-found'>No Users found.</div>";
76.}
77.
78.?>
复制代码
作者:
如果有一天21
时间:
2012-01-04 16:08
谢谢分享
作者:
crazymus2012
时间:
2012-02-25 00:53
嗯,不错哈~
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2