带范围过滤器的Php搜索查询


Php search query with range filter

我正在尝试进行搜索查询,但不确定如何将所有内容组合在一起。我的范围过滤器部分有问题。

我正在努力实现的目标:一个搜索表单1.(如果字段A、B(不为空(,则放入搜索查询

2.(使用(价格较低范围,价格较高范围(搜索价格列包括匹配字段A、B(如果不为空(和价格(如果在范围内(的结果。(如果搜索字段A、B为空,则显示范围之间存在的所有结果(。

谢谢你抽出时间。

我现在掌握的密码。

    <?php
    ini_set('display_errors', 1); error_reporting(E_ALL);
    session_start();
    include 'connect.php';
    if($_POST) 
    {
    $A = ($_POST['A']);
    $B = ($_POST['B']);
    $C = ($_POST['C']);
    $pricelow = ($_POST['pricelow']);
    $pricehigh = ($_POST['pricehigh']);
    $sql = array();
    if (!empty($A)) {
        $sql[] = "A='$A'";
    }
    if (!empty($B)) {
        $sql[] = "B='$B'";
    }
    if (!empty($C)) {
        $sql[] = "C='$C'";
    }
    if (!empty($price)) {
    for($i = pricelow; $i<pricehigh; $i++){
        $price = $i;
    }
    $sql[] = "price='$price'";
    $sql = implode(' AND ', $sql);
    $sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . $sql: '');
    $result = mysqli_query($con,$sql);
    $output = array();
    // fetch your results
    while( $row = mysqli_fetch_assoc($result) )
    {
        // add result row to your output's next index
        $output[] = $row;
    }
    // echo the json encoded object
    echo json_encode( $output ); 
    }
    ?>

编辑:

$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . $sql: '') . ("AND" 'price' BETWEEN  "$pricelow" AND "$pricehigh");

编辑:

if (!empty($pricelow) || !empty($pricehigh)) {

        $sql[] =  $pricehigh>= 'price' and  'price'>=$pricelow ;
}
$sql = array();

$sql = implode(' AND ', $sql);
$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . implode(" AND ", $sql): '');
<?php
    ini_set('display_errors', 1); error_reporting(E_ALL);

    session_start();
    include 'connect.php';
    if($_POST) 
    {
    $A = ($_POST['A']);
    $B = ($_POST['B']);
     $C = ($_POST['C']);
     $pricelow = ($_POST['pricelow']);
 $pricehigh = ($_POST['pricehigh']);
     $query = "SELECT * FROM Listing WHERE ";
     $flag = 0;
    if (!empty($A)) {
        $query .= $flag==0?" A='$A' ":" AND A = '$A'";
        $flag = 1;
    }
    if (!empty($B)) {
        $query .= $flag==0?" B = '$B' ":" AND B = '$B'";
        $flag = 1;    
    }
    if (!empty($C)) {
        $query .= $flag==0?" C='$C' ":" AND C= '$C'";
        $flag = 1;
    }
    if ($flag == 0) {
        $query .= " price > $pricelow AND price > $pricehigh"
    }

    $result = mysqli_query($con,$query);
            $output = array();
    // fetch your results
    while( $row = mysqli_fetch_assoc($result) )
    {
        // add result row to your output's next index
        $output[] = $row;
    }
    //your rest of the code
>?

我不能测试,所以,试着报告结果吧!

您正在创建一个条件数组,然后尝试将该数组用作字符串。这将不起作用,因为生成的查询无效。看看这里,implode是一个使用分隔符内爆array的函数。如果您使用" AND "作为分隔符,那么您将获得所需的字符串。所以,不是:

$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . $sql: '');

执行以下操作:

$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . implode(" AND ", $sql): '');

你的问题应该得到解决。

编辑:

我已经阅读了你的编辑,我的意思是这个代码特别:

if (!empty($pricelow) || !empty($pricehigh)) {

        $sql[] =  $pricehigh>= 'price' and  'price'>=$pricelow ;
}
$sql = array();

$sql = implode(' AND ', $sql);
$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . implode(" AND ", $sql): '');

在构建数组之后,您将再次对其进行初始化,从而丢失先前处理的所有信息。然后你把它内爆两次,这是不需要的。试试这个方法:

if (!empty($pricelow) || !empty($pricehigh)) {

        $sql[] =  $pricehigh>= 'price' and  'price'>=$pricelow ;
}
$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . implode(" AND ", $sql): '');