注意: 我form表单提交的地址明明是/blog-advanced/backend/web/index.php?r=sys/index
的路径,提交后直接给我变成/blog-advanced/backend/web/index.php
了,但是用post的方式提交路径和action的一样,难道是 get 方式提交不支持这样的 url 路径吗? 知情人士请回答 在你的表单里面加上一个隐藏域,再次提交时就会在URL中拼凑出r='sys/index'这一段了<input type="hidden" name="r" value="sys/index">
views:视图层 <?php use yii\helpers\Html; use yii\bootstrap\ActiveForm; use yii\helpers\Url; use yii\base; use\yii\widgets\LinkPager; ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <center> <body> <?php $form=ActiveForm::begin([ 'action'=>Url::toRoute(['showc']), 'method'=>'get', ]); echo '姓名:'.Html::input('text','username',$search);//可以写第三个值,相当于value保留值,但是添加成功不显示&username,能力强大的人可以解决一波 echo Html::submitButton(); ActiveForm::end();echo "<br/>"; ?> <!-- <form action="index.php?r=ceshi/showc" method="get">--> <!-- <input type="hidden" name="_csrf-frontend" value=""/>--> <!-- 请输入姓名<input type="text" name="search"><input type="submit" value="查询"/>--> <!-- </form>--> <table border="1"> <tr> <td>id</td> <td>用户名</td> <td>性别</td> <td>爱好</td> <td>学历</td> <td>操作</td> </tr> <?php foreach($date as $val){?> <tr> <td><?= $val['id']?></td> <td><?= $val['username']?></td> <td><?= $val['sex']?></td> <td><?= $val['hobby']?></td> <td><?= $val['xueli']?></td> <td><a href="?r=ceshi/desca&id=<?= $val['id']?>">详情</a></td> </tr> <?php }?> </table> <?= LinkPager::widget([ 'pagination' => $pages, 'nextPageLabel' => '下一页', 'prevPageLabel' => '上一页', 'firstPageLabel' => '首页', 'lastPageLabel' => '尾页', ]); ?> </body> </center> </html> controller:控制层public function actionShowc(){ $db=\Yii::$app->db; // $count=$db->createCommand("select COUNT(*) from ceshi")->queryScalar(); // $pages= new Pagination(['totalCount'=>$count,'pageSize'=>3]); // $date=$db->createCommand("select * from ceshi")->queryAll(); $user=empty($_GET['username'])?'':$_GET['username']; // print_r($user);die; if(!empty($user)){ $query = Ceshi::find()->where("username like '%$user%'"); }else{ $query = Ceshi::find(); } $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize'=>3]); $date = $query->offset($pages->offset) ->limit($pages->limit) ->all(); $xuel=$db->createCommand("select * from ceshixueli")->queryAll(); //把另一个表里的值循环添加到另一个表中 // print_r($date);die; foreach($date as &$val){ foreach($xuel as &$va){ if($val['xueli']==$va['id']){ $val['xueli']=$va['xueli']; } } } // print_r($date);die; return $this->render('showc',['date'=>$date,'search'=>$user,'pages'=>$pages]); }model:模型<?php namespace frontend\models; use Yii; use yii\base\Model; use yii\web\UploadedFile; use yii\captcha\Captcha; /** * Login form */ class Ceshi extends \yii\db\ActiveRecord { //模式必须把verifycode 加属性加上否则报错 // public $username; // public $userpwd; // public $userfile; // public $addtime; // public $verifyCode; // public $sex; // public $hobby; // public $xueli; /** * @inheritdoc */ public function rules() { return [ // username and password are both required [['username', 'userpwd'], 'required'], [['userfile'], 'file', 'extensions' => 'png, jpg', 'maxFiles' => 4], ['verifyCode', 'required'], ['verifyCode', 'captcha','captchaAction'=>'ceshi/captcha'], [['sex','hobby'], 'string'], [['sex','hobby','xueli'], 'required'], // rememberMe must be a boolean value // password is validated by validatePassword() // ['password', 'validatePassword'], ]; } public function attributeLabels() { return [ 'username' => '昵称', 'userpwd' => '密码', 'verifyCode' => '验证码', 'userfile' => '头像', 'addtime' => '有效期', 'sex' => '性别', 'hobby' => '爱好', 'xueli' => '学历', ]; }
yii框架所搜分页
最新推荐文章于 2017-11-02 20:24:40 发布