Jump to content

Recommended Posts

Hi, this is my first post, I am slightly above noob in PHP so please be merciful.

 

I am trying to create a search facility where the user can type in a word, and/or specify other things from a series of drop down lists (there are 5 drop down lists as well as the word field).

 

I have got it to return all the records in the Mysql database that have the word in field.

 

But I am stumped when it comes to the following:

 

How do I get it to take into account other selections from the drop down lists IF specified by the user (the default option is "Dont Specify"), btw the word field doesn't need to be filled in either as long as at least on of the selections from a drop down box is made.

 

Second, how do i get it to match a word in the form field to a range of words in a record in the database, e.g. a record may have the words "chocolate,banana,orange", I would want that record returned if the user just typed "chocolate".

 

Below is a bit of my code, thanks in advance for your help. :D

 

<?PHP

if (!file_exists("dbconnect.php"))

{

die("Database settings not found, administrator intervention required.") ;

}

else

{

require("dbconnect.php") ; //Must connect to the database.

}

 

$word = $_REQUEST['word'] ; //Variables from the form.

$type = $_REQUEST['type'] ;

$flavor = $_REQUEST['flavor'] ;

$tiers = $_REQUEST['tiers'] ;

$serves = $_REQUEST['serves'] ;

$price = $_REQUEST['price'] ;

 

$def = "Dont Specify" ;

 

if(!isset($word))

{

unset($word) ;

}

if($type == $def)

{

unset($type) ;

//$type = NULL ;

}

if($flavor == $def)

{

unset($flavor) ;

//$tiers = NULL ;

}

if($serves == $def)

{

unset($serves) ;

//$serves = NULL ;

}

if($price == $def)

{

unset($price) ;

//$price = NULL ;

}

 

if(isset($word))

{

$query = "SELECT * FROM image_bank WHERE tags LIKE '%$word%'" ;

}

$result = mysql_query($query) ;

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

$dbtags = $row['tags'] ;

$dbtiers = $row['tiers'] ;

$dbtype = $row['type'] ;

$dbflavor = $row['flavor'] ;

$dbserves = $row['serves'] ;

$dbprice = $row['price'] ;

$imgurl = $row['url'];

$thumburl = $row['thumb_url'] ;

 

echo "$dbtags" . "<br />" ;

echo "$dbtiers" . "<br />" ;

echo "$dbtype" . "<br />" ;

echo "$dbflavor" . "<br />" ;

echo "$dbserves" . "<br />" ;

echo "$dbprice" . "<br />" ;

echo '<a href="' . "$imgurl" . '">' . "$imgurl" . "</a>" . "<br />" ;

echo '<img src="' . "$thumburl" . '" height="100" width="50" />' ;

 

 

}

 

include("dbdisconnect.php") ;

 

?>

 

You need to build up the sql based on the form fields entered. So you start of with the basic sql (also escape any user input prior to running a query):

$word = mysql_real_escape_string($_REQUEST['word']);
$sql    = "SELECT * FROM image_bank WHERE tags LIKE '%".$word%."'";

 

Then check the additional fields and add to the query

if(strlen($_REQUEST['field2'])) {
  $field2 = mysql_real_escape_string($_REQUEST['field2']);
  $sql .= " AND tags LIKE '%".$field2%."'";
}

 

Then run the query

mysql_query($sql);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.