Home  >  Article  >  php教程  >  qeephp内容分页

qeephp内容分页

WBOY
WBOYOriginal
2016-06-13 10:50:431740browse

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

user表的结构如图:

 

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

01
function actionCusList()
02
{
03
    $cus_info = User::find();
04
    //获取当前是第几页
05
    $page = intval( $this->_context->page );
06
    $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


02
        Search >
03
       

04
   

05
   
06
   
35
                
36
   

37
      _control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
38
   

39
   
40
     
41
       
42
           
43
           
44
           
45
           
46
           
47
           
48
           
49
       
50
     
51
     
52
     
53
       
54
         
55
         
56
         
57
         
58
         
59
         
60
         
63
       
64
    
65
    
66
   
Cust. ID First Name Last Name Email Address Program Last Login Action

61
            View
62
         

67
   

68
      _control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
69
   

分页关键代码为:

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

 


作者:frylan

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn