溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

yii框架搜索分頁modle怎么寫

發(fā)布時間:2021-08-30 14:53:33 來源:億速云 閱讀:83 作者:小新 欄目:開發(fā)技術

這篇文章主要為大家展示了“yii框架搜索分頁modle怎么寫”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“yii框架搜索分頁modle怎么寫”這篇文章吧。

控制器層

<?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 //每頁顯示條數(shù)
  ]);
  $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怎么寫”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

yii
AI