Search In Yii Using Different Categories

I am using esearch extension in my site and it gives the good result.

I have to models ITEMS and Itemcategories model. I want to use the search feature via item category. i.e. search the items related to the selected category only. I have already generate the categories dropdown list

If anyone have any idea then plz share.

Thanks in advance.

This should be very straight forward since you already have a relationship/association in between item and category

in you criteria select all the items where category_id is equal to category_id from search box

$criteria = new CDbCriteria;

$criteria->compare('category_id', $_GET['category']); // use $_POST['category'] if you using post method to search

if you don’t understand paste your code here I will sort it out for you


Here is the code.

<div class="row search-bar">

        &lt;div class=&quot;col-md-4&quot;&gt;

            &lt;select class=&quot;form-control&quot;&gt;

                &lt;option&gt;Select a Category&lt;/option&gt;


                &#036;criteria = new CDbCriteria();

                &#036;data = Categories::model()-&gt;findAll();

                foreach (&#036;data as &#036;model) {


                    &lt;option&gt;&lt;?php echo &#036;model-&gt;name; ?&gt;&lt;/option&gt;  

                &lt;?php } ?&gt;



        &lt;div class=&quot;col-md-8&quot;&gt;

            &lt;?php SearchAction::renderInputBox(); ?&gt;



// search form

<div class="row search-bar">

	<?php echo CHtml::beginForm(array('site/search', 'get')); ?>

		<div class="col-md-4">

		<?php echo CHtml::dropDownList('post', '', CHtml::listData(Category::model()->findAll(), 'id', 'name'), array(


				'prompt'=>'Select a Category'



		<div class="col-md-8">

			<?php echo CHtml::textField('q', ''); ?>


		<?php echo CHtml::button('Search'); ?>


	<?php echo CHtml::endForm(); ?>


// in your controller add a action 

public function actionSearch($q, $category)


		$criteria=new CDbCriteria;

		$criteria->compare('name', $q, true);


		$result = Item::model()->findAll($criteria);

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



// in your search result page

foreach($result as $row) {

  echo $row->name;