欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

yii框架搜索分页modle写法

程序员文章站 2024-04-01 19:08:22
控制器层

控制器层

<?php
namespace frontend\controllers;
header('content-type:text/html;charset=utf-8');
use yii;
use yii\base\invalidparamexception;
use yii\web\badrequesthttpexception;
use yii\web\controller;
use yii\filters\verbfilter;
use yii\filters\accesscontrol;
use common\models\loginform;
use frontend\models\passwordresetrequestform;
use frontend\models\resetpasswordform;
use frontend\models\signupform;
use frontend\models\contactform;
use frontend\models\goods; //加载jidian 表的model
use yii\data\pagination; //yii框架中使用分页
use frontend\web\myclass\qrcode;//加载生成二维码类
/**
 * 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 yii\widgets\activeform;
use yii\helpers\url;
use yii\helpers\html;
use yii\widgets\linkpager;
?>
<!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 '搜索',"&nbsp",html::input('text','goods_name',$where);
// echo '年龄',"&nbsp",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 yii\widgets\linkpager;
echo linkpager::widget([
  'pagination' => $pages,
  'nextpagelabel' => '下一页', 
  'prevpagelabel' => '上一页', 
]);
?>

model层

<?php
namespace frontend\models;
use yii;
class goods extends \yii\db\activerecord
{
}

以上所述是小编给大家介绍的yii框架搜索分页modle写法,希望对大家有所帮助