分页:
在cakephp中很多都是通过components来完成的,想上传 分页,分页已经集成到了cakephp中,直接用就可以了,
在controller加入
var $paginate = array( 'limit' => 3, 'order' => array('created' => 'desc') );/**
* describle search user by username * @param string $username * @return array() * @access public */ public function search() { //form提交获取查询的key if (isset($this->params['form']['blb_searchname']) && $this->params['form']['blb_searchname']) { $strFind = $this->params['form']['blb_searchname']; }else{ //通过url获取参数和页码 $strFindurl = isset($this->params['named']['lang']) ? $this->params['named']['lang'] : ''; $strFind = urldecode($strFindurl); } //查询数据库 $arrUserInfo = $this->paginate('User',array('username like' => '%'.$strFind.'%')); //返回信息 $this->set('strfind',urlencode($strFind)); $this->set('arruser',$arrUserInfo); }
views/
<table> <tr> // $paginator->sort("ID",'id')可以在页面中点击按该字段排序 <th><?php echo $paginator->sort('ID', 'id'); ?></th> <th><?php echo $paginator->sort('Username', 'username'); ?></th> <th><?php echo $paginator->sort('Pw', 'password'); ?></th> <th><?php echo $paginator->sort('Email', 'email'); ?></th> <th><?php echo $paginator->sort('Firstname', 'first_name'); ?></th> </tr> <?php foreach($arruser as $v): ?> <tr> <td><?php echo $v['User']['id']; ?> </td> <td><?php echo $v['User']['username']; ?> </td> <td><?php echo $v['User']['password']; ?> </td> <td><?php echo $v['User']['email']; ?> </td> <td><?php echo $v['User']['first_name']; ?> </td> </tr> <?php endforeach; ?> </table> <hr/> <?php //设置传递的查询参数 $paginator->options(array('url'=>array_merge(array('lang'=>$strfind),$this->passedArgs))); echo $paginator->first('首页'); echo $paginator->prev('上一页'); echo $paginator->numbers(); echo $paginator->next('下一页'); echo $paginator->last('尾页'); ?>
//session
cakephp的session是封装了php中的seesion不用打开就可以用,
除了常用的$this->Session->writer() read() delter() destory() 方法外还有
$this->Session->setFlash('aaa');表示把数据aaa刷新到seesion中只保留第一次加载,在页面中可以通过
echo $session->Flash();来显示