Jump to content

Search form code


graham23s

Recommended Posts

Hi Guys,

 

i think i have found a way to piece together a search query using a plain form, if the user select "Search All" which has the value 0 i do this:

 

<?php
  ## gender options #################################################
  if($gender == 'M' || $gender =='F') {
  
  $searchquery .= "WHERE `gender`='$gender' "; 
  
  } elseif($gender == 0) {
  
  // do nothing with the query
  
  }
  ## gender options #################################################
?>

 

what i'm wondering is, if this would work for all the fields, if it's X then search X from the database else carry on (ie select all)

 

just curious before i do a lot of editing that might not work.

 

cheers guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/79000-search-form-code/
Share on other sites

Yes, you are doing it correctly...just to make sure, here is an example.

 

<?php

$query = "SELECT col FROM table WHERE";

if (isset($gender)) $query .= " AND gender='$gender'";
if (isset($country)) $query .= " AND country='$country'";

//...and so on

?>

Link to comment
https://forums.phpfreaks.com/topic/79000-search-form-code/#findComment-399764
Share on other sites

Hi Poco,

 

ah yeah the isset so instead of

 

  if($gender == 'M' || $gender =='F') {
  
  $searchquery .= "WHERE `gender`='$gender' "; 
  
  } elseif($gender == 0) {
  
  // do nothing
  
  }

 

i could do

 

if(isset($var)) {
  // query here etc
}

 

thanks mate

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/79000-search-form-code/#findComment-400444
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.