On my website (in progress), I have a search script, which allows you to search like on http://www.deviantart.com (lots of different options can be searched at once). However, I have just thrown together 115 lines of rubbish for my search script, and whilst it works in most areas, I know that there must be a easier way to process a form. I am quite good at PHP (this search script is about the last thing I have to do in a Content Management System I’ve made) and am simply looking for a PHP script that gets results from a mySQL database depending on input from more than one form field.
For example, you can enter a keyword (where like), choose how many results you want on a page (limit), how to display them (order by), what category to view results from (where), what author to view from (where). I want the visitor to be able to use as many or as little of these search areas as they want.
[code]<?php
$limit = $_POST [‘limit’];
$order = $_POST [‘order’];
$category = $_POST [‘category’];
$author = $_POST [‘author’];
$textsearch = $_POST [‘textsearch’];
if(isset($textsearch)){
$string= “title LIKE ‘%$textsearch%’”;}
if($category != All){
$string1=“category=’$category’”;}
if($author != All){
$string2=“author=’$author’”;}
if($order == A){
$string3=“ORDER BY title”;}
if($order == Z){
$string3=“ORDER BY title ASC”;}
if($order == Plays){
$string3=“ORDER BY hits DESC”;}
if($order == Rating){
$string3=“ORDER BY score DESC”;}
if(isset($string2) && ($string1) && ($string)){
$string="WHERE title LIKE ‘%$textsearch%’ ";
$string1="AND category=’$category’ ";
$string2=“AND author=’$author’”;}
if(isset($string) && ($string1) && (!$string2)){
$string="WHERE title LIKE ‘%$textsearch%’ ";
$string1=“AND category=’$category’”;}
if(isset($string) && ($string2) && (!$string1)){
$string="WHERE title LIKE ‘%$textsearch%’ ";
$string2=“AND author=’$author’”;}
if(isset($string2) && ($string1) && (!$string)){
$string1="WHERE category=’$category’ ";
$string2=“AND author=’$author’”;}
if(isset($string) && (!$string1) && (!$string2)){
$string=“WHERE title LIKE ‘%$textsearch%’”;}
if(isset($string1) && (!$string) && (!$string2)){
$string1=“WHERE category=’$category’”;}
if(isset($string2) && (!$string1) && (!$string)){
$string1=“WHERE author=’$author’”;}
if($category == All){
$string1="";}
if($author == All){
$string2="";}
$query_count = "SELECT count(*) FROM games $string $string1 $string2 $string3";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
$query = "SELECT * FROM games $string $string1 $string2 $string3 LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("<p>No games found!</p>");
}
while($r = mysql_fetch_array($result)){
include(“function.php”);
echo("
");
}
if($page != 1){
$pageprev = $page--;
echo("<a href="?Page==Search&page=$pageprev"><<Previous</a> ");
}else{
echo("<<Previous ");
}
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href="?Page==Search&page=$i">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href="?Page==Search&page=$i">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;
echo(" <a href="?Page=Search&page=$pagenext">Next>></a>");
}else{
echo(" Next>>");
}
mysql_free_result($result);
?>[/code]That is what I have, but there has to be a much simpler way that going through each possibility individually?
Of course, there is a working database connection connected to this file etc. It isn’t a database problem.