【问题标题】:Dynamically pull from a database depending on dropdown result根据下拉结果从数据库中动态拉取
【发布时间】:2013-01-25 22:29:00
【问题描述】:

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

这是我下面的代码:

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

【问题讨论】:

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


    【解决方案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);
    ?>
    

    这应该允许您根据用户选择的值在数据库中显示多行。有什么问题给我留言!

    【讨论】:

      【解决方案2】:

      用户 jquery .change。比如一路上:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-08
        • 2013-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-25
        • 1970-01-01
        相关资源
        最近更新 更多