Page 1 of 1
advanced search
Posted: Fri Jul 26, 2002 4:22 pm
by mrpaulfrank
Im trying to make an advanced search option for my website. i already have a regular search.php script which contains all connectivity and result display info. what im trying to do here is make this form take the information that the user selects and submit it to the exisitng search.php file to display the advanced results.
Code: Select all
<?php
$genre = "genre";
$studio = "studio";
$title = "title";
$actors = "actors";
$description = "description";
?>
</head>
<body>
<?php
echo "<FORM ACTION="/search/advancedsearchrun.php" METHOD="POST" TARGET="main">\n";
echo "<SELECT name="$genre" size=1>\n";
echo "<option>- Category - </option>\n";
echo "<option value="nr">New Releases </option>\n";
echo "<option value="comedy">Comedy</option>\n";
echo "<option value="horror">Horror </option>\n";
echo "<option value="drama">Drama </option>\n";
echo "<option value="action">Action </option>\n";
echo "<option value="scifi">Sci-fi </option>\n";
echo "<option value="adult">Adult </option>\n";
echo "</select><br>\n";
echo "<SELECT name="$studio" size=1>\n";
echo "<option>- Studio - </option>\n";
echo "<option value="studio1">Studio1 </option>\n";
echo "<option value="studio2">Studio2 </option>\n";
echo "</select><br>\n";
echo "Title:";
echo "<INPUT TYPE="text" NAME="$title" SIZE=20><br>\n";
echo "Actors:";
echo "<INPUT TYPE="text" NAME="$actors" SIZE=20><br>\n";
echo "Description:";
echo "<INPUT TYPE="text" NAME="$description" SIZE=20><br>\n";
echo "<INPUT TYPE="submit" VALUE="Search"><br>\n";
echo "<INPUT TYPE="reset" VALUE="Clear"><br>\n";
echo "</FORM>";
?>
</body>
</html>
what things am i missing? should i be using function search_form? im lost as to my next step for making this work.
Posted: Fri Jul 26, 2002 5:04 pm
by RandomEngy
If you already have the search.php verified and operational, I see little that you are missing. Just configure search.php to accept the $_POST stuff and you should be good. Also, you could make it easier on yourself by only starting up the PHP tags when you need them. That way you don't have to escape every single quote, and inserting a PHP variable isn't too painful:
Posted: Fri Jul 26, 2002 5:22 pm
by mrpaulfrank
sounds easy, can anyone give me a sample of the code needed for the search.php file to accept the $_post information? thanks.
Posted: Fri Jul 26, 2002 6:09 pm
by samscripts
what does search.php look like at present? When your form is submitted, you can access the form values like this:
Code: Select all
$genre = $_POSTї"genre"];
$studio = $_POSTї"studio"];
$title = $_POSTї"title"];
etc
etc
then just use them in your regular search script.
Oh, and to check if the form has been submitted:
Code: Select all
1) give your forms submit button a name, eg:
<input type=submit name=search value=Search>
2) check if this exists as a variable:
if( isset($_POSTї"search"]) ){
// search form submitted, so use form variables to search.
}else{
// do your regular search instead
}
see
http://www.php.net/manual/en/language.v ... ternal.php for more info on using form post variables.
hope this helps,
Sam
Posted: Fri Jul 26, 2002 7:12 pm
by mrpaulfrank
alright, everything is working just fine so far, now i need to know how to make the search work (on the most broad level) when something is NOT submitted in a text box, or selected from a pulldown menu. in other words, what its doing right now is not showing any results if there is no information listed in one of the five search options. anyone?
btw, the search script is pretty basic, since i defined those post variables, i have the script selecting information from the mysql db table using 'OR' and 'AND' statements
Code: Select all
$query_Recordset1 = "SELECT id, title, genre, format, image, actors, description FROM test WHERE genre = '$genre' OR genre2 = '$genre' OR genre3 = '$genre' AND studio = '$studio' AND title = '$title' AND actors = '$actors' AND description = '$description'";
then i have a do while loop after this displaying results at a controlled number of times. im guessing i need some sort of null value if nothing is writted or selected in one of the boxes, therefore keeping the mysql select statement from not giving results when it doesnt have a variable to work with.
Posted: Fri Jul 26, 2002 7:31 pm
by samscripts
Hi, just test each variable to see if it has a value, and only include it in the search if it has been set:
Code: Select all
$query_Recordset1 = "SELECT id, title, genre, format, image, actors, description FROM test ";
$criteria = array();
if( $genre != '' ) $criteriaї] = "genre='$genre' OR genre2='$genre' OR genre3='$genre' ";
if( $studio != '' ) $criteriaї] = "studio='$studio' ";
if( $title != '' ) $criteriaї] = "title='$title' ";
if( $actors!= '' ) $criteriaї] = "actors='$actors' ";
if( $description != '' ) $criteriaї] = "description='$description' ";
$criteria = join(" AND ", $criteria);
if( $criteria != '' ) $query_Recordset1 .= " WHERE ".$criteria;
I think this should work - I've used an array to store the search string criteria because it means that you don't have to keep checking if there are already criteria there (ie do you need the WHERE clause, the AND etc.)
hope this makes sense,
Sam
Posted: Fri Jul 26, 2002 8:08 pm
by mrpaulfrank
it works so far for the genre dropdown menu. if the correct selection is made, it limits the results. although, all the other options: studio, title, actors, description are not limiting the results when something is entered. haha, i guess i got what i asked for...now it will still show results if nothing is entered in the box or pulldown, but at the same time theyre not working. heres some code:
Code: Select all
$genre = $_POSTї"genre"];
$studio = $_POSTї"studio"];
$title = $_POSTї"title"];
$actors = $_POSTї"actors"];
$description = $_POSTї"description"];
mysql_select_db($database_connection, $connection);
$query_Recordset1 = "SELECT id, title, genre, format, image, actors, description FROM test ";
$criteria = array();
if( $genre != '' ) $criteriaї] = "genre='$genre' OR genre2='$genre' OR genre3='$genre' ";
if( $studio != '' ) $criteriaї] = "studio='$studio' ";
if( $title != '' ) $criteriaї] = "title='$title' ";
if( $actors!= '' ) $criteriaї] = "actors='$actors' ";
if( $description != '' ) $criteriaї] = "description='$description' ";
$criteria = join(" AND ", $criteria);
if( $criteria != '' ) $query_Recordset1 .= " WHERE ".$criteria;
this is obviously the search script thats being executed by the selections in the previous script (which was in my very first post). where do i need to make changes here?
Posted: Sat Jul 27, 2002 4:47 am
by mrpaulfrank
n/m