Forum Moderators: coopster

Message Too Old, No Replies

PHP Search box

Need to create a 3 subject search box

         

togethercomms

10:01 am on Nov 23, 2009 (gmt 0)

10+ Year Member



Hi all,

I have been looking for a little while online for a script that would allow me to create a search box on my website that allows me to search 3 different keyword areas.

I need three search boxes that will search for the same result, i have found nothing online because their is no straight forward tutorial on it.

Many Thanks all

skinsey

12:13 pm on Nov 23, 2009 (gmt 0)

10+ Year Member



I didn't go through a test this but here is something to get you started.

if(isset($_POST['company']))
{
$company = $_POST['company'];
$where = "company = '$company'";

}
if(isset($_POST['state']))
{
$state = $_POST['state'];

if($where == NULL)
{
$where = "$where state = '$state'";
}
else
{
$where = "$where AND state = '$state'";
}
}
if(isset($_POST['city']))
{
$city = $_POST['city'];

if($where == NULL)
{
$where = "$where city = '$city'";
}
else
{
$where = "$where AND city = '$city'";
}
}


$query = "SELECT * FROM tablename where $where";

rocknbil

7:11 pm on Nov 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member





var $input = Array ('company','state','city');
var $where=$select=NULL;
foreach ($input as $i) {
if (isset($_POST[$i])) {
if ($where) { $where .= " and"; }
$where .= " $i='$_POST[$i]'";
}
}
}
$select = "select * from $table";
if ($where) { $select .= " where $where"; }

TWO THINGS you need to address:
- The above code does not filter $_post[input], do not directly query the database without cleansing.
- The above code uses the same names for input as the fields in the database, do not do this - use an aliasing hash or some method to avoid revealing database field names.

togethercomms

11:13 am on Nov 24, 2009 (gmt 0)

10+ Year Member



Thats great, something to get me started, probably be back on soon.

Many Thanks