du8589840 2017-05-23 11:29
浏览 52
已采纳

PHP Codeigniter高级搜索过滤器

I am writing an advanced search query using Codeigniter Query Builder but its giving wrong results. I have 3 database tables(vehicles, vehicle_pictures, date_ranges) from where I am getting the results. The date_ranges table contains unavailable dates for each vehicle and there can be zero or more unavailable date ranges for each vehicle, I want to know if this approach is good? and what is wrong with my query. Following is a picture and code of my filters. Picture of Filters

public function get_results() {
    $location = strip_tags($this->input->get('location'));
    $location = explode(',', $location ? $location : '');
    $pickup = $this->input->get('pickup');
    $dropoff = $this->input->get('dropoff');
    $min_price = $this->input->get('min_price');
    $max_price = $this->input->get('max_price');
    $types = $this->input->get('types') ? $this->input->get('types') : [];
    $travelers = $this->input->get('people');
    $min_year = $this->input->get('min_year');
    $max_year = $this->input->get('max_year');
    $min_length = $this->input->get('min_length');
    $max_length = $this->input->get('max_length');

    $select = ['vehicles.id', 'vehicles.year', 'vehicles.model', 'vehicles.nightly_rate', 'vehicles.class', 'vehicle_pictures.picture', 'vehicles.people'];
    $this->db->select($select);

    if (!empty($location)) {
        foreach ($location as $loc) {
            $this->db->or_like('country', $loc, 'both')
            ->or_like('state', $loc, 'both')
            ->or_like('city', $loc, 'both')
            ->or_like('street', $loc, 'both')
            ->or_like('zip', $loc, 'both');
        }
    }

    if (!empty($pickup) && !empty($dropoff)) {
        $d1 = date('Y-m-d', strtotime($pickup));
        $d2 = date('Y-m-d', strtotime($dropoff));

        $this->db->where("'$d1' NOT BETWEEN date_ranges.start_date AND date_ranges.end_date AND '$d2' NOT BETWEEN date_ranges.start_date AND date_ranges.end_date OR ('$d1' < date_ranges.start_date AND '$d2' > date_ranges.end_date) ");

    } else {
        if ($pickup && !$dropoff) {
            $d1 = date('Y-m-d', strtotime($pickup));
            $this->db->where("'$d1' NOT BETWEEN date_ranges.start_date AND date_ranges.end_date ");
        }
        if ($dropoff && !$pickup) {
            $d2 = date('Y-m-d', strtotime($dropoff));
            $this->db->where("'$d2' NOT BETWEEN date_ranges.start_date AND date_ranges.end_date ");
        }
    }

    if (!empty($min_price) && !empty($max_price)) {
        $this->db->where("vehicles.nightly_rate BETWEEN '$min_price' AND '$max_price' ");
    } else {
        if (!empty($min_price)) {
            $this->db->where('vehicles.nightly_rate <=', $min_price);
        }
        if (!empty($max_price)) {
            $this->db->where('vehicles.nightly_rate >=', $max_price);
        }
    }

    if (!empty($min_year) && !empty($max_year)) {
        $this->db->where("vehicles.year BETWEEN '$min_year' AND '$max_year' ");
    } else {
        if (!empty($min_year)) {
            $this->db->where('vehicles.year <=', $min_year);
        }
        if (!empty($max_year)) {
            $this->db->where('vehicles.year >=', $max_year);
        }
    }

    if (!empty($min_length) && !empty($max_length)) {
        $this->db->where("vehicles.length BETWEEN '$min_length' AND '$max_length' ");
    } else {
        if (!empty($min_length)) {
            $this->db->where('vehicles.length <=', $min_length);
        }
        if (!empty($max_length)) {
            $this->db->where('vehicles.length >=', $max_length);
        }
    }

    if (!empty($travelers)) {
        $this->db->where('vehicles.people >', $travelers);
    }

    if(!empty($types)) {
        $this->db->where_in('vehicles.class', array_values($types));
    }

    $query = $this->db->join('vehicle_pictures', 'vehicles.id = vehicle_pictures.vehicle_id', 'left')
    ->join('date_ranges', 'vehicles.id = date_ranges.vehicle_id', 'left')
    ->group_by('vehicles.id')
    ->get('vehicles');
    echo '<pre>';
    print_r($query->result_array());
    exit;
}
  • 写回答

1条回答 默认 最新

  • doubi9531 2017-05-23 12:05
    关注

    Try this

    if (!empty($location)) {
                $this->db->group_start();
            foreach ($location as $loc) {
                $this->db->or_like('country', $loc, 'both')
                ->or_like('state', $loc, 'both')
                ->or_like('city', $loc, 'both')
                ->or_like('street', $loc, 'both')
                ->or_like('zip', $loc, 'both');
            }
                $this->db->group_end();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置