在PHP学习过程中你是否感到困惑?以下是本站小编精心为大家整理的PHP教程,希望对大家有所帮助!更多内容请关注应届毕业生网!
cakephp中的.分页还是很简单的,下面例子复习下
1 数据表
123456789 |
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/ 中,代码为:
1234 |
<?php class User extends AppModel{
var $name = 'User' ; ?> |
3 app/controllers/users_中
123456789 | function view_users(){
$this ->paginate= array (
'limit' =>2
);
//users用于在前端页面中显示
$this ->set( 'users' , $this ->paginate( 'User' )); } |
4 页面模版文件中
app/views/users/view_
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 | <?php echo "<pclass='page-title'>Users</p>" ; //title //this'addnewuser'buttonwillbeusedforthenexttutorial echo "<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;' ><?php echo $paginator ->sort( 'Firstname' , 'firstname' );?></th>
<th><?php echo $paginator ->sort( 'Lastname' , 'lastname' );?></th>
<th><?php echo $paginator ->sort( 'Email' , 'email' );?></th>
<th><?php echo $paginator ->sort( 'Username' , 'username' );?></th>
<th>Action</th>
</tr>
<tr>
<?php
foreach ( $users as $user ){ //wewilloopthroughtherecordstoDISPLAYDATA
echo "<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'linkherewillbeusedforournexttutorials
echo $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,displaythis
echo "<pclass='no-records-found'>NoUsersfound.</p>" ; } ?> |