cakephp的分页排序

4 篇文章 0 订阅
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>";
}

?>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值