无法在PHP中使用搜索逻辑

时间:2015-07-12 04:01:09

标签: php html forms

所以我试图获得一个具有多重价值的表格。我喜欢最低价格范围,最大值,面积,城市以及单词的搜索查询。我已经创建了单独的"块"检查每个然后得到结果,我计划或合并阵列。这是一个很好的方法吗?这就是我所得到的。

//Get post response
if(isset($_POST['query']) && isset($_POST['price1']) && isset($_POST['price2']) && isset($_POST['city']) && isset($_POST['area'])){
    $query = $_POST['query'];
    $price1a = $_POST['price1'];
    $price2a = $_POST['price2'];
    $city = $_POST['city'];
    $area = $_POST['area'];
}

//Make sure price is INT
$price1 = (int)$price1a;
$price2 = (int)$price2a;

echo $price1;

//Check to see which fields are empty
if(empty($query) && empty($price1) && empty($price2) && empty($city) && empty($area)){
    //User didn't bother entering anything, just return everything
    $stmt = $con->prepare("SELECT * FROM wp_posts");
    $stmt->execute();

    //Return as JSON string for parsing via front
    $json = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
    echo $json;
}

//Price range for two INT
if(!empty($price1) || !empty($price2)){
    $stmt = $con->prepare("SELECT * FROM wp_posts WHERE price BETWEEN :price1 AND :price2");
    $stmt->bindValue(':price1', $price1);
    $stmt->bindValue(':price2', $price2);
    $stmt->execute();

    $json = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
    echo $json;
}

//Price range for one INT, no max
if(!empty($price1) && empty($price2)){
    //Only price two is empty, therefore no max, so set a large number
    $max = 100000000;
    $stmt = $con->prepare("SELECT * FROM wp_posts WHERE price BETWEEN :price1 aND :price2");
    $stmt->bindValue(':price1', $price1);
    $stmt->bindValue(':price2', $max);
    $stmt->execute();

    $json = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
    echo $json;
}

这只是一小部分,但两个较低的if语句显示最低和最高价格。我计划在获得这两个结果然后合并JSON。这会有用吗?任何更好的方式进行多输入搜索"?

0 个答案:

没有答案