Jump to content

help with $_GET


denny911

Recommended Posts

Hello to everyone.I have a sort of search form on my. it consists of three form items.on the search.php i have a select statement with variables (i get them from $_GET variables coming from my form) included.something like this:$ctg = $_GET['frm_category'];$type = $_GET['frm_type'];$city = $_GET['frm_city'];

" SELECT * FROM $table1, tbl_users WHERE category = '$ctg' AND type = '$type'AND city = '$city' ORDER BY..  "

all in all, everything works just fine, UNLESS one (or several fields) not selected (their values being passed with NO VALUE).how can i avoid using ONE or MORE variables in my SELECT statement if no value has been passed from my form?i managed to do so using the IF statement(if VARIABLE empty --STATEMENT WITHOUT THAT VARIABLE-- else --STATEMENT WITH THAT VARIABLE INCLUDED)but, this seems to be complicated, so i wonder if there's any other way?I'm pretty new to PHP so.. :)Thanks in advanceDenis K., Bosnia H.

Link to comment
Share on other sites

well, one solution would be to use wildcards

"SELECT * FROM $table1, tbl_users WHERE category LIKE '%".$ctg."' AND type LIKE '%".$type."'AND city LIKE  '%".$city".'"

Link to comment
Share on other sites

i managed to do so using the IF statement(if VARIABLE empty --STATEMENT WITHOUT THAT VARIABLE-- else --STATEMENT WITH THAT VARIABLE INCLUDED)
That's the best way to do it, if you can find something that is less complicated then a basic if statement, please let the programming community know.
Link to comment
Share on other sites

Maybe there should be a 'why not' statement in PHP?! That would be fun! Uh. I literally just did something like your doing. I put the 'LIKE' statements into an array so :

$likes= array();foreach ($values as $k=>$v) {  if (isset($_GET['this'])) {	$this= $_GET['this']; 	likes[]= "word LIKE '$this'";  }  elseif ...}

then in the query you use implode(" AND ", $likes)or you can change to OR (can't mix with this method) if the likes are optionalso something like

 $sql= "SELECT col FROM table WHERE ".implode(" AND ", $likes);

This method I suppose isn't simpler as you're using more loops and arrays. I suppose you can't get simpler than if...else like justsomeguy says! But I like my method :) Maybe it will be useful for you!

Link to comment
Share on other sites

Right. This is about as easy as it gets:

$sql = "SELECT * FROM $table1, tbl_users ";$where = "";if ($ctg != "")  $where .= " WHERE category = '$ctg'";if ($type != ""){  if ($where == "")	$where .= " WHERE ";  else	$where .= " AND ";  $where .=  "type = '$type'";}if ($city != ""){  if ($where == "")	$where .= " WHERE ";  else	$where .= " AND ";  $where .=  "city = '$city'";}$sql .= $where . " ORDER BY ...";

It might be a lot of code (I format my code for readability over compactness), but it is easy to read and it will execute quickly. If you want code that is more compact, you can replace this:

  if ($where == "")	$where .= " WHERE ";  else	$where .= " AND ";

With this:

$where .= ($where == "" ? " WHERE " : " AND ");

to get this:

$sql = "SELECT * FROM $table1, tbl_users ";$where = "";if ($ctg != "")  $where .= " WHERE category = '$ctg'";if ($type != "")  $where .= ($where == "" ? " WHERE " : " AND ") . "type = '$type'";if ($city != "")  $where .= ($where == "" ? " WHERE " : " AND ") . "city = '$city'";$sql .= $where . " ORDER BY ...";

Link to comment
Share on other sites

well guys, it seems LULZIM gave me the most suitable solution.yes, i had to do some modification in order for the code to work (i had my categories' id's numbered from 1 to 34) so now i changed them to end in 0 (zero) so that Lulzim's condition LIKE %$category would work if there's no value passed from the form (since there's no category id = 0)--thanks to all of you who wanted to help.see you next timep.s. i have a new topic in PHP category so if ur interested in it, take a look!Denis

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...