登录  /  注册
cakephp的分页排序
巴扎黑
发布: 2016-11-23 15:45:03
原创
973人浏览过

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中

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

4 页面模版文件中
app/views/users/view_users.ctp

<?php
echo "<div class='page-title'>Users</div>"; //title
//this 'add new user' button will be used for the next tutorial
echo "<div style='float:right;'>";
    $url = "add/";
    echo $form->button('Add New User', array('onclick' => "location.href='".$this->Html->url($url)."'"));
echo "</div>";
echo "<div style='clear:both;'></div>";
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 "<div 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 "</div>";
    
}else{ //if there are no records found, display this
    echo "<div class='no-records-found'>No Users found.</div>";
}
?>
登录后复制



来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学