Addsearchcondition

Hi friends.

I have 2 variables and I want to check if the first variable exists in the the city column and the second variable in type "OR" category column.

I did this and it doesn’t work :

$criteria = new CDbCriteria(array(

		'condition'=>'published="true"'


		));

if(isset($_GET[‘city’]) || isset($_GET[‘word’]))

		{


			if(!empty($_GET['city']) )


			$criteria->addSearchCondition('city',$_GET['city'] , true, 'AND');


			if(!empty($_GET['word']) ){


			$criteria->addSearchCondition('type',$_GET['word'] , true, 'OR');


			$criteria->addSearchCondition('category',$_GET['word'] , true, 'OR');





		}}

And only rows with published=true will be displayed…

As a result i get rows that doesn’t meet my criteria…

Where is the problem please help :blink: :blink:

thanks

Hello

With your code you are asking to build a criteria that matches records that have:

  • published = "true"

  • AND city LIKE %{$_GET[‘city’]}%

  • OR type LIKE %{$_GET[‘word’]}%

  • OR category LIKE %{$_GET[‘word’]}%

You’ll agree with me you get exactly what you coded.

But I believe you want this:

  • published = "true"

  • AND city LIKE %{$_GET[‘city’]}%

  • AND [color="#FF0000"]([/color]type LIKE %{$_GET[‘word’]}% OR category LIKE %{$_GET[‘word’]}%[color="#FF0000"])[/color]

Right?

So you need to use CDbCriteria::mergeWith():


$criteria = new CDbCriteria(array(

    'condition'=>'published="true"'

));


if (isset($_GET['city']) || isset($_GET['word'])) {

    if (!empty($_GET['city'])) {

      $criteria->addSearchCondition('city', $_GET['city'], true, 'AND'); /* 'AND' is the default value for $operator, so needless here */

    }

    if (!empty($_GET['word'])) {

        $criteria2 = new CDbCriteria;

        $criteria2->addSearchCondition('type', $_GET['word'], true);

        $criteria2->addSearchCondition('category', $_GET['word'], true, 'OR');

        $criteria->mergeWith($criteria2);

    }

}

PS Please format your code with, at least, the <> button on the post’s textarea toolbar (you can edit your original post to do so)

Amazing

Many Thanks!!!

Hi, gentleman.

I works so well. Could you please help me with the pagination. After pressing the pagination of the grid, this does not work.

Thanks in advice.