Page 1 of 1

selectable search

Posted: Thu Jul 08, 2010 11:08 pm
by Rastan
I want to display some different options for my game such as name, tribe, how much money someone is carrying, max level, min level, etc etc and I want to make a page where people can search by any number of these fields 1 or all of them.

I'd like them to be able to type in the values for the fields and them all be available on 1 page then it just filter the searches by those fields that get filled in. I hope that makes sense. I'm having trouble figuring out just how to do this or where to get started.

If someone could point me in the right direction that would be much appreciated.

Re: selectable search

Posted: Sat Jul 10, 2010 3:38 am
by Rastan
I actually managed to piece this one together finally. I have just included the bare bones of it for any who like me might be looking for similar. I have taken out the pagination(added the limit 25 cause of that for now), injection escapes, and anything that too much relates to my specific project. I'm sure there is better ways of doing this but this is working like I want it to for now.

Basically what happens is on a page I didn't include is a matching series of forms that can be filled out so that a player can search just by currency, just by tribe, or any single field all the way up to filling out all of them. I wanted it this way so if someone was just farming players they could just search how much currency was on hand... or just search members of one tribe etc and it would give somewhat decent results without me writing a query for each possible instance separately.

Hope this helps someone.

Code: Select all

/* These variables came from a run of the mill form POST.  Each is a separate field so I can do a very variable search of potential pvp targets or the like. I feel them to be pretty self explanatory as to what they represent except stones which is my game currency and tribe which is sort of like a guild for my project.
*/
$splayer = $_POST['player'];
$slevelmin = $_POST['levelmin'];
$slevelmax = $_POST['levelmax'];
$stribe = $_POST['tribe'];
$sstonemin = $_POST['stonemin'];
$sstonemax = $_POST['stonemax'];

/* if player is blank its picks by name for any player that's not the session player.  if not blank it uses LIKE and  wildcards the searched name
This variable is the only one that I couldn't potentially leave blank for the query because I needed something to AND off of.
*/
if ($splayer == '')
{
$q1= " name <> '$player'";
}
else
{
$q1 = " name LIKE '%$splayer%'";
}
///if level is blank  it will write nothing into the query. if not blank it writes that piece of code for the query.  The rest of these work similar to this//
if ($slevelmin == '')
{
$q2= '';
}
else
{
$q2 = " AND level >=  '$slevelmin' ";
}

if ($slevelmax == '')
{
$q3= '';
}
else
{
$q3 = " AND level <=  '$slevelmax'";
}

if ($stribe == '')
{
$q4= '';
}
else
{
$q4 = " AND tribe LIKE  '%$stribe%' ";
}


if ($sstonemin == '')
{
$q5= '';
}
else
{
$q5 = " AND stonesnow >=  '$sstonemin' ";
}

if ($sstonemax == '')
{
$q6= '';
}
else
{
$q6 = " AND stonesnow <=  '$sstonemax' ";
}
/*
So now we have the query which has all the "q" varibles in place.  $q1 cannot be blank because the rest of it use it as a point to AND off of
Now a player should be able to search any number of the provided fields and come up with a fairly decent result no matter what they fill in or leave blank.
*/

$squery="SELECT name, level, tribe, stonesnow from player where $q1 $q2 $q3 $q4 $q5 $q6 Order by name Limit 25";
$squery2=mysql_query($squery) or die("Found no players that match your request!");

 while($squery3=mysql_fetch_array($squery2))
{

echo "The search information in whatever format" ;
}

Re: selectable search

Posted: Sat Jul 10, 2010 6:53 am
by Jackolantern
The basic logic for a search engine like that is actually not all that hard. What is hard is making the search engine intelligent. That involves dynamically building regular expressions based on what the user has typed, and thinking ahead to make sure those regular expressions are tuned right to get useful results. For example, if the user enters 5 letters into the search, you don't want to build your regular expression to match any 5 letters. That would basically be useless. One potential algorithm that could be useful in that situation is to swap out any vowels in the search phrase to match any other vowels, or no character at all, since people make misspellings or forget the vowels much more often than consonants.

However, if you are not feeling rather regular expression (and to some extent, AI) adventurous, there are many smart search scripts freely available online that you can gut and use. And I highly suggest to do it. Even some large-scale commercial websites have pitiful search logic, gamespot.com is one I just noticed the other day, and your site can shine in that regard.

Re: selectable search

Posted: Tue Jul 13, 2010 8:06 am
by Rastan
Have posted the basics of my search script in case someone is looking for similar maybe it'll give them something to start with.