qeephp内容分页

当列出查找的内容有很多条的时候我们可以将数据进行分页显示。

user表的结构如图:

user表结构

现在要将用户以列表的形式显示,显然不可能将查询结果显示在一页当中,此时要将结果分页显示,首先将分页控件page.php复制到项目的control文件中,然后我们可以在控制器中输入如下代码:

function actionCusList()
    {
        $cus_info = User::find();
        //获取当前是第几页
        $page = intval( $this->_context->page );
        $page<1?1:$page;
        //设置每页显示的数量
        $page_size = 10;
                                                  
        //按条件查找用户
        if($id = $this->_context->get('cus_id'))
            $cus_info->where('id = ?', $id);
        else
        {
            if($first_name = $this->_context->get('first_name'))
               $cus_info->where('first_name = ?', $first_name);
                                                      
            if($last_name = $this->_context->get('last_name'))
               $cus_info->where('last_name = ?', $last_name);
                                                      
            if($email = $this->_context->get('email'))
               $cus_info->where('email = ?', $email);
                                                         
            if($code = $this->_context->get('code'))
               $cus_info->where('pro_id = ?', Program::find('code = ?', $code)->getOne()->id);
        }
                                                  
        $cus_info->limitPage($page, $page_size);
        $cus = $cus_info->getAll();
                                          
        //渲染视图
        $this->_view['url_args'] = $this->_context->get();
        $this->_view['pagination'] = $cus_info->getPagination();
        $this->_view['cus'] = $cus;
    }

此代码中有一处是按条件查找用户,我们可以进行前台设计一个表单让用户输入特定条件来查找用户,代码如下:

<div class="pt10">
        <a href="javascript:;" onclick="javascript:$(this).parent().next('div').toggle();" title="search &gt;">Search &gt;
        </a>
    </div>
    <!--search-->
    <div id="searchbox" style="display: none;" class="search_box">
      <a href="javascript:void(0);" onclick="javascript:$(this).parent().hide();" class="close2 f_r" title="close">close</a>
    <form name="city_search_form" action="#" method="get">
      <p>
         <label for="">Customer ID</label>
         <input class="text" name="cus_id" type="text">
      </p>
      <p>
          <label for="">First Name</label>
          <input name="first_name" class="text" type="text">
      </p>
      <p>
          <label for="">Last Name</label>
          <input name="last_name" class="text" type="text">
      </p>
      <p>
          <label for="">Email Address</label>
          <input name="email" class="text" type="text">
      </p>
       <p>
          <label for="">Program Code</label>
          <input name="code" class="text" type="text">
      </p>
      <p>
        <label>&nbsp;</label>
        <a onclick="city_search_form.submit();" title="" class="add_btn">Search</a>
     </p>
    </form>
    </div>
                
    <div class="paging pt10 txt_right">
      <?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
    </div>  
    <table class="pagetab mtb6" cellpadding="0" cellspacing="0" width="100%">
      <thead>
        <tr>
            <th width="10%">Cust. ID</th>
            <th width="15%">First Name</th>
            <th width="15%">Last Name</th>
            <th width="20%">Email Address</th>
            <th width="10%">Program</th>
            <th width="25%">Last Login</th>
            <th width="5%">Action</th>
        </tr>
      </thead>
      <?php foreach ($cus as $cus):; ?>
      <tbody>
        <tr>
          <td><?php echo $cus['id']; ?></td>
          <td><?php echo $cus['first_name']; ?></td>
          <td><?php echo $cus['last_name']; ?></td>
          <td><?php echo $cus['email']; ?></td>
          <td><?php echo $cus['program']['name']; ?></td>
          <td><?php echo $cus['last_login_date']; ?></td>
          <td>
            <a href="<?php echo url('cus/cusdetail', array('id' => $cus['id']))?>" title="View">View</a>
          </td>
        </tr>
     </tbody>
     <?php endforeach; ?>
    </table>
    <div class="paging pt10 txt_right">
      <?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
    </div>

分页关键代码为:

<?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>

显示结果如图:

用户列表显示

转载于:https://my.oschina.net/frylan/blog/64729

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值