[英]MySQL drop downs
I have 1 table named restaurants and this holds the restaurant details such as Name, Address etc. 我有一张名为餐厅的表,其中包含餐厅的详细信息,例如名称,地址等。
I am looking to create a review section which allows users to rate and review through a form for each restaurant. 我希望创建一个评论部分,以允许用户通过表格为每个餐厅评分和评论。 At the moment I have 2 drop down menus in my form one for County and the other for RestaurantName, I would like the user to choose a County and then for the next dropdown to be populated with only the restaurants in that County. 目前,我在窗体中有2个下拉菜单,一个是“县”,另一个是“餐馆名”,我希望用户选择一个县,然后在下一个下拉菜单中仅填充该县的餐馆。
What is the best way to do this? 做这个的最好方式是什么?
If you need any more info let me know. 如果您需要更多信息,请告诉我。 I currently have both dropdowns populated with data from mySql table, but it is showing all restaurants currently. 目前,我的两个下拉列表都填充了mySql表中的数据,但它显示了当前的所有餐厅。
drop.php drop.php
<?php
mysql_connect("localhost", "B00606958", "uHmB4jRw") or die("Error connecting to database: ".mysql_error());
/*
localhost - location of mysql server,
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("b00606958") or die(mysql_error());
/* tutorial_search is the name of database we've created */
?>
<?php
$query_parent = mysql_query("SELECT DISTINCT County FROM restaurants") or die("Query failed: ".mysql_error());
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dependent DropDown List</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#parent_cat").change(function() {
$(this).after('<div id="loader"><img src="images/loading.gif" alt="loading subcategory" /></div>');
$.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
$("#sub_cat").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
});
});
});
});
</script>
</head>
<body>
<form method="get">
<label for="category">County</label>
<select name="parent_cat" id="parent_cat">
<?php while($row = mysql_fetch_array($query_parent)): ?>
<option value="<?php echo $row['County']; ?>"><?php echo $row['County']; ?></option>
<?php endwhile; ?>
</select>
<br/><br/>
<label>Restaurant</label>
<select name="sub_cat" id="sub_cat"></select>
</form>
</body>
</html>
loadsubcat.php loadsubcat.php
mysql_connect("localhost", "B00606958", "uHmB4jRw") or die("Error connecting to database: ".mysql_error());
/*
localhost - location of mysql server,
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("b00606958") or die(mysql_error());
/* tutorial_search is the name of database we've created */
$parent_cat = $_GET['parent_cat'];
$query = mysql_query("SELECT RestaurantID FROM restaurants WHERE County = {$parent_cat}");
while($row = mysql_fetch_array($query)) {
echo "<option value='$row[RestaurantID]'>$row[RestaurantName]</option>";
}
?>
Table Structure: 表结构:
Tbl name = restaurants Tbl名称=餐厅
1 RestaurantName varchar(255) 1个餐厅名称varchar(255)
2 AddressLine1 varchar(100) 2 AddressLine1 varchar(100)
3 AddressLine2 varchar(100) 3 AddressLine2 varchar(100)
4 Town varchar(50) 4镇varchar(50)
5 County varchar(50) 5县瓦尔查尔(50)
6 Postcode varchar(7) 6邮政编码varchar(7)
7 Telephone varchar(11) 7电话varchar(11)
8 Email varchar(50) 8电子邮件varchar(50)
9 Website varchar(100) 9网站varchar(100)
10 NoOfDishes int(255) 10 NoOfDishes int(255)
11 RestaurantID bigint(20) 11 RestaurantID bigint(20)
12 Rating int(5) 12级整数(5)
You have two way for do this: 您有两种方法可以做到这一点:
1) Don't write connection code
in each and every page. 1)不要在每个页面中编写connection code
。 Write in one page & include in each page where it requires. 写一页并在需要的每一页中包含。 I created one page namely dbConnection.php page. 我创建了一个页面,即dbConnection.php页面。
Use this code. 使用此代码。 And, if any error comes. 而且,如果出现任何错误。 Feel free to ask. 随便问。
dbConnection.php dbConnection.php
<?php
$con = mysql_connect("localhost", "B00606958", "uHmB4jRw") or die("Error connecting to database: ".mysql_error());
mysql_select_db("b00606958",$con) or die(mysql_error());
?>
drop.php drop.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dependent DropDown List</title>
</head>
<body>
<?php include('dbConnection.php');?> //connection here.
<form method="get">
<label for="category">County</label>
<select name="parent_cat" id="parent_cat">
<?php
$query_parent = mysql_query("SELECT DISTINCT County FROM restaurants") or die("Query failed: ".mysql_error());
while($row = mysql_fetch_array($query_parent)): ?>
<option value="<?php echo $row['County']; ?>"><?php echo $row['County']; ?></option>
<?php endwhile; ?>
</select>
<br/><br/>
<label>Restaurant</label>
<div class="RestaurantDiv"></div>
</form>
</body>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#parent_cat").change(function() {
$(this).after('<div id="loader"><img src="images/loading.gif" alt="loading subcategory" /></div>');
var county = $(this).val();
$.ajax({url:"loadsubcat.php?parent_cat="+county,cache:false,success:function(result){
$(".RestaurantDiv").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
});
}});
});
});
</script>
</html>
loadsubcat.php loadsubcat.php
<?php
include('dbConnection.php');
$parent_cat = $_GET['parent_cat'];
$query = mysql_query("SELECT * FROM restaurants WHERE County = {$parent_cat}");
?>
<select name="sub_cat" id="sub_cat"></select>
<?
while($row = mysql_fetch_array($query)) {?>
<option value="<?echo $row['RestaurantID'];?>"><?php echo $row['RestaurantName']?></option>
<?}?>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.