yii分页

来源:互联网 发布:vb应用程序 传入参数 编辑:程序博客网 时间:2024/06/06 10:55
<?php


namespace app\models;
use yii;
use yii\base\Model;
use yii\web\UploadedFile;



//M层
/**
 * UploadForm is the model behind the upload form.
 */
class Brand extends Model
{
   public  $table='brand';//$table 是表名
  public function selectPage($p){
 $size=4;
 $page=($p-1)*$size;
 $command = Yii::$app->db->createCommand('SELECT count(*) as num FROM '.$this->table);
 $posts = $command->queryAll();
 $num=$posts[0]['num'];


 $end=ceil($num/$size);


 $xia=$p+1>=$end?$end:$p+1;
 $shang=$p-1<=1?1:$p-1;


 $comm= Yii::$app->db->createCommand('SELECT * FROM '.$this->table." limit $page,$size");
 $pos = $comm->queryAll();
 $arr['xia']=$xia;
 $arr['end']=$end;
 $arr['shang']=$shang;
 $data['info']=$pos;
 $data['page']=$arr;


 return $data;
}


}

//C层

 public function actionBrand_list(){
        $p=isset($_GET['p'])?$_GET['p']:"1";
        $model=new Brand;
       $data=$model->selectPage($p);
        
        return $this->render('brand_list',['info'=>$data['info'],'page'=>$data['page']]); 
    }

//视图层

  <a href="../index.php?r=brand/brand_list&p=1">首页</a>
          <a href="../index.php?r=brand/brand_list&p=<?php echo $page['shang']?>">上一页</a>
          <a href="../index.php?r=brand/brand_list&p=<?php echo $page['xia']?>">下一页</a>
          <a href="../index.php?r=brand/brand_list&p=<?php echo $page['end']?>">最末页</a>







原创粉丝点击