入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

YII框架的多条件查询

创建时间:2016-12-13 投稿人: 浏览次数:517

控制器:

<?php
namespace frontendcontrollers;

use Yii;
use yiiaseInvalidParamException;
use yiiwebBadRequestHttpException;
use yiiwebController;
use yiifiltersVerbFilter;
use yiifiltersAccessControl;
use yiidbActionIndex;
use commonmodelsLoginForm;
use frontendmodelsPasswordResetRequestForm;
use frontendmodelsResetPasswordForm;
use frontendmodelsSignupForm;
use frontendmodelsContactForm;
use frontendmodelsSearch;
use yiidataPagination;
/**
 * Site controller
 */
class SearchController extends Controller
{
    //展示数据
    public function actionShow()
    {
        $where = Yii::$app->request->get();
        $query = new yiidbQuery();
        $query->from("search");
        //判断where条件
        if(!empty($where["mold"])){
            $query->andWhere(["mold"=>$where["mold"]]);
        }
        if(!empty($where["grade"])){
            $query->andWhere(["grade"=>$where["grade"]]);
        }
        $data = $query->from("search")->all();
        // return $this->render("show",["data"=>$data]);
        $pages = new  Pagination([
            //"totalCount" => $countQuery->count(),
            "totalCount" => $query->count(),
            "pageSize"   => "3"  //每页显示条数
        ]);
                     $arr = $query->offset($pages->offset)->limit($pages->limit)->all();
                     return $this->render("show",["data"=>$data,"where"=>$where,"pages"=>$pages]);
    }
}



视图层:

<?php
/* @var $this yiiwebView */

use yiihelpersHtml;
use yiihelpersUrl;
use yiiaseWidget;

use yiiwidgetsActiveForm;
use yiiwidgetsLinkPager;
$this->title = "多条件查询";
?>
<h1><?= Html::encode($this->title) ?></h1>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>


<?php
 $form = ActiveForm::begin([
            "action"=>Url::toRoute(["search/show"]),
            "method"=>"get",
    ]);             
echo "公司类型:".Html::input("text","mold");
echo "公司等级:".Html::input("text","grade");
echo Html::submitButton("搜索",["class"=>"btn btn-primary"]);

ActiveForm::end();
?>
    <table class="table">
        <tr>
            <th>ID</th>
            <th>公司名称</th>
            <th>公司类型</th>
            <th>金额</th>
            <th>公司等级</th>
            <th>还款方式</th>
            <!-- <th>操作</th> -->
        </tr>
        <?php foreach ($data as $key => $value): ?>
            <tr>
                <td><?php echo $value["id"]?></td>
                <td><?php echo $value["company"]?></td>
                <td><?php echo $value["mold"]?></td>
                <td><?php echo $value["money"]?></td>
                <td><?php echo $value["grade"]?></td>
                <td><?php echo $value["mode"]?></td>
                <!-- <td><a href="?r=search/show&id=<?php //echo $value["id"]?>">删除</a></td> -->
            </tr>
        <?php endforeach ?>
    </table>
    <?php
    echo LinkPager::widget([
    "pagination"=>$pages,
    "nextPageLabel"=>"下一页",
    "firstPageLabel"=>"首页",
    ])
    ?>
</body>
</html>


我试了一下分页,出现了问题。

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像