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

时间:2015-11-25 14:17:11

标签: php postgresql

问题

我无法过滤我的桌子,我已经过滤了产品类别,但我不确定在查看数字时该怎么做。

预期结果

当用户按下提交按钮时,我希望看到的是表格更改为提交的内容。例如,在下拉框中有一个' 0 - 1'的选项,这意味着价格范围小于一磅。如果我按下这个,我希望看到表格变化,只显示价格低于1磅的产品。

实际结果是什么

目前我每次尝试新内容时都会遇到不同的错误,我首先尝试复制类别选项,但这并不起作用。

我可以这样做吗?

对于下拉框

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

不知道该为这个值付出什么。

SQL代码

$'?' = 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");

以下是我目前实施表格的代码

<?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>\n";
        }
    echo "</table>\n";

我需要另一个Where条件

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

有没有更好的方法来为价格范围进行过滤?

1 个答案:

答案 0 :(得分:3)

第一个php不是我的力量,所以把它作为指导。

为每个选项添加一些ID号

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

然后你有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");

注意:

BETWEEN的工作方式与0.00 <= price AND price <= 1.00类似,因此,如果一件商品的价格正好1.00,也会出现在第一个和第二个范围内。

注2:

更好的选择是在数据库中创建表PriceRange,这样您就可以非常轻松地更新或添加新范围,而无需更改php页面。

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

您的查询将类似

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