I am using Yii framework, I want to do some search filtering, but I am getting wrong results in some cases as follow:
I have 2 check-boxes: Canada and united states, if I check Canada I will get all the results related to Canada only, while if I check united states I will get all the results in the db regardless its related to united states or not, and this bug is absolutely happen because united states string is of 2 parts so it need to be in quotations. here is my code:
the view page:
echo '<div class="checkbox"><label>'.
CHtml::checkBox($m2->tag, false, array('value'=>"$m2->tag")).$m2->tag
.'</label></div>';
the controller:
$c = new CDbCriteria();
$c->order = "idJob DESC";
$model = Jobs::model()->findAll($c);
$model2 = Tags::model()->findAll();
$lcr = "";
$tag="";
foreach($model2 as $m2){
if(isset($_POST[$m2->tag])){
$tag = $_POST[$m2->tag];
if($m2->category=='Location')
$lcr[]= $tag;
}
}
if($lcr!="")
$c->addInCondition('location', $lcr, 'AND');
$model = Jobs::model()->findAll($c);
OK it sounds like I found a solution for this problem and its working correctly, here is the solution:
in the controller:
foreach($model2 as $m2){
if (strpos($m2->tag, ' ') !== FALSE)
$m2->tag = str_replace(" ","_",$m2->tag);
if(isset($_POST["$m2->tag"])){
....
So, as you see in the code, if the tag
contain any white space it will be replaced by _
, and in the HTML the id attribute will replace the white space by _
automatically, so they are matched now.
Please check the following code on your code its will work you
$criteria->addSearchCondition('location', 'YOUR SEARCH STRING', false);