當前位置:才華齋>計算機>php語言>

cakephp的分頁排序

php語言 閱讀(7.01K)

PHP學習過程中你是否感到困惑?以下是本站小編精心為大家整理的PHP教程,希望對大家有所幫助!更多內容請關注應屆畢業生網!

cakephp的分頁排序

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

1 資料表

123456789CREATETABLEIFNOTEXISTS`users`(`id`int(11)NOTNULLAUTO_INCREMENT,`firstname`varchar(32)NOTNULL,`lastname`varchar(32)NOTNULL,`email`varchar(32)NOTNULL,`username`varchar(32)NOTNULL,`password`varchar(32)NOTNULL,PRIMARYKEY(`id`))

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

1234<?phpclassUserextendsAppModel{var$name='User';?>

3 app/controllers/users_中

123456789functionview_users(){$this->paginate=array('limit'=>2);//users用於在前端頁面中顯示$this->set('users',$this->paginate('User'));}

4 頁面模版檔案中

app/views/users/view_

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273<?phpecho"<pclass='page-title'>Users</p>";//title//this'addnewuser'buttonwillbeusedforthenexttutorialecho"<pstyle='float:right;'>";$url="add/";echo$form->button('AddNewUser',array('onclick'=>"='".$this->Html->url($url)."'"));echo"</p>";echo"<pstyle='clear:both;'></p>";if(sizeOf($users)>0){//checkifthereareuserrecordsreturned?><table><tr><!--第一個引數是表格列的label,第一個引數是排序中實際資料庫的欄位--><thstyle='text-align:left;'><?phpecho$paginator->sort('Firstname','firstname');?></th><th><?phpecho$paginator->sort('Lastname','lastname');?></th><th><?phpecho$paginator->sort('Email','email');?></th><th><?phpecho$paginator->sort('Username','username');?></th><th>Action</th></tr><tr><?phpforeach($usersas$user){//wewilloopthroughtherecordstoDISPLAYDATAecho"<tr>";echo"<td>";echo"{$user['User']['firstname']}";echo"</td>";echo"<td>{$user['User']['lastname']}</td>";echo"<td>{$user['User']['email']}</td>";echo"<td>{$user['User']['username']}</td>";echo"<tdstyle='text-align:center;'>";//'Edit'and'Delete'linkherewillbeusedforournexttutorialsecho$html->link('Edit',array('action'=>'edit/'.$user['User']['id']),null,null);echo"/";echo$html->link('Delete',array('action'=>'delete/'.$user['User']['id']),null,'Areyousureyouwanttodeletethisrecord?');echo"</td>";echo"</tr>";}?></tr></table><?php//分頁開始echo"<pclass='paging'>";//第一頁echo$paginator->first('First');echo"";//前一頁if($paginator->hasPrev()){echo$paginator->prev('<<');}echo"";//指定頁數echo$paginator->numbers(array('modulus'=>2));echo"";if($paginator->hasNext()){echo$paginator->next('>>');}echo"";//最後一頁echo$paginator->last('Last');echo"</p>";}else{//iftherearenorecordsfound,displaythisecho"<pclass='no-records-found'>NoUsersfound.</p>";}?>