簡體   English   中英

MySQL下拉列表

[英]MySQL drop downs

我有一張名為餐廳的表,其中包含餐廳的詳細信息,例如名稱,地址等。

我希望創建一個評論部分,以允許用戶通過表格為每個餐廳評分和評論。 目前,我在窗體中有2個下拉菜單,一個是“縣”,另一個是“餐館名”,我希望用戶選擇一個縣,然后在下一個下拉菜單中僅填充該縣的餐館。

做這個的最好方式是什么?

如果您需要更多信息,請告訴我。 目前,我的兩個下拉列表都填充了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名稱=餐廳

1個餐廳名稱varchar(255)

2 AddressLine1 varchar(100)

3 AddressLine2 varchar(100)

4鎮varchar(50)

5縣瓦爾查爾(50)

6郵政編碼varchar(7)

7電話varchar(11)

8電子郵件varchar(50)

9網站varchar(100)

10 NoOfDishes int(255)

11 RestaurantID bigint(20)

12級整數(5)

您有兩種方法可以做到這一點:

  1. 當用戶更改“國家/地區”選擇值時發出Ajax請求;
  2. 在“餐廳”選項中注冊對國家/地區的引用; 當用戶更改國家/地區時,隱藏與所選國家/地區的引用不匹配的每個餐廳選擇(具體實現取決於您使用的語言);

1)不要在每個頁面中編寫connection code 寫一頁並在需要的每一頁中包含。 我創建了一個頁面,即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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM