简体   繁体   English

PHP:输入MySQL查询

[英]PHP: Input MySQL query

Hey guys I am a little confused, I have a listing page that displays all of my rows in my SQL table. 大家好,我有点困惑,我有一个列表页面,显示我的SQL表中的所有行。

An example of what I am developing can be seen here: http://www.drivencarsales.co.uk/used-cars.php 在此可以看到我正在开发的示例: http : //www.drivencarsales.co.uk/used-cars.php

So as you can see I have developed a listing page that fetches all of the table rows, each row is equivalent to one vehicle. 因此,正如您所看到的,我开发了一个列表页面,该页面可获取所有表行,每一行等效于一种车辆。

I now want to let users filter the results using the form to the left, I was going to use AJAX originally however I feel as if it would take way to long to learn how to develop it that way. 现在,我想让用户使用左侧的表单过滤结果,我本来打算使用AJAX,但是我觉得学习这种方式开发似乎需要很长时间。

Here is the code setup I am using to achieve the example I have shown: 这是我用来实现所示示例的代码设置:

<?php
include('database.php');
try {
  $results = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make ASC");
} catch (Exception $e) {
  echo "Error.";
  exit;
}

try {
  $filterres = $db->query("SELECT DISTINCT Make FROM import ORDER BY Make ASC");
} catch (Exception $e) {
  echo "Error.";
  exit;
}
?>

As you can see the first block of code has two SQL selectors, the $results is used to fetch the whole table and list all vehicles. 如您所见,第一个代码块具有两个SQL选择器,$ results用于获取整个表并列出所有车辆。

The second block is used to display the 'Make' column for the form. 第二个块用于显示表单的“制作”列。

This block of code is the actual form: 此代码块是实际形式:

<form>
     <select class="form-control select-box" name="">
                 <option value="make-any">Make (Any)</option>
                 <?php while($make = $filterres->fetch(PDO::FETCH_ASSOC))
                 {
                 echo '
                 <option value="">'.$make["Make"].'</option>
                 ';
                 } ?>
     </select>
     <button href="#" class="btn btn-block car-search-button btn-lg btn-success"><span class="glyphicon car-search-g glyphicon-search"></span> Search cars 
     </button>
     </form>

As you can see this block is using a while loop to display all of the 'Make's' in the 'Make' column and uses a DISTINCT clause so that it doesn't show identical options. 如您所见,此块正在使用while循环在“ Make”列中显示所有“ Make”,并使用DISTINCT子句,以便不显示相同的选项。

Here is the block that lists the results to the page: 这是将结果列出到页面的块:

<?php while($row = $results->fetch(PDO::FETCH_ASSOC))
      {
      echo '
        <div class="listing-container">
          <a href="carpage.php"><h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3></a>
          <h3 class="price-listing">£'.number_format($row['Price']).'</h3>
        </div>
        <div class="listing-container-spec">
         <img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/>
          <div class="ul-listing-container">
            <ul class="overwrite-btstrp-ul">
              <li class="diesel-svg list-svg">'.$row["FuelType"].'</li>
              <li class="saloon-svg list-svg">'.$row["Bodytype"].'</li>
              <li class="gear-svg list-svg">'.$row["Transmission"].'</li>
              <li class="color-svg list-svg">'.$row["Colour"].'</li>
            </ul>
          </div>
          <ul class="overwrite-btstrp-ul other-specs-ul h4-style">
            <li>Mileage: '.number_format($row["Mileage"]).'</li>
            <li>Engine size: '.$row["EngineSize"].'cc</li>
          </ul>
          <button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked 
          </button>
          <button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details 
          </button>
          <button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive 
          </button>
          <h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4>
        </div>
          ';
      } ?>

So down to my question... How can I filter these results displayed in the listing block using the select element, when a user selects a 'Make' from the select element I want them to be able to submit the form and return all rows in the SQL table containing the same 'Make' string and hide other rows that are false. 所以请问我的问题...当用户从select元素中选择“制作”时,如何使用select元素过滤显示在列表框中的结果,我希望他们能够提交表单并返回所有行在包含相同“ Make”字符串的SQL表中,并隐藏其他错误的行。

Any ideas how I can achieve this or any easier ways? 有什么想法可以实现这一目标或更简单的方法吗?

Thanks 谢谢

My best guess is for you to use JS to submit the form again, when select-option is selected, with a parameter stating the model. 我最好的猜测是,当选择了select-option时,使用JS再次提交表单,并带有说明模型的参数。

Then in your PHP file, you should look for that parameter and edit the query-string accordingly. 然后,在您的PHP文件中,您应该寻找该参数并相应地编辑查询字符串。

see this answer on how to submit a form when option is selected. 有关选择选项时如何提交表单的信息,请参见此答案

This answer will show you how to handle parameters in PHP (using GET method) 答案将向您展示如何在PHP中处理参数(使用GET方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM