Deram
Deram

Reputation: 5

Yii Search in navigation menu

I'm currently writing an application in Yii in which i want to create a search box in the header (either in the navigation menu or just above it). This search box should be able to be accessed from every part of the site, and it should be able to search on different columns of different tables.

I have no idea on how to do this, and almost all posts on the web about it involves using the grid-view, or an extension (I'd like to create the code without an extension if that's possible).

Do you have an idea to how the search code should look (what i should put in which controller etc.)?

-- EDIT --

I still don't know how to do this but I will show you what I have at the moment anyway. It is not much and it is pretty obvious where i am missing some code.

/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

/controllers/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,
    ));
}

Upvotes: 0

Views: 4258

Answers (1)

msoa
msoa

Reputation: 1349

You can perform like this:

Find #mainmeu in /protected/views/layout/main.php

<div id="mainmenu">
    <div style="width: 80%;float: right">
        <?php $this->widget('zii.widgets.CMenu',array(
            'items'=>array(
                array('label'=>'home', 'url'=>array('/site/index')),
                array('label'=>'about', 'url'=>array('/site/page', 'view'=>'about')),
                array('label'=>'contact', 'url'=>array('/site/contact')),
            ),
        )); ?>
    </div>

    <div style='float: left;direction: rtl; color: #ffffff; margin: 5px 0 0 5px; font-size: 13px'>
        <?php echo CHtml::form(Yii::app()->createUrl('product/search'),'get') ?>
            <?php echo CHtml::textField('search_key', 'search') ?>
            <?php echo CHtml::submitButton(); ?>
        <?php echo CHtml::endForm() ?>
    </div>
</div><!-- mainmenu -->

Edit:

/models/Product.php:

public function search()
{
    $criteria=new CDbCriteria;
    $criteria->compare('name',$this->name,true);
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

/controllers/productController.php -> actionSearch():

public function actionSearch()
{
    $model = new Product('search');
    $model->unsetAttributes();
    if(isset($_GET['search_key'])) 
        $model->name = $_GET['search_key'];     

    $this -> render('search', array(
        'model' => $model,
    ));
}

/view/product/search.php:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'product-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
        'name',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

Upvotes: 4

Related Questions