Here's my script:
<?
//Get the type and determine the propery queries.
$type = mysql_real_escape_string($_GET['type']);
?>
<td valign="top" width="960" height="500" bgcolor="#ffffff">
<blockquote><br /><br />
<img src="images/pageHeaderRentals.jpg" width="184" height="60" title="Rentals"><br />
<h3>Search Results</h3>
<center>
<table align="center" border="0" cellpadding="2" cellspacing="2" width="800">
<?
if($type == "keywords"){
$keywords = mysql_real_escape_string($_GET['kw']);
$kw = explode(",", $keywords);
foreach($kw AS $keyword){
$query = "SELECT id, title, description, listingPhoto, mapReference, propertyCode, city, state, rating FROM Properties WHERE (`description` LIKE '%$keyword%' OR `area` LIKE '%$keyword%' OR `rentalTerms` LIKE '%$keyword%' OR `amenities` LIKE '%$keyword%' OR `community` LIKE '%$keyword%') AND active = 'Yes'";
$result = mysql_query($query);
if(!$result){
echo "<blockquote>";
echo "Your search returned <b class='managerSectionHeader'>0</b> results. Please click <a href='advancedSearch.php'>here</a> to go back and search again!<br /><br />";
echo "</blockquote>";
}else{
$info = mysql_fetch_assoc($result);
echo "<blockquote>";
echo "Your search returned <b class='managerSectionHeader'>".mysql_num_rows($result)."</b> properties. If you would like to search again please click <a href='advancedSearch.php'>here</a> to go back and search again!<br /><br />";
echo "</blockquote>";
$listingPhoto = substr($info['listingPhoto'], 3);
echo "<tr>";
echo "<td valign='top' width='150'><img src='".$listingPhoto."' width='150' height='100' title='Listing Photo' border='0'></td><td valign='top' width='650'><b class='adminManagerHeader'>".$info['title']."</b><br /><i>Located in ".$info['city'].", ".$info['state']."</i><br /><br />".substr($info['description'], 0, 200)."... <a class='adminLink' href='viewProperty.php?id=".$info['id']."'>(more)</a><br /><b class='small-purple'>Map Reference:</b><b class='small-grey'> ".$info['mapReference']."</b> <b class='small-purple'>Property Code:</b><b class='small-grey'> ".$info['propertyCode']."</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td height='2' colspan='2'><img src='images/hbar.jpg' height='2' width='800' title=''></td>";
echo "</tr>";
}
}
}
?>
</table>
</center>
</blockquote>
</td>
It outputs part of a table dynamically based on records pulled from a MySQL db. I have a couple of problems. If the 2nd term in the keywords is not found, I get a "0 results found" error, even though the term may exist. A sub-problem of this is that it will print the "Your search returned # of results etc etc..." for each keyword. How do I make it so that it loops through the array to find the records and totals them, echoing once to the screen and not for each query?
Problem 2 is I don't want a record to be displayed more than once. Meaning if a record is found by using the 1st keyword, even if it contains the 2nd keyword, it should only be echo'd to the screen once.
I'm pretty sure my problems have to do with the fact I'm using a foreach and iterating through the keywords array. Any help is appreciated and if you know a better way, please... I'm all ears! (and fingers for typing)