cakephp的分頁排序

巴扎黑
發布: 2016-11-23 15:45:03
原創
1268 人瀏覽過

cakephp中的分頁還是很簡單的,下面例子複習下 

1 資料表 

  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`) 
)
登入後複製

2 在app/models/user.php 中,程式碼為: 

 <?php 
class User extends AppModel{ 
    var $name = &#39;User&#39;; 
?>
登入後複製


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

4 頁面模版檔案中 
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>";
}
?>
登入後複製



相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!