naveed1
(ndk)
February 25, 2014, 5:22pm
1
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.
alirz23
(Ali Raza)
February 25, 2014, 5:38pm
2
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
alirz23
(Ali Raza)
February 25, 2014, 5:39pm
3
if you don’t understand paste your code here I will sort it out for you
JANAB
naveed1
(ndk)
February 25, 2014, 5:54pm
4
Here is the code.
<div class="row search-bar">
<div class="col-md-4">
<select class="form-control">
<option>Select a Category</option>
<?php
$criteria = new CDbCriteria();
$data = Categories::model()->findAll();
foreach ($data as $model) {
?>
<option><?php echo $model->name; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-8">
<?php SearchAction::renderInputBox(); ?>
</div>
</div>
alirz23
(Ali Raza)
February 25, 2014, 6:37pm
5
// 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(
'class'=>'form-control',
'prompt'=>'Select a Category'
));?>
</div>
<div class="col-md-8">
<?php echo CHtml::textField('q', ''); ?>
</div>
<?php echo CHtml::button('Search'); ?>
<?php echo CHtml::endForm(); ?>
</div>
// in your controller add a action
public function actionSearch($q, $category)
{
$criteria=new CDbCriteria;
$criteria->compare('name', $q, true);
$criteria->compare('category_id',$category);
$result = Item::model()->findAll($criteria);
$this->render('search', array('result'=>$result));
}
// in your search result page
foreach($result as $row) {
echo $row->name;
}