根据下拉结果从数据库中动态提取

时间:2013-02-10 19:26:22

标签: sql database forms dynamic drop-down-menu

我目前正在从我的数据库中提取所需的信息,但希望显示的信息根据下拉结果而改变。我做了一些研究,并认为最好的方法是使用表格但不确定它是如何工作的。

以下是我的代码:

      <section id="compare">

    <select>
      <option>Select Gym</option>       
      <option>fitness first</option>
      <option>anytime fitness</option>
      <option>the gym</option>
      <option>its leisure ltd</option>
      <option>the armoury</option>
    </select>
    <select>
      <option>Select Gym</option>       
      <option>fitness first</option>
      <option>anytime fitness</option>
      <option>the gym</option>
      <option>its leisure ltd</option>
      <option>the armoury</option>      
    </select>

    <section id="left"> 
    <?php

    mysql_select_db("gyms", $con);
    $result = mysql_query("SELECT * FROM gym WHERE id='1'") or die ('Error: '.mysql_error ());

        while($row = mysql_fetch_array($result)){
          echo "<h1>" . $row['name'] . "</h1>";
          echo "<p><h6>type</h6>" . $row['type'] . "</p>";
          echo "<p><h6>price</h6>" . $row['price'] . "</p>";
          echo "<p><h6>hours</h6>" . $row['hours'] . "</p>";
          echo "<p><h6>parking</h6>" . $row['parking'] . "</p>";
          echo "<p><h6>facilities</h6>" . $row['facilities'] . "</p>";
          } 
    ?>      
    </section>

    <section id="right">
    <?php
    $result = mysql_query("SELECT * FROM gym WHERE id='2'") or die ('Error: '.mysql_error ());

        while($row = mysql_fetch_array($result2)){
          echo "<h1>" . $row['name'] . "</h1>";
          echo "<p><h6>type</h6>" . $row['type'] . "</p>";
          echo "<p><h6>price</h6>" . $row['price'] . "</p>";
          echo "<p><h6>hours</h6>" . $row['hours'] . "</p>";
          echo "<p><h6>parking</h6>" . $row['parking'] . "</p>";
          echo "<p><h6>facilities</h6>" . $row['facilities'] . "</p>";
          } 

    mysql_close($con);
    ?>
    </section>

  </section>    

2 个答案:

答案 0 :(得分:1)

你是绝对正确的,你需要使用一个实现POST方法的表单,它在表单中发布选定的值,匹配数据库中包含的'id'字段。

以下是一个例子:

<form action="compare.php" method="post">
    <select name="gyms-1">
      <option value="0">Select Gym</option>         
      <option value="1">fitness first</option>
        </select>
    <select name="gyms-2">
      <option value="0">Select Gym</option>         
      <option value="1">fitness first</option>      
    </select>
    <input name="send" id="send" type="submit" value="compare" />
</form>

然后这是实现post方法的PHP代码:

<?php
    $gyms=$_POST['gyms-1'];
    $jimmy=$_POST['gyms-2'];

    mysql_select_db("gyms", $con);
    $result = mysql_query("SELECT * FROM gym WHERE id='$gyms'") or die ('Error: '.mysql_error ());

        while($row = mysql_fetch_array($result)){
          echo "<h1>" . $row['COLUMN NAME'] . "</h1>";
          echo "<p><h6>type</h6>" . $row['COLUMN NAME'] . "</p>";
          } 

    $result2 = mysql_query("SELECT * FROM gym WHERE id='$jimmy'") or die ('Error: '.mysql_error ());

        while($row = mysql_fetch_array($result2)){
          echo "<h1>" . $row['COLUMN NAME'] . "</h1>";
          echo "<p><h6>type</h6>" . $row['COLUMN NAME'] . "</p>";
          } 

    mysql_close($con);
?>

这应该允许您根据用户选择的值在数据库中显示多行。任何问题都会让我大喊大叫!

答案 1 :(得分:0)

用户jquery .change。例如,沿途:

   $("#left").change(function(){ 
    $("#right").html("[post your data]");
   });