PHP PDO过滤功能

时间:2017-07-04 02:13:24

标签: php filter

我试图为酒店预订过滤器,我无法解决这个问题。如果有人可以帮我一些指示,那将非常有帮助。我的过滤选项是选择城市,房间类型和/或价格。

function filterOffers(){
    $conn = connectDB();

    $query = "SELECT * FROM offer WHERE ";

    if (isset($_POST['judet_id']) && strlen($_POST['judet_id'])>0){
        $query .= " judet_id LIKE :judet_id";
    }



    if (isset($_POST['tip_camera']) && (intval($_POST['tip_camera'])==1 or intval($_POST['tip_camera']) == 2 or intval($_POST['tip_camera']) == 3) ){
        $query .= " and tip_camera = :tip_camera";
    }

    if (isset($_POST['rating']) && (intval($_POST['rating']) > 1 or intval($_POST['rating']) < 5) ){
        $query .= " and 0 < :rating";
    }

    if (isset($_POST['type']) && (intval($_POST['type'])==1 or intval($_POST['type']) == 2) ){
        $query .= " and type = :type";
    }

    if (isset($_POST['pret']) && intval($_POST['pret']) > 0 ){
        $query .= " and 0 < :pret";
    }

    $query .= " and status=1 ORDER BY id ASC";

    $stmt=$conn->prepare($query);

    if (isset($_POST['judet_id']) && strlen($_POST['judet_id'])>0){
        $stmt->bindParam(":judet_id","%".$_POST['judet_id']."%");
    }

    if (isset($_POST['tip_camera']) && (intval($_POST['tip_camera'])==1 or intval($_POST['tip_camera']) == 2 or intval($_POST['tip_camera']) == 3) ){
        $stmt->bindParam(":tip_camera",intval($_POST['tip_camera']));
    }

    if (isset($_POST['rating']) && (intval($_POST['rating']) > 1 or intval($_POST['rating']) < 5) ){
        $rating=$_POST['rating'];
        $stmt->bindParam(":rating",$rating);
    }

    if (isset($_POST['type']) && (intval($_POST['type'])==1 or intval($_POST['type']) == 2) ){
        $stmt->bindParam(":type",intval($_POST['type']));
    }

    if (isset($_POST['pret']) && intval($_POST['pret']) > 0 ){
        $stmt->bindParam(":pret",intval($_POST['pret']));
    }
    // echo $query;
    $stmt->execute();

    $results = array();

    while ($result=$stmt->fetch(PDO::FETCH_OBJ)) {
        $results[]=$result;
    }

    return results;
} // end function

if(isset($_POST['filtrare'])){
    $ultimileOferte=filterOffers();
    // echo "die";die();
}

0 个答案:

没有答案