PHP使用查询更改订单和价格过滤器

时间:2014-02-17 08:32:18

标签: php

我正在为我的网站制作一个过滤器,看起来我的逻辑已经不多了! 我设法根据所选的单选按钮对项目的顺序进行排序,但我不知道如何整合价格输入(来自:__ To:__)

在index.php中:

$price_range ='';
if(isset($_POST['submit'])){
    $lowest_price = $_POST['lowest_price'];
    $highest_price = $_POST['highest_price'];
    $price_range = "`price` BETWEEN LIKE '%" .$lowest_price."%' AND LIKE '%" .$highest_price."%'";
                    }

    $order_list=(isset($_POST['order_list']))?$_POST['order_list']:'';                      
switch($order_list) {
    case "link1":   $order_list_sql = "ORDER BY `date` DESC";       break;
    case "link2":   $order_list_sql = "ORDER BY `price` ASC";       break;
    case "link3":   $order_list_sql = "ORDER BY `price` DESC";      break;
    case "link4":   $order_list_sql = "ORDER BY `id` DESC";         break;
    default:        $order_list_sql = "ORDER BY `date` DESC";   
                    }

$res =  mysql_query("SELECT * FROM `classifieds` ".$order_list_sql."    LIMIT 10");

在form.php中:

<form action="index.php" method="post" enctype="multipart/form-data">
    <li>Ad Order
        <ul>
            <li>
                <label>
                    <input type="radio" name="order_list" value="link1" <?php echo (isset($_POST[ 'order_list'])=='link1' ? 'checked': '')?>>Newest First</label>
            </li>
            <li>
                <label>
                    <input type="radio" name="order_list" value="link2" <?php echo (isset($_POST[ 'order_list'])=='link2' ? 'checked': '')?>>Lowest Price First</label>
            </li>
            <li>
                <label>
                    <input type="radio" name="order_list" value="link3" <?php echo (isset($_POST[ 'order_list'])=='link3' ? 'checked': '')?>>Highest Price First</label>
            </li>
            <li>
                <label>
                    <input type="radio" name="order_list" value="link4" <?php echo (isset($_POST[ 'order_list'])=='link4' ? 'checked': '')?>>Nearest First</label>
            </li>
        </ul>
    </li>
    <li>Price
        <ul>
            <li>From: $
                <input type="text" placeholder="0.00" size="8" name="lowest_price" value="<?php if(isset($_POST['lowest_price'])) { echo $_POST['lowest_price']; } ?>">
            </li>
            <li>To: $
                <input type="text" size="8" name="highest_price" value="<?php if(isset($_POST['highest_price'])) { echo $_POST['highest_price']; } ?>">
            </li>
        </ul>
    </li>
    <input type="submit" value="Save">
</form>

更新: 我到目前为止所尝试的但是似乎正确地进入了其他声明

    if(isset($_POST['submit']) === true && empty($_POST['submit']) === false){
    $lowest_price = $_POST['lowest_price'];
    $highest_price = $_POST['highest_price'];
    $price_range = "WHERE `price` BETWEEN ".$lowest_price." AND ".$highest_price."";

    if(empty($_POST['order_list']) === false && $_POST['order_list'] == "link1"){
        $order_list = "ORDER BY `date` DESC";   
    }
    if(empty($_POST['order_list']) === false && $_POST['order_list'] == "link2"){
        $order_list = "ORDER BY `price` ASC";   
    }
    if(empty($_POST['order_list']) === false && $_POST['order_list'] == "link3"){
        $order_list = "ORDER BY `price` DESC";
    }
    if(empty($_POST['order_list']) === false && $_POST['order_list'] == "link4"){
        $order_list = "ORDER BY `id` DESC";     
    }                           
    $res =  mysql_query("SELECT * FROM `classifieds` ".$price_range." ".$order_list." LIMIT 10");
}else{
    $res =  mysql_query("SELECT * FROM `classifieds` ORDER BY `date` DESC LIMIT 10");
}

1 个答案:

答案 0 :(得分:0)

将$ price_range更改为:

$price_range = "WHERE price BETWEEN ".$lowest_price." AND ".$highest_price." ";

将$ res更改为

$res =  mysql_query("SELECT * FROM classifieds ".$price_range." ".$order_list_sql."    LIMIT 10");

让我知道结果..