MySQL下拉

时间:2016-03-25 11:12:24

标签: php mysql

我有一张名为餐厅的餐桌,它包含餐厅的详细信息,如姓名,地址等。

我希望创建一个评论部分,允许用户对每个餐厅的表单进行评分和评论。目前我在我的表格中有2个下拉菜单,一个用于郡,另一个用于RestaurantName,我希望用户选择一个县,然后在下一个下拉菜单中只填充该县的餐馆。

最好的方法是什么?

如果您需要更多信息,请告诉我们。我目前有两个下拉列表填充mySql表中的数据,但它显示当前所有的餐馆。

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     

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>";
}

?>

表格结构:

Tbl name = restaurants

1 RestaurantName varchar(255)

2 AddressLine1 varchar(100)

3 AddressLine2 varchar(100)

4 Town varchar(50)

5县varchar(50)

6 Postcode varchar(7)

7电话varchar(11)

8电子邮件varchar(50)

9网站varchar(100)

10 NoOfDishes int(255)

11 RestaurantID bigint(20)

12评级int(5)

2 个答案:

答案 0 :(得分:0)

你有两种方法可以做到这一点:

  1. 在用户更改国家/地区选择值时发出Ajax请求;
  2. 在餐厅选项中注册国家/地区的引用;当用户更改国家/地区时,每个餐馆选择与所选国家/地区的引用不匹配(实施取决于您使用的语言);

答案 1 :(得分:0)

1)不要在每一页都写connection code。写在一页&amp;在每个页面中包含它所需的内容。我创建了一个页面,即 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

<!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

<?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>