Advanced Database Searching...

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
Goldmund
-


Joined: 19 Feb 2005
Posts: 19
Location: Seattle, WA

PostPosted: Mon Mar 21, 2005 3:01 am    Post subject: Advanced Database Searching... Reply with quote

Hi Guys-
I'm having trouble getting a advanced search of my database. First of all, I have twelve drop-down menus. It would take a very, very long time to set up a conditional loop for all combinations if people decide to only use just a few of these fields. So I was thinking the next easiest option would be to make the 'ANY' value="Yada OR Yada OR Yada...". Here is a brief example of my script:
-------------------------------------------------------------------------------------
if (isset($_POST['submit'])) {

$gender = $_POST['gender'];
$build = $_POST['build'];

$query = "SELECT * FROM profile WHERE gender='$gender' AND build='$build'";

if ($result = mysql_query ($query)) { //Run the query
print '<h3>Your Matches...</h3>';

//Retrieve and print every record
while ($row = mysql_fetch_array ($result)) {
print "<a href=\"personals_view.php?id={$row['user_name']}\">{$row ['user_name']}</a><hr />\n";
}

} else { //Query didn't run
die ('<p>Could not retrieve the data because: <b>' . mysql_error() . "</b>.
The query was $query.</p>");

} //End of query IF

} else { //Didn't press 'Submit' yet

<form action="personals.php" method="post">
<select name="gender" align="left" >
<option value="Male OR Female" >Any</option>
<option value="Male" >Male</option>
<option value="Female" >Female</option>
</select>

<select name="build" >
<option value="Slim/Slender OR Average OR Athletic OR Fit">Any</option>
<option value="Slim">Slim/Slender</option>
<option value="Average">Average</option>
<option value="Athletic">Athletic</option>
<option value="Fit">Fit</option>
</select>

</form>
-------------------------------------------------------------------------------------
This didn't work. On another forum I was told to exchange my OR's for commas and my syntax in my $query like so:
-------------------------------------------------------------------------------------
$query = "SELECT * FROM profile WHERE gender IN ('$gender') AND build IN ('$build')";

<option value="Male, Female" >Any</option>
...
<option value="Slim/Slender, Average, Athletic, Fit">Any</option>
-------------------------------------------------------------------------------------
This also didn't work. I've tried everything it seems. Every combination possible and nothing seems to work. Does anyone have any ideas? Thanks in advanced!
_________________
You learn something new every day.
Back to top View user's profile Send private message
Goldmund
-


Joined: 19 Feb 2005
Posts: 19
Location: Seattle, WA

PostPosted: Mon Mar 21, 2005 6:10 pm    Post subject: Reply with quote

For everyone interested-

Okay, I solved the problem for having an advanced search on your website. I will give an example below (using parts from the above script):

1)First you change the <select> menu options like so:
--------------------------------------------------------------------------------------
<select name="build" >
<option value="_">Any</option>
<option value="Slim">Slim/Slender</option>
<option value="Average">Average</option>
<option value="Athletic">Athletic</option>
<option value="Fit">Fit</option>
</select>
--------------------------------------------------------------------------------------
By using "_" allows the 'Any' value to equal "one character"

2)Now we change the query like so:
--------------------------------------------------------------------------------------
$query = "SELECT * FROM profile WHERE gender LIKE '%$gender%' AND build LIKE '%$build%'";
--------------------------------------------------------------------------------------
This tells the query to SELECT * FROM profile WHERE both the values 'gender and build' have at least one character, which all of them do obviously.

I hope this helps anyone who has had trouble in creating multiple questioned searches. Good Day!
_________________
You learn something new every day.
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group