没有gridview的搜索结果不会显示在Yii2中

时间:2016-07-22 17:11:49

标签: search yii2

我试图组织来自2个领域的搜索表单。但它没有显示结果。它只是留在网站/索引。请帮帮我

来自siteController.php的

代码

public function actionSearch()
{
    $driver = new Driver();

    if ($driver->load(Yii::$app->request->post())) {

        $driver = Driver::find()
            ->where(['from' => $driver->from])
            ->andWhere(['to' => $driver->to])
            ->all();

        return $this->render('search', ['driver' => $driver]);
    }
    else {
        throw new NotFoundHttpException('Input data not found' );
    }
}

来自models / Driver.php的代码

<?php

namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use Yii;
/**
 * This is the model class for table "driver".
 *
 * @property string $id
 * @property string $from
 * @property string $to
 * @property string $data
 * @property string $about
 * @property string $car
 */
class Driver extends \yii\db\ActiveRecord{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'driver';
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
//            [['from', 'to', 'data', 'about', 'car'], 'required'],
            [['about'], 'string'],
            [['from', 'to', 'data', 'car'], 'string', 'max' => 255],
        ];
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'from' => 'From',
            'to' => 'To',
            'data' => 'Data',
            'about' => 'About',
            'car' => 'Car',
        ];
    }

}

来自site / index.php的代码

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use app\models\Driver;
/* @var $driver app\models\Driver */
?>
<?php
$driver = new Driver;
?>

      <div class="row">

          <div>
              <?php $form = ActiveForm::begin([
              'action' => ['site/search']
          ]); ?>

              <div class="row">
                  <div class="col-xs-5">
                      <?= $form->field($driver, 'from')->label('От')->textInput(['class' => 'input form-control']) ?>
                  </div>
                  <div class="col-xs-5">
                      <?= $form->field($driver, 'to')->label('До')->textInput(['class' => 'input form-control']) ?>
                  </div>
                  <div class="col-xs-2" align="left" style="margin-top: 30px">
                      <input type="image" src="<?= \Yii::getAlias('@web/images/button_search.png')?>" class="icon_button" alt="Поиск" >
                  </div>
              </div>

              <?php ActiveForm::end(); ?>
          </div>

      </div>

来自site / search.php的代码

<?php
use yii\widgets\LinkPager;

$this->title = "Поиск";

$this->registerMetaTag([
    'name' => 'description',
    'content' => 'driver',
]);
$this->registerMetaTag([
    'name' => 'keywords',
    'content' => 'driver',
])
?>

<?php if (!$driver) { ?>
    <p>Ничего не найдено</p>
<?php } else { ?>


<?php foreach ($driver as $one){
        $from = $one -> from;
        $to = $one -> to;
        $data = $one -> data;
        ?>

            <?php return $this -> render('found_drivers', [
            'from' => $from,
            'to' => $to,
            'data' => $data
        ]); ?>
    <?php  }?>
<?php } ?>

和site / found_drivers.php

<div class="one">
    <h2><?=$id?></h2>
    <hr />
    <table class="">
        <tr>
            <td>
                <p><?=$from?></p>
            </td>
            <td class="right">

            </td>
            <td class="center">
                <p><?=$to?></p>
            </td>
        </tr>
    </table>
    <div class="">
        <?=$data?> <br>
        <div class="clear"></div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

您在site/index中有一个表单,并且它已提交到同一页面。如果您想在另一个操作中处理表单数据,则需要将其添加到表单中:

$form = ActiveForm::begin([
    'action' => ['site/search']
]);

更多信息here

正如@makie在评论中所说,您应该使用render()代替include()来呈现部分观看次数。