我目前正在 Yii 中编写一个应用程序,我想在标题中创建一个搜索框(在导航菜单中或在其上方)。这个搜索框应该能够从站点的每个部分访问,并且应该能够在不同表格的不同列上进行搜索。
我不知道如何做到这一点,网上几乎所有关于它的帖子都涉及使用网格视图或扩展(如果可能的话,我想创建没有扩展的代码)。
您对搜索代码的外观有什么想法(我应该在哪个控制器中放入什么等)?
- 编辑 -
我仍然不知道该怎么做,但无论如何我会告诉你我现在拥有的东西。这并不多,而且很明显我缺少一些代码。
/view/layout/main.php:
<?php echo CHtml::form(Yii::app()->createUrl('product/search'), 'get') ?>
<?php echo CHtml::textField('search_key','',array('placeholder' => 'Search')); ?>
<?php echo CHtml::submitButton('Go'); ?>
<?php echo CHtml::endForm() ?>
/view/product/search.php:
//Not sure by any means what to write here, but I'll like a list view populated with the search results
/控制器/productController.php
/**
* Search through model.
*/
public function actionSearch()
{
if(isset($_GET['search_key'])){
$search = $_GET['search_key'];
$model->name = $search;
}
$this -> render('search', array(
'model' => $model,
));
}
/models/Product.php
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}