Author Topic: Member search  (Read 620 times)

Offline zavin

  • Level 1
  • *
  • Posts: 1
  • Reputation: +0/-0
    • View Profile
Member search
« on: June 08, 2008, 11:51:57 AM »
 I am trying to add a search for my game. Making a search for just one field is no problem, but I want the user to be able to search with many fields. I have googled and looked at a lot of search scripts, but none of them are very useful for what I'm trying to do. Can anyone lead me in the right direction or show me an example that I can implement.

Offline leZourite

  • Level 12
  • *
  • Posts: 81
  • Reputation: +2/-0
    • View Profile
    • After doomsday
Re: Member search
« Reply #1 on: June 09, 2008, 06:07:39 AM »
I don't know for others but generally i build up a query depending on the input.

example : (for the sake of the example some parts will be missing though)

you have 3 inputs : player name, player min level, player max level.

Code: [Select]
$playername = (isset($_POST['playername'])) ? $_POST['playername'] : null; // this line checks if the player name was posted if yes then put the post in the variable $playername else null

$minlevel = (isset($_POST['minlevel'])) ? $_POST['minlevel'] : null; // same with min level

$maxlevel = (isset($_POST['maxlevel'])) ? $_POST['maxlevel'] : null; // same thing

$searchquery = 'SELECT name, level, whatever FROM tableplayer ''; // start the search query

// Now we will start the conditional query !

if ($playername) {
$searchquery .= ' WHERE name LIKE "%'.$playername.'%" ';
}
else {
$searchquery .= ' WHERE name LIKE "%" ';
}

if ($minlevel) {
$searchquery .= ' AND level >='.$minlevel ';
}

if ($maxlevel) {
$searchquery .= ' AND level <='.$maxlevel ';
}

$searchquery .= ' LIMIT 20';

//execute query retrieve recordset etc etc etc...

The rest should be easy enough and depending on the number of inputs you will have to add more conditions to your query. Note that this example does NOT sanitize the inputs it merely checks that something was posted so don't use it "as is". Apart from the first condition that is mandatory (you need a WHERE close at least), the rest of the conditions will construct or not the query if needed.

Hope it helps :)

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal