Configuring URL rules based on different Query Strings

In SiteController.php I have added a function actionSearch() which has several lines like if(isset($_GET[‘txtKeyword’]) && trim($_GET[‘txtKeyword’]) != “”){…

In two different search forms in my site the user entered search would generate different query string variables. A couple of which are

localhost.mysite/site/search?serviceTypeIdKeyword=1&category=35&txtKeyword=&minprice=&maxprice=50&minbeds=&maxbeds=&minsize=&maxsize=&serviceTypeId=1&categoryIdMajor=1

localhost.mysite/site/search?refNo=EL-R-1005

In config/main.php what do I need to write in the rules array ?

‘rules’=>array(

'<controller:\w+>/<id:\d+>'=>'<controller>/view',


    .............................


    what do I need to write here ?

),

How do you want the rewritten url to look like?

My question is that would yii be able to detect which controller and action to pass the different query strings from two or three different forms giving different query strings

Another question is if I use this

<form action=’/site/search’ method=“get” … it shows me Not Found page. The URL is


http://localhost/mysite/site/search?serviceTypeIdKeyword=1&...



but this works fine

<form action=‘index.php/site/search’ method=“get” …

Another problem I’m having is with the where clause. I start my where clause with a variable $whereClause="1=1 “; then $whereClause .= " serviceTypeId=” . $_GET[‘serviceTypeId’] . " And "; and so on.

I get this error CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; The SQL statement executed was: SELECT unitlistings.*

FROM unitlistings

WHERE 1 sellprice <= 50000000 AND status = ‘available’ AND 1=1

I need WHERE 1=1 at the start. If I use $whereClause=""; I get WHERE 0 which is also an sql error.

I’m also not getting the whereClause inside this conditional


if(isset($_GET['category']) && trim($_GET['category']) != ""){

 $whereClause += " minorCategoryId IN ( " . urldecode($_GET['category']) . " ) AND ";

}

. The query string shows category=35%2C36%2C37%2C40 when the form is submitted

Hey Shaani,

First of l would like to tell you that if you are specifying url hardcoded something as you have mentioned in your post then its not a good approach.:) You should use


Yii::app()->createAbsoluteUrl() 

to define a url. if you are using plan html forms and not creating a form with CActive form class.

And for your next question related to Sql statement,

  1. what is 1 here.??

2)And try this line as


if((isset($_GET['category']))  && (trim($_GET['category']) != "")){

 echo $whereClause += " minorCategoryId IN ( " . urldecode($_GET['category']) . " ) AND ";

}

Does this make any scene to you ?

And please also check a $_GET params that in which form you are getting data for categories. and other after encoding/decoding.

I got the error, $whereClause += should be $whereClause .=

I’m using 1=1 because I have many conditionals in my code so I start with the variable $whereClause = "1=1 AND "; and then do $whereClause .= inside if else statements to get the final sql query as WHERE 1=1 AND … but I’m getting error because of WHERE 1 AND … Not getting 1=1. Why ?

Ahhhh You are contacting string in php with ‘+’ operator OMG.

Please use ‘.’ operator to contact string in php :)