当前位置:首页 > PHP教程 > PHP常见问题

yii框架搜索分页modle写法

控制器层

<?php namespace frontendcontrollers; header('content-type:text/html;charset=utf-8'); use yii; use yiibaseinvalidparamexception; use yiiwebbadrequesthttpexception; use yiiwebcontroller; use yiifiltersverbfilter; use yiifiltersaccesscontrol; use commonmodelsloginform; use frontendmodelspasswordresetrequestform; use frontendmodelsresetpasswordform; use frontendmodelssignupform; use frontendmodelscontactform; use frontendmodelsgoods; //加载jidian 表的model use yiidatapagination; //yii框架中使用分页 use frontendwebmyclassqrcode;//加载生成二维码类 /** * site controller */ class goodscontroller extends controller { public $enablecsrfvalidation = false; //商品展示列表 public function actiongoodslist() { //接收过来搜索的条件 $w=yii::$app->request->get('goods_name'); //分页 $test=new goods(); //实例化model模型 $arr=$test->find()->where(['like','goods_name',"$w"]); //加上搜索的条件where $pages = new pagination([ 'totalcount' => $arr->count(), 'pagesize' => 4 //每页显示条数 ]); $models = $arr->offset($pages->offset) ->limit($pages->limit) ->all(); return $this->render('goodslist', [ //前台的页面 'data' => $models, 'pages' => $pages, 'where' =>$w //把搜索的条件显示到前面 ]); } }

视图层

<?php use yiiwidgetsactiveform; use yiihelpersurl; use yiihelpershtml; use yiiwidgetslinkpager; ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>商品的展示列表</title> </head> <body> <?php $form=activeform::begin([ 'action'=>url::toroute(['goods/goodslist']), 'method'=>'get', ]); echo '搜索'," ",html::input('text','goods_name',$where); // echo '年龄'," ",html::input('text','age',$where['age']); echo html::submitbutton('搜索'); activeform::end(); ?> <table> <?php foreach ($data as $key => $val): ?> <tr> <td>商品名称是:<?= $val['goods_name']?></td> </tr> <?php endforeach ?> </table> </body> </html> <?php // use yiiwidgetslinkpager; echo linkpager::widget([ 'pagination' => $pages, 'nextpagelabel' => '下一页', 'prevpagelabel' => '上一页', ]); ?>

model层

<?php namespace frontendmodels; use yii; class goods extends yiidbactiverecord { }

以上所述是小编给大家介绍的yii框架搜索分页modle写法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对硕编程网站的支持!



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐