I have two tables: A table of restaurants and a table of areas. The "EntryID" key in th开发者_JAVA技巧e Addresses table is a number that is the same as the primary key of each record in the restaurant table. So EntryID is a foreign key. I want to be able to modify my PHP select script to search the database across these two tables. Here is what I have right now:
$RPrice = $_GET["price"];
$RType = $_GET["type"];
$RAtmosphere = $_GET["RAtmosphere"];
$RArea = $_GET["RArea"];
if (!empty($RPrice)) $w[]="Foodlist.price='".mysql_real_escape_string($RPrice)."'";
if (!empty($RType)) $w[]="Foodlist.ftype='".mysql_real_escape_string($RType)."'";
if (!empty($RAtmosphere)) $w[]="Foodlist.atmosphere='".mysql_real_escape_string($RAtmosphere)."'";
if (!empty($RArea)) $w[]="Addresses.area='".mysql_real_escape_string($RArea)."'";
if (count($w)) $where="WHERE ".implode(' AND ',$w); else $where='';
$sql="SELECT Foodlist.ID, Foodlist.name from Foodlist JOIN Addresses ON (Foodlist.ID = Addresses.EntryID) $where";
$result= mysql_query($sql);
while($row=mysql_fetch_array($result)){
$ID=$row['ID'];
echo "<a class=\"cross-link\" href=\"javascript:ajaxpage('result.php?id=$ID', 'results2'); ajaxpage('videoloader.php?id=$ID', 'results1');\">".$row['name']."</a><div id=\"contentarea\"></div>";
}
I've commented out the "area" portion out because that is something that will need to be changed.
I want to be able to maintain the same functionality as before, but I want to be able to search across multiple area records. I hope this makes sense. If you need more clarification, please let me know.
Foodlist(table) contains the following columns: ID, price, type, atmosphere Addresses(table) contains the following columns: EntryID, area
Right now, the query is assuming that all of these columns (except entryID) are in foodList. I need the query to search area from the Addresses table. Also, I'm wanted to return 1 result from the query per ID. It is possible that one record in the FoodList table will have multiple corresponding records in the Addresses table. In this case, I only want it to return the one that matches the area given.
SELECT f.ID, name FROM Foodlist f JOIN Addresses a ON (f.ID = a.EntryID) $where
I believe this should be sufficient for what you need.
精彩评论