Maison  >  Article  >  développement back-end  >  cakephp的分页排序

cakephp的分页排序

巴扎黑
巴扎黑original
2016-11-23 15:45:031270parcourir

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;; 
?>

3  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;));
}

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>";
}
?>



Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:PHP mysql 事务处理实例Article suivant:PHP7参数、数组和Zvals