程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> qeephp內容分頁

qeephp內容分頁

編輯:關於PHP編程

當列出查找的內容有很多條的時候我們可以將數據進行分頁顯示。

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved