>  기사  >  백엔드 개발  >  cakephp 페이징 정렬

cakephp 페이징 정렬

巴扎黑
巴扎黑원래의
2016-11-23 15:45:031314검색

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에서 코드는 다음과 같습니다. is:

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

app/controllers/users_controller.php의 3

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



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.