如何将案例切换mySQL查询的结果回显到span标记内的表中

时间:2019-03-24 22:38:03

标签: php html mysql

一旦选择了一个选项,并且用户单击“提交”,我将需要用户从Allergengen 1的选择框中输入内容,查询将基于选择框的选定值运行。我希望通过使用一个案例并切换以指定要运行的查询来实现此目的,由此我想将查询结果回显到一个表中。该表为“ tableleft”

-

Click the RED filter
   Iterate over all elements on the page and
      If it has the RED class, make hidden property = false
      Otherwise, make hidden property = true
   Then, iterate over all buttons of type COLOUR
      If it's the RED button, add it the css class for btn_active
      Otherwise, remove the btn_active class

我希望查询一旦运行就可以通过回显到“左表”输出,对此将有任何帮助。

1 个答案:

答案 0 :(得分:-1)

首先,您应该使用MySQLi或PDO_MySQL扩展名。

而且您不需要开关盒。

这里有一个完整的例子:

<!DOCTYPE html>
    <html>
    <head>
      <title>Query</title>
      <!-- Bootstrap -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    </head>
    <body>  
      <div class="container p-2">

       <h2>Allergen Menu</h2><hr class="mb-4">

       <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <div class="form-group">
          <label for="exampleFormControlSelect1">Please Select</label>
          <select name="allergen" class="form-control" id="exampleFormControlSelect1">
            <option value="wheat">Wheat</option>
            <option value="Spelt">Spelt</option>
            <option value="kamut">Kamut</option>
            <option value="Rye">Rye</option>
            <option value="Barley">Barley</option>
            <option value="Oats">Oats</option>
            <option value="Fish">Fish </option>
            <option value="Crustaceans">Crustaceans</option>
            <option value="Molluscs">Molluscs</option>
            <option value="Eggs">Eggs</option>
            <option value="Soybeans">Soybeans</option>
            <option value="Milk">Milk</option>
            <option value="Almonds">Almonds</option>
            <option value="Hazelnut">Hazelnut</option>
            <option value="Walnut">Walnut</option>
            <option value="CashewNut">Cashew Nut</option>
            <option value="PecanNut">Pecan Nut</option>
            <option value="BrazilNut">Brazil Nut</option>
            <option value="Pistacio">Pistacio</option>
            <option value="Macadamia">Macadamia</option>
            <option value="Peanuts">Peanuts</option>
            <option value="Celery">Celery</option>
            <option value="Mustard">Mustard</option>
            <option value="Sesame">Sesame</option>
            <option value="Sulphites">Sulphites</option>
            <option value="Lupin">Lupin</option>
          </select>
        </div>
        <button type="submit" name="submit" class="btn btn-primary mb-2">Submit</button>
      </form>

      <h2 class="mt-5">Result</h2><hr class="mb-5">


      <?php
      if(isset($_POST['submit'])){

        // Connect to DB
        $dsn = 'mysql:host=localhost; dbname=Your_DB_Name; charset=utf8mb4';
        try{  
          $connection = new PDO($dsn, 'DB_username', 'DB_password');
          $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }catch(Exception $e){
          die($e->getMessage());
        }

        // Get Input data
        $allergen = htmlentities(addslashes($_POST['allergen']));

        // prepare query
        $sql = 'SELECT * FROM allergendishmenu WHERE ' . $allergen . ' = 0'; 
        $result = $connection->query($sql)->fetchAll(PDO::FETCH_OBJ);

      }
      ?>

      <table class="table table-bordered">
        <thead>
          <tr>
            <th scope="col">Dishname</th>
            <th scope="col">Allergens</th>
          </tr>
        </thead>
        <tbody>
          <?php 
          if(isset($result)){
            foreach($result as $data): ?>   
              <tr>          
                <td><?php echo $data->DishName; ?></td> 
                <td><?php echo $data->$allergen; ?></td>
              </tr>
              <?php 
            endforeach; 
          }else{
            echo '<tr><td colspan="2">No data loaded</td></tr>';
          }
          ?>  
        </tbody>
      </table>

    </div>


    </body>
    </html>