choose table-column in search-dropdown

Im trying to make a search dropdown which search for different table-columns depending on what you choose in the dropdown.

The first choice show all rows with category_work_id = 1,2,3

the second choice show all rows with category_work_id = 4,5,6

the third choice show all rows with category_portfolio_id = 1,2,3

the fourth choice show all rows with category_portfolio_id = 4,5,6

all in the same table.

Any ideas? <_<

Hi isla, welcome to the forum.

  1. Add a virtual attribute to your model and declare it safe on search



public $searchChoice;

...

public function rules()

{

	return array(

		...

		array('searchChoice, ...', 'safe', 'on'=>'search'),

	);

}



  1. Declare search choice options and their texts in your model.



const CHOICE_ONE = 1;

const CHOICE_TWO = 2;

const CHOICE_THREE = 3;

const CHOICE_FOUR = 4;


public static $searchChoiceOptions = array(

    self::CHOICE_ONE => 'Work = 1,2,3',

    self::CHOICE_TWO => 'Work = 4,5,6',

    self::CHOICE_THREE => 'Portofolio = 1,2,3',

    self::CHOICE_FOUR => 'Portofolio = 4,5,6',

);



  1. Use the virtual attribute in your search form.



echo $form->dropDownList($model, 'searchChoice', YourModel::$searchChoiceOptions, $options);



  1. Construct the suitable criteria depending on the given searchChoice in your search() function.



public function search()

{

	$criteria=new CDbCriteria;


	$criteria->compare(...);

	...

	if ($this->searchChoice == self::CHOICE_ONE)

	{

	    $criteria->addInCondition(...);

	}

	else if ($this->searchChoice == self::CHOICE_TWO)

	{

	    $criteria->addInCondition(...);

	}

	...

	return new CActiveDataProvider(get_class($this), array(

		'criteria' => $criteria,

	));

}



[EDIT]

I failed to add ‘static’ keyword to $searchChoiceOptions in the original post … it was a bonehead.

When I make the changes and load the page it only loads 1/10 of the page, not including the layout or the data.

What does the $options variable do in the search-view?


echo $form->dropDownList($model, 'searchChoice', Swimmer::$searchChoiceOptions, $options);

You should have some error in the view file.

Maybe unmatched tags … missing closing/opening tags.

It is ‘htmlOptions’ of CActiveForm::dropDownList().

Look it up in the reference.

http://www.yiiframework.com/doc/api/1.1/CActiveForm/#dropDownList-detail

If you don’t want to set any options, you can omit it:




echo $form->dropDownList($model, 'searchChoice', Swimmer::$searchChoiceOptions);



I can’t find the problem in the view file, it must be in the model then i guess.

This is my _search view




<?php $form=$this->beginWidget('CActiveForm', array(

	'action'=>Yii::app()->createUrl($this->route),

	'method'=>'get',

)); ?>

	

	<div class="row">

		<?php echo $form->dropDownList($model, 'searchChoice', Swimmer::$searchChoiceOptions); ?>

	</div>

	

	<div class="row buttons">

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

	</div>


<?php $this->endWidget(); ?>



Should the constants and the searchChoiceOptions array be included in a function?

No, the constants and $searchChoiceOptions are to be declared in the model class.

I don’t see any problem in your _search view, either.

I guess your _search view is included in your main view by renderPartial, isn’t it?

What about the main view? Isn’t there any mismatching tag?

[EDIT]

Ah, sorry. I forgot to add the ‘static’ keyword. :-[




public static $searchChoiceOptions = array(

    self::CHOICE_ONE => 'Work = 1,2,3',

    self::CHOICE_TWO => 'Work = 4,5,6',

    self::CHOICE_THREE => 'Portofolio = 1,2,3',

    self::CHOICE_FOUR => 'Portofolio = 4,5,6',

);



Thanks alot! Working now ;D