yii框架搜索分頁modle寫法
更新時間:2016年12月19日 11:02:49 作者:CZY_1214
這篇文章主要介紹了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 '搜索'," ",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 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寫法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
PHP獲取遠(yuǎn)程http或ftp文件的md5值的方法
這篇文章主要介紹了PHP獲取遠(yuǎn)程http或ftp文件的md5值 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
Laravel事件系統(tǒng)實現(xiàn)瀏覽量的統(tǒng)計
Laravel的事件系統(tǒng)提供了一種簡單而強大的方式來實現(xiàn)瀏覽量的統(tǒng)計,通過創(chuàng)建瀏覽事件和事件監(jiān)聽器,以及在合適的地方觸發(fā)事件,我們可以輕松地實現(xiàn)網(wǎng)頁瀏覽量的統(tǒng)計功能,本文將介紹如何使用Laravel的事件系統(tǒng)來實現(xiàn)瀏覽量的統(tǒng)計2024-03-03
全世界最小的php網(wǎng)頁木馬一枚 附PHP木馬的防范方法
php網(wǎng)頁木馬代碼,大家可以看下自己的網(wǎng)站里面是不是有這樣的代碼,注意網(wǎng)站安全用mcafee限制w3wp.exe生成php或者asp文件。并在php.ini中設(shè)置一下。2009-10-10

