知识屋:更实用的电脑技术知识网站
所在位置:首页 > 编程技术 > PHP编程

qeephp内容分页

发布时间:2015-05-27 19:33:40作者:知识屋

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

user表的结构如图:

 user表结构

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

01
function actionCusList()
02
{
03
    $cus_info = User::find();
04
    //获取当前是第几页
05
    $page = intval( $this->_context->page );
06
    $page<1?1:$page;
07
    //设置每页显示的数量
08
    $page_size = 10;
09
                                              
10
    //按条件查找用户
11
    if($id = $this->_context->get('cus_id'))
12
        $cus_info->where('id = ?', $id);
13
    else
14
    {
15
        if($first_name = $this->_context->get('first_name'))
16
           $cus_info->where('first_name = ?', $first_name);
17
                                                  
18
        if($last_name = $this->_context->get('last_name'))
19
           $cus_info->where('last_name = ?', $last_name);
20
                                                  
21
        if($email = $this->_context->get('email'))
22
           $cus_info->where('email = ?', $email);
23
                                                     
24
        if($code = $this->_context->get('code'))
25
           $cus_info->where('pro_id = ?', Program::find('code = ?', $code)->getOne()->id);
26
    }
27
                                              
28
    $cus_info->limitPage($page, $page_size);
29
    $cus = $cus_info->getAll();
30
                                      
31
    //渲染视图
32
    $this->_view['url_args'] = $this->_context->get();
33
    $this->_view['pagination'] = $cus_info->getPagination();
34
    $this->_view['cus'] = $cus;
35
}
此代码中有一处是按条件查找用户,我们可以进行前台设计一个表单让用户输入特定条件来查找用户,代码如下:

01
<div class="pt10">
02
        <a href="javascript:;" onclick="javascript:$(this).parent().next('div').toggle();" title="search &gt;">Search &gt;
03
        </a>
04
    </div>
05
    <!--search-->
06
    <div id="searchbox" style="display: none;" class="search_box">
07
      <a href="javascript:void(0);" onclick="javascript:$(this).parent().hide();" class="close2 f_r" title="close">close</a>
08
    <form name="city_search_form" action="#" method="get">
09
      <p>
10
         <label for="">Customer ID</label>
11
         <input class="text" name="cus_id" type="text">
12
      </p>
13
      <p>
14
          <label for="">First Name</label>
15
          <input name="first_name" class="text" type="text">
16
      </p>
17
      <p>
18
          <label for="">Last Name</label>
19
          <input name="last_name" class="text" type="text">
20
      </p>
21
      <p>
22
          <label for="">Email Address</label>
23
          <input name="email" class="text" type="text">
24
      </p>
25
       <p>
26
          <label for="">Program Code</label>
27
          <input name="code" class="text" type="text">
28
      </p>
29
      <p>
30
        <label>&nbsp;</label>
31
        <a onclick="city_search_form.submit();" title="" class="add_btn">Search</a>
32
     </p>
33
    </form>
34
    </div>
35
                
36
    <div class="paging pt10 txt_right">
37
      <?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
38
    </div>
39
    <table class="pagetab mtb6" cellpadding="0" cellspacing="0" width="100%">
40
      <thead>
41
        <tr>
42
            <th width="10%">Cust. ID</th>
43
            <th width="15%">First Name</th>
44
            <th width="15%">Last Name</th>
45
            <th width="20%">Email Address</th>
46
            <th width="10%">Program</th>
47
            <th width="25%">Last Login</th>
48
            <th width="5%">Action</th>
49
        </tr>
50
      </thead>
51
      <?php foreach ($cus as $cus):; ?>
52
      <tbody>
53
        <tr>
54
          <td><?php echo $cus['id']; ?></td>
55
          <td><?php echo $cus['first_name']; ?></td>
56
          <td><?php echo $cus['last_name']; ?></td>
57
          <td><?php echo $cus['email']; ?></td>
58
          <td><?php echo $cus['program']['name']; ?></td>
59
          <td><?php echo $cus['last_login_date']; ?></td>
60
          <td>
61
            <a href="<?php echo url('cus/cusdetail', array('id' => $cus['id']))?>" title="View">View</a>
62
          </td>
63
        </tr>
64
     </tbody>
65
     <?php endforeach; ?>
66
    </table>
67
    <div class="paging pt10 txt_right">
68
      <?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
69
    </div>
分页关键代码为:

1
<?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
显示结果如图:

 用户列表显示


作者:frylan

(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜