1. 程式人生 > >yii框架查詢分頁展示

yii框架查詢分頁展示

class ChaxunController extends Controller
{
    public function actionChaxun(){
        $query= new Query();
        $query->from('goods');
        $where=Yii::$app->request->get()?Yii::$app->request->get():"";
        if(!empty($where['gname'])){
            //$query->andFilterWhere('like','gname',$where['gname']);
            $query->andWhere(['like','gname',$where['gname']]);
        }
        if(isset($where['sort1'])&& $where['sort1']!=''){
            $query->andWhere(['>=','sort',$where['sort1']]);
        }
        if(isset($where['sort2'])&& $where['sort2']!=''){
            $query->andWhere(['<=','sort',$where['sort2']]);
        }


        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->setPageSize(3);
       $data=$query->offset($pagination->offset)->limit($pagination->limit)->all();
        //print_r($data);exit;
        return $this->render('show',['data'=>$data,'where'=>$where,'pagination'=>$pagination]);
    }
}

<?php
//use yii\bootstrap\ActiveForm;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;
?>

<?php
    $form=ActiveForm::begin([
        'action'=>Url::toRoute('chaxun'),
        'method'=>'get'
    ]);
    echo Html::input('text','gname',$where['gname']);
    echo Html::input('text','sort1',$where['sort1']);
    echo '-'.Html::input('text','sort2',$where['sort2']);
    echo Html::submitButton();
   ActiveForm::end();
?>
<table class="table">
    <?php foreach($data as $v): ?>
    <tr>
        <td><?php echo $v['gid']; ?></td>
        <td><?php echo $v['gname']; ?></td>
        <td><?php echo $v['sort']; ?></td>
    </tr>
    <?php endforeach;?>
    <?php
    echo LinkPager::widget([
        'pagination' => $pagination,

    ]);
    ?>
</table>