免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2261 | 回复: 2

cakephp的分页排序 [复制链接]

论坛徽章:
0
发表于 2012-01-04 16:07 |显示全部楼层
cakephp的分页排序






cakephp中的分页还是很简单的,下面例子复习下

1 数据表
  1.   CREATE TABLE IF NOT EXISTS `users` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `firstname` varchar(32) NOT NULL,
  4.   `lastname` varchar(32) NOT NULL,
  5.   `email` varchar(32) NOT NULL,
  6.   `username` varchar(32) NOT NULL,
  7.   `password` varchar(32) NOT NULL,
  8.   PRIMARY KEY (`id`)
  9. )
复制代码
2 在app/models/user.php 中,代码为:
  1.    <?php
  2. class User extends AppModel{

  3.     var $name = 'User';
  4. ?>
复制代码
3  app/controllers/users_controller.php中
   
Java代码
  1. 1.function view_users(){   
  2. 2.      
  3. 3.        $this->paginate = array(   
  4. 4.        'limit' => 2  
  5. 5.    );   
  6. 6.      
  7. 7.   //users用于在前端页面中显示   
  8. 8.    $this->set('users', $this->paginate('User'));   
  9. 9.}  
  10. function view_users(){
  11.    
  12.         $this->paginate = array(
  13.         'limit' => 2
  14.     );
  15.    
  16.    //users用于在前端页面中显示
  17.     $this->set('users', $this->paginate('User'));
  18. }
复制代码
4 页面模版文件中
app/views/users/view_users.ctp

Java代码
  1. 1.<?php   
  2. 2.echo "<div class='page-title'>Users</div>"; //title   
  3. 3.  
  4. 4.//this 'add new user' button will be used for the next tutorial   
  5. 5.echo "<div style='float:right;'>";   
  6. 6.    $url = "add/";   
  7. 7.    echo $form->button('Add New User', array('onclick' => "location.href='".$this->Html->url($url)."'"));   
  8. 8.echo "</div>";   
  9. 9.echo "<div style='clear:both;'></div>";   
  10. 10.  
  11. 11.if( sizeOf( $users ) > 0 ){ //check if there are user records returned   
  12. 12.?>   
  13. 13.<table>   
  14. 14.    <tr>   
  15. 15.      
  16. 16.   <!--第一个参数是表格列的label,第一个参数是排序中实际数据库的字段-->      
  17. 17.         <th style='text-align: left;'><?php echo $paginator->sort('Firstname', 'firstname'); ?></th>   
  18. 18.        <th><?php echo $paginator->sort('Lastname', 'lastname'); ?></th>   
  19. 19.        <th><?php echo $paginator->sort('Email', 'email'); ?></th>   
  20. 20.        <th><?php echo $paginator->sort('Username', 'username'); ?></th>   
  21. 21.        <th>Action</th>   
  22. 22.    </tr>   
  23. 23.    <tr>   
  24. 24.    <?php   
  25. 25.        foreach( $users as $user ){ //we wil loop through the records to DISPLAY DATA   
  26. 26.            echo "<tr>";   
  27. 27.                echo "<td>";   
  28. 28.                                      echo "{$user['User']['firstname']}";   
  29. 29.                echo "</td>";   
  30. 30.                echo "<td>{$user['User']['lastname']}</td>";   
  31. 31.                echo "<td>{$user['User']['email']}</td>";   
  32. 32.                echo "<td>{$user['User']['username']}</td>";   
  33. 33.                echo "<td style='text-align: center;'>";   
  34. 34.                    //'Edit' and 'Delete' link here will be used for our next tutorials   
  35. 35.                    echo $html->link('Edit', array('action'=>'edit/'.$user['User']['id']), null, null);   
  36. 36.                    echo " / ";   
  37. 37.                    echo $html->link('Delete', array('action'=>'delete/'.$user['User']['id']), null, 'Are you sure you want to delete this record?');   
  38. 38.                echo "</td>";   
  39. 39.            echo "</tr>";   
  40. 40.        }   
  41. 41.    ?>   
  42. 42.    </tr>   
  43. 43.</table>   
  44. 44.  
  45. 45.<?php   
  46. 46.    //分页开始   
  47. 47.    echo "<div class='paging'>";   
  48. 48.  
  49. 49.    //第一页   
  50. 50.      echo $paginator->first('First');   
  51. 51.    echo " ";   
  52. 52.      
  53. 53.    //前一页   
  54. 54.    if($paginator->hasPrev()){   
  55. 55.        echo $paginator->prev('<<');   
  56. 56.    }   
  57. 57.      
  58. 58.    echo " ";   
  59. 59.   //指定页数   
  60. 60.    echo $paginator->numbers(array('modulus' => 2));   
  61. 61.    echo " ";   
  62. 62.      
  63. 63.      
  64. 64.    if($paginator->hasNext()){   
  65. 65.        echo $paginator->next('>>');   
  66. 66.    }   
  67. 67.      
  68. 68.    echo " ";   
  69. 69.    //最后一页   
  70. 70.    echo $paginator->last('Last');   
  71. 71.      
  72. 72.    echo "</div>";   
  73. 73.      
  74. 74.}else{ //if there are no records found, display this   
  75. 75.    echo "<div class='no-records-found'>No Users found.</div>";   
  76. 76.}   
  77. 77.  
  78. 78.?>  
复制代码

论坛徽章:
0
发表于 2012-01-04 16:08 |显示全部楼层
谢谢分享

论坛徽章:
0
发表于 2012-02-25 00:53 |显示全部楼层
嗯,不错哈~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP