Home > Backend Development > PHP Tutorial > cakephp paging sorting

cakephp paging sorting

巴扎黑
Release: 2016-11-23 15:45:03
Original
1326 people have browsed it

Pagination in cakephp is still very simple. Let’s review the following example

1 Data table

  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`) 
)
Copy after login

2 In app/models/user.php, the code is:

 <?php 
class User extends AppModel{ 
    var $name = &#39;User&#39;; 
?>
Copy after login

3 In app/controllers/users_controller.php

function view_users(){
    
        $this->paginate = array(
        &#39;limit&#39; => 2
    );
    
   //users用于在前端页面中显示 
    $this->set(&#39;users&#39;, $this->paginate(&#39;User&#39;));
}
Copy after login

4 page template file
app/views/users/view_users.ctp

<?php
echo "<div class=&#39;page-title&#39;>Users</div>"; //title
//this &#39;add new user&#39; button will be used for the next tutorial
echo "<div style=&#39;float:right;&#39;>";
    $url = "add/";
    echo $form->button(&#39;Add New User&#39;, array(&#39;onclick&#39; => "location.href=&#39;".$this->Html->url($url)."&#39;"));
echo "</div>";
echo "<div style=&#39;clear:both;&#39;></div>";
if( sizeOf( $users ) > 0 ){ //check if there are user records returned
?>
<table>
    <tr>
    
   <!--第一个参数是表格列的label,第一个参数是排序中实际数据库的字段-->    
         <th style=&#39;text-align: left;&#39;><?php echo $paginator->sort(&#39;Firstname&#39;, &#39;firstname&#39;); ?></th>
        <th><?php echo $paginator->sort(&#39;Lastname&#39;, &#39;lastname&#39;); ?></th>
        <th><?php echo $paginator->sort(&#39;Email&#39;, &#39;email&#39;); ?></th>
        <th><?php echo $paginator->sort(&#39;Username&#39;, &#39;username&#39;); ?></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[&#39;User&#39;][&#39;firstname&#39;]}";
                echo "</td>";
                echo "<td>{$user[&#39;User&#39;][&#39;lastname&#39;]}</td>";
                echo "<td>{$user[&#39;User&#39;][&#39;email&#39;]}</td>";
                echo "<td>{$user[&#39;User&#39;][&#39;username&#39;]}</td>";
                echo "<td style=&#39;text-align: center;&#39;>";
                    //&#39;Edit&#39; and &#39;Delete&#39; link here will be used for our next tutorials
                    echo $html->link(&#39;Edit&#39;, array(&#39;action&#39;=>&#39;edit/&#39;.$user[&#39;User&#39;][&#39;id&#39;]), null, null);
                    echo " / ";
                    echo $html->link(&#39;Delete&#39;, array(&#39;action&#39;=>&#39;delete/&#39;.$user[&#39;User&#39;][&#39;id&#39;]), null, &#39;Are you sure you want to delete this record?&#39;);
                echo "</td>";
            echo "</tr>";
        }
    ?>
    </tr>
</table>
<?php
    //分页开始
    echo "<div class=&#39;paging&#39;>";
    //第一页
      echo $paginator->first(&#39;First&#39;);
    echo " ";
    
    //前一页
    if($paginator->hasPrev()){
        echo $paginator->prev(&#39;<<&#39;);
    }
    
    echo " ";
   //指定页数
    echo $paginator->numbers(array(&#39;modulus&#39; => 2)); 
    echo " ";
    
   
    if($paginator->hasNext()){ 
        echo $paginator->next(&#39;>>&#39;);
    }
    
    echo " ";
    //最后一页
    echo $paginator->last(&#39;Last&#39;);
    
    echo "</div>";
    
}else{ //if there are no records found, display this
    echo "<div class=&#39;no-records-found&#39;>No Users found.</div>";
}
?>
Copy after login



Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template