RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting

Searching multiple fields...

Join Date: May 2007
Location: Los Angeles, CA
Posts: 7
Reputation: monkeywright is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
monkeywright monkeywright is offline Offline
Newbie Poster

Troubleshooting Searching multiple fields...

  #1  
May 2nd, 2007
I'm fairly new to PHP. Recently got a program to help me build a directory database for a werb client. I've been slowly customizing it to fit my design, but I can't get the search to behave the way I want it to. I need to search multiple fields in one form (ie - name AND city, or area code AND business type). Users should be able to search for as much or as few keywords as they want. My form code looks like this:
	<form name="form1" method="POST" action="<?php $s = 'gtsearch/searchresults2.php'; echo "$path$s"; ?>">
  <table width="254" border="2" align="left" cellpadding="0" cellspacing="0" bordercolor="#ffffcc">
<tr>
<td bgcolor="#ffffcc"><table width="244"  border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td colspan="3"><div align="center" class="style1">FIND A GYM </div></td>
</tr>
<tr>
<td width="72"><span ><font color="#000000">Company</font></span></td>
<td colspan="2"><input type="text" name='title'/></td>
</tr>
<tr>
<td><span ><font color="#000000">City</font></span></td>
<td colspan="2"><input type="text" name="city"></td>
</tr>
<tr>
<td><font size="2">Area Code</font></td>
<td colspan="2"><input type="text" name='phone' size="3"></td>
</tr>
<tr>
<td><font size="2">Zip Code</font></td>
<td colspan="2"><input name="zip" type="text" size="10"></td>
</tr>
<tr>
<td><span class="style4">State </span></td>
<td colspan="2"><select name="state" size="1">
<option value="STATEPROV" selected>Select a State</option>
                            <option value="AL" >Alabama </option>
                            <option value="AK">Alaska </option>
                            <option value="AZ">Arizona </option>
                            <option value="AR">Arkansas </option>
                            <option value="CA">California </option>
                            <option value="CO">Colorado </option>
</select></td>
</tr>
<tr bgcolor="#024282">
<td bgcolor="#FFFFCC">&nbsp;</td>
<td bgcolor="#FFFFCC"><INPUT TYPE="submit" NAME="Search" VALUE="Search" onclick="validate()">
</td>
</tr>
</table></td>
</tr>
</table></form>

And the search results age code looks like this in the header:
[PHP]<?php
session_start();
include 'includes/config.php';
include 'includes/warmup.php';
include "styles/$stylesheet";
$title=$_POST['title'];
$cit=$_POST['city'];
$phone=$_POST['phone'];
$statepro=$_POST['state'];
$zip=$_POST['zip'];
?>[/PHP]

And THIS in the search results form:
[PHP]<h3>Your Search Results </h3>
<body link="<?php echo $color3; ?>" vlink="<?php echo $color3; ?>" alink="<?php echo $color3; ?>">
<div align="center">
<table border="0" cellspacing="0" cellpadding="10" align="center" width="100%">
<tr>
<td valign="top" width="70%">



<?php
if (empty($POST)) {$message = "Your search contained no keywords"; include 'status.php';}else{
$keywords = explode(" ", $title, $cit, $phone, $statepro, $zip);
foreach ($keywords as $value) {
$sql = "Select * from LISTINGS WHERE ";
$sep = '';
if (!empty($title)) {
$sql .= $sep . "title like '%$title%' ";
$sep = ' AND ';
}
if (!empty($city)) {
$sql .= $sep . "city like '%$city%' ";
$sep = ' AND ';
}
if (!empty($phone)) {
$sql .= $sep . "phone like '%$phone%' ";
$sep = ' AND ';
}
if (!empty($state)) {
$sql .= $sep . "stateprov like '%$state%' ";
$sep = ' AND ';
}
if (!empty($zip)) {
$sql .= $sep . "zip like '%$zip%' ";
$sep = ' AND ';
}
$sql="ORDER BY TITLE ASC LIMIT 50";
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ) ) {
$id = $row["ID"];
$title = $row["TITLE"];
$directory = $row["DIRECTORY"];
$phone = $row["PHONE"];
$website = $row["WEBSITE"];
$email = $row["EMAIL"];
$address = $row["ADDRESS"];
$statepro = $row["STATEPROV"];
$cit = $row["CITY"];
$zip = $row["ZIP"];
$map = $row["MAP"];
if ($hold == $id) {} else {
$hold = $id;
?>
<BR>
<table cellspacing="1" cellpadding="2" border="0" bordercolor="#FFFFFF" bgcolor="#F0F0F0" width="100%">
<tr bgcolor="#F9F9F9">

<td width="33%"> <font color="#000000">» <a href="item.php?id=<?php echo $id; ?>&dir=<?php echo $directory; ?>">
<?php echo $title; ?>
</a> </font></td>

<td width="33%">
<?php echo $cit; ?>
,
<?php echo $statepro; ?>
,
<?php echo $map; ?>
</td>
<td width="33%"><a href="<?php echo $website; ?>">Website</a> | <a href="mailto:<?php echo $email ; ?>">Contact</a>
<?php $qrst = none; ?>
</td>
</tr>
</table>

<?php } ?>
<?php } ?>

<?php }
?>

<?php }
?>

<?php if (empty($qrst)) {?>

<table cellspacing="1" cellpadding="2" border="0" bordercolor="#FFFFFF" bgcolor="#F0F0F0" width="100%">
<tr bgcolor="#F9F9F9">

<td width="33%"> <font color="#000000">» Sorry,
we couldn't find anything matching that criteria.</font></td>
</tr>
</table>
<?php } ?>
</td>


</tr>
</table>
</div>[/PHP]

When I try to search, the results page always says no keywords were entered. If I use the single field search that was included with the program, everything works fine.

HELP!
AddThis Social Bookmark Button
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:53 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC