dongnvwang8591 2015-11-25 14:17
浏览 29
已采纳

使用SQL命令创建价格范围的下拉框

The Problem

I am having trouble filtering my table, I have filtered it for product category but I am unsure how to do it when looking at numbers.

Expected Outcome

What I expect to see when the user presses the submit button is for the table to change to what ever it was that submitted. For example, in the drop down box there is an option for '0 - 1', this meaning at price range less than a pound. If I was to press this I would expect to see the table change, showing only products with a price less than 1 pound.

What the actual outcome is

At the moment I am getting different errors every time I try something new, I first attempted to copy the category option but that didn't work.

Could I do something like this?

For the drop down box

<form action="database.php" method="post">
<select name="price" id="price">
    <option value="?">All Products</option>
    <option value="?">Less than £1</option>
    <option value="?">More than £1, Less than £5</option>
    <option value="?">More than £5, Less than £10</option>
</select>
<input  type="submit" name="submit" value="Search"/>
</form>

Would not know what to put for the value.

The SQL code

$'?' = pg_query("SELECT Foodtype, Manufacturer, 
    Description, Price FROM food WHERE Price = BETWEEN 0.00 AND 1.00");

$'?' = pg_query("SELECT Foodtype, Manufacturer, 
    Description, Price FROM food WHERE Price = BETWEEN 1.00 AND 5.00");

$'?' = pg_query("SELECT Foodtype, Manufacturer, 
    Description, Price FROM food WHERE Price = BETWEEN 5.00 AND 10.00");

Here is my current code for implement the table

<?php
    $conn = pg_connect("host=hostname.com port=1234
                                        dbname=******* user=guest password=********");
    // Empty var that will be populated if the form is submitted
    $where = '';
        if (isset($_POST['submit'])) {
            if (!empty($_POST['category'])) {
        // Where conditional that will be used in the SQL query
        $where = " WHERE Category = '".pg_escape_string($_POST['category'])."'";
            }
        }
        $res = pg_query($conn, "SELECT Foodtype, Manufacturer, Description, Price 
        FROM food " . $where . " ORDER BY Category ASC");
            echo "<table id=\"myTable\" border='1'>";
            while ($a = pg_fetch_row($res)) {
            echo "<tr>";
        for ($j = 0; $j < pg_num_fields($res); $j++) {
        echo "<td>" . $a[$j] . "</td>";
        }
        echo "<td><form id='cart' name='cart' method='POST' action='addToBasket.php'>
        <input type='submit' name='Select' id='Select' value='Add To Basket'>
        </form></td>";
        echo "</tr>
";
        }
    echo "</table>
";

I will need another Where conditional

$where = " WHERE Price= '".pg_escape_string($_POST['price'])."'";

Is there a better way to do a filter for price range?

  • 写回答

1条回答 默认 最新

  • dqs13465424392 2015-11-25 15:02
    关注

    First php isnt my strength so take it just as guidelines.

    Just put some id number for each option

    <select name="price" id="price">
        <option value="1">All Products</option>
        <option value="2">Less than £1</option>
        <option value="3">More than £1, Less than £5</option>
        <option value="4">More than £5, Less than £10</option>
    

    Then you have the where

    $where = " WHERE Category = '".pg_escape_string($_POST['category'])."'";
    
    switch ($_POST['price']) {
         case 2: 
               $where = $where." and Price BETWEEN 0.00 AND 1.00";
               break;
         case 3: 
               $where = $where." and Price BETWEEN 1.00 AND 5.00";
               break;
         case 4: 
               $where = $where." and Price BETWEEN 5.00 AND 10.00";
               break;
         default:
               break;
    }
    
    $res = pg_query($conn, "SELECT Foodtype, Manufacturer, Description, Price 
        FROM food " . $where . " ORDER BY Category ASC");
    

    NOTE:

    BETWEEN works like 0.00 <= price AND price <= 1.00 so if one item price is exactly 1.00 will also appear on both the first and second range.

    NOTE 2:

    A better option is create a table PriceRange in database this way you can update or add new ranges very easy without have to change the php page.

    RangeID    LowerPrice   UpperPrice
      1            0         1.000.000.000
      2            0              1
      3            1              5 
      4            5             10
    

    Your query will be something like

    SELECT Foodtype, Manufacturer, Description, Price 
    FROM food f
    JOIN PriceRange p
      ON f.Price BETWEEN p.LowerPrice AND p.UpperPrice
    WHERE Category = $_POST['category']
      AND RangeID = $_POST['price']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误