Click to See Complete Forum and Search --> : advanced seach


kproc
02-07-2007, 08:48 PM
Below is could that Im trying to use to create an advance search.

the values for all categories except title are select menus. the problem is the below returns all enteries. Its not narrowing it down based on the the conditions set.

If on field is blank then I want it to be omitted from the query and use the other criteria set by the user any help is excellent


if(isset($_POST['advanceSearch'])){
$title = $_POST['title'];
$ad_type = $_POST['ad_type'];
$category = $_POST['category'];
$sub_category = $_POST['sub_category'];

$query_advance_ads = ("SELECT * FROM ads WHERE title LIKE '%$title%' OR category = '$category' OR sub_category = '$subcategory' OR ad_type = '$ad_type'");
$sql_advance_ads = mysql_query($query_advance_ads)or die("SQL Error: $query_advance_ads<br>" . mysql_error());
}

?>

NightShift58
02-07-2007, 08:57 PM
Maybe this:<?php
if(isset($_POST['advanceSearch'])){
$title = $_POST['title'];
$ad_type = $_POST['ad_type'];
$category = $_POST['category'];
$sub_category = $_POST['sub_category'];

$query_advance_ads = ("SELECT * FROM ads "
. " WHERE (category = '$category' OR sub_category = '$subcategory' OR ad_type = '$ad_type')"
. " AND title LIKE '%$title%' ";
$sql_advance_ads = mysql_query($query_advance_ads)or die("SQL Error: $query_advance_ads<br>" . mysql_error());
}
?>

kproc
02-07-2007, 09:08 PM
Thanks Nightshift. That worked excellent. I noticed that you use lots of spacing in your code and the layout is easy to ready, is there a book or a website that could help me learn more about code formating

NightShift58
02-07-2007, 09:19 PM
No, there isn't as such - that I know of...

White-spacing doesn't count, so use as you please to make your code more readable to you.