Rumah > php教程 > PHP开发 > yii框架搜索分页modle写法

yii框架搜索分页modle写法

高洛峰
Lepaskan: 2016-12-23 17:10:00
asal
1177 orang telah melayarinya

控制器层

<?PHP
namespace frontend\controllers;
header(&#39;content-type:text/html;charset=utf-8&#39;);
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(&#39;goods_name&#39;);
  //分页
  $test=new Goods();  //实例化model模型
  $arr=$test->find()->where([&#39;like&#39;,&#39;goods_name&#39;,"$w"]); //加上搜索的条件where
  $pages = new Pagination([
    &#39;totalCount&#39; => $arr->count(),
    &#39;pageSize&#39;  => 4 //每页显示条数
  ]);
  $models = $arr->offset($pages->offset)
    ->limit($pages->limit)
    ->all();
  return $this->render(&#39;goodslist&#39;, [ //前台的页面
    &#39;data&#39; => $models,
    &#39;pages&#39; => $pages,
    &#39;where&#39; =>$w   //把搜索的条件显示到前面
  ]);
     
  }
}
Salin selepas log masuk

视图层

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>商品的展示列表</title>
</head>
<body>
<?php
$form=ActiveForm::begin([
  &#39;action&#39;=>Url::toRoute([&#39;goods/goodslist&#39;]),
  &#39;method&#39;=>&#39;get&#39;,
]);
echo &#39;搜索&#39;," ",Html::input(&#39;text&#39;,&#39;goods_name&#39;,$where);
// echo &#39;年龄&#39;," ",Html::input(&#39;text&#39;,&#39;age&#39;,$where[&#39;age&#39;]);
echo Html::submitButton(&#39;搜索&#39;);
ActiveForm::end();
?>
  <table>
  <?php foreach ($data as $key => $val): ?>
    <tr>
      <td>商品名称是:<?= $val[&#39;goods_name&#39;]?></td>
    </tr>
  <?php endforeach ?>
  </table>
</body>
</html>
<?php
// use yii\widgets\LinkPager;
echo LinkPager::widget([
  &#39;pagination&#39; => $pages,
  &#39;nextPageLabel&#39; => &#39;下一页&#39;, 
  &#39;prevPageLabel&#39; => &#39;上一页&#39;, 
]);
?>
Salin selepas log masuk

model层

<?php
namespace frontend\models;
use Yii;
class Goods extends \yii\db\ActiveRecord
{
}
Salin selepas log masuk

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

更多yii框架搜索分页modle写法相关文章请关注PHP中文网!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan