【问题标题】:yii framework: how to make search result empty in search form?yii框架:如何在搜索表单中使搜索结果为空?
【发布时间】:2014-04-04 09:58:34
【问题描述】:

您好,我创建了一个带有输入框和按钮的单独搜索表单。

在我的模型中,我想按类别搜索产品...

但问题是当输入框为空并单击搜索按钮时,它会显示数据库表中的所有条目..

控制器代码是-

class AddController extends Controller
{
public function actionAddsearch()
{
$model_form = new Add("search");
    $attributes = Yii::app()->getRequest()->getPost("Add");
if(!is_null($attributes))
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
}

  $this->render("searchResults", array(
 "model" => $model_form, 
 "models" => $model_form->searchAdd(),

 ));

 }

型号代码--

class Add extends CActiveRecord
 {
public function searchAdd()
{
        $criteria = new CDbCriteria();

        $criteria->compare("addname", $this->category, TRUE, "OR");
        $criteria->compare("category", $this->category, TRUE, "OR");
        return Add::model()->findAll($criteria);    
}

查看代码-addsearch.php

   <div class="search-bar">
<?php
   $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
"action"=>$this->createUrl("add/addsearch"),
'type'=>'search',
)
); 

     echo $form->textFieldRow($model,'category');
     echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
     $this->widget('bootstrap.widgets.TbButton',array(
        'buttonType'=>'submit',
        'type'=>'success',
        'size'=>'large',
        'label'=>'Search For Products   ',
    ));

  $this->endWidget(); 


  ?>
 </div>

searchResults.php

<?php 
  echo "<h4>Results for Your Search</h4>";

   foreach($models as $model):

    $this->beginWidget('bootstrap.widgets.TbDetailView',array(
    'type'=>'bordered condensed',
    'data' => array(
  'Shop Name' =>CHtml::link(CHtml::encode($model->addname),array('add/view','id'=> $model->addid)),
'Category' => $model->category
),
'attributes' => array(array('name' => 'Shop Name', 'label' => 'Name of Shop','value'=>CHtml::link(CHtml::encode($model->addname),
                array('add/view','id'=>$model->addid)),'type'=>'raw'),
array('name' => 'Category', 'label' => 'Category of Shop'),

),
)
);

echo "<br><hr><br>";
$this->endWidget();
 endforeach;

 ?>

代码有什么问题??

当文本框为空时,我不想显示任何产品.. 在此先感谢

【问题讨论】:

  • 该死的,有谁知道什么是“代码格式化”... =\

标签: php yii


【解决方案1】:

改变这个

if(!is_null($attributes))
{
     $model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
}

$this->render("searchResults", array(
      "model" => $model_form, 
      "models" => $model_form->searchAdd(),
      ));

if($attributes)
{
     $model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
     $this->render("searchResults", array(
           "model" => $model_form, 
           "models" => $model_form->searchAdd(),
           ));
}

【讨论】:

    【解决方案2】:

    1) 'catery' 属性在搜索场景中是否安全? (在规则中)

    2) 类别是表模式的一个属性?

    【讨论】:

    • 是的,它在搜索场景中是安全的,它是表的属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多