【问题标题】:PHP Codeigniter Advanced Search FilterPHP Codeigniter 高级搜索过滤器
【发布时间】:2017-05-23 11:29:49
【问题描述】:

我正在使用 Codeigniter Query Builder 编写高级搜索查询,但它给出了错误的结果。我有 3 个数据库表 (vehicles, vehicle_pictures, date_ranges) 从中获取结果。 date_ranges 表包含每辆车的不可用日期,每辆车可以有零个或多个不可用日期范围,我想知道这种方法是否有效?以及我的查询有什么问题。以下是我的过滤器的图片和代码。 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;
}

【问题讨论】:

  • 你的 CI 版本是多少?
  • @sintakonte CI 版本是 3.1.3。
  • or_like 部分保留在组内
  • @Rajesh 你能解释一下你的意思吗?
  • 将其添加为答案,因为无法在评论中发布代码。试试看

标签: php mysql sql codeigniter query-builder


【解决方案1】:

试试这个

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();
    }

【讨论】:

  • 非常感谢@Rajesh,它现在正在工作,你拯救了我的一天。 :-)
  • 很高兴有帮助:)
  • Rajesh 现在我有另一个问题,如果车辆有 2 个或更多不可用的日期范围,那么日期检查在一个范围内正常工作时不起作用。
  • 删除这部分来查看OR ('$d1' &lt; date_ranges.start_date AND '$d2' &gt; date_ranges.end_date)
猜你喜欢
  • 2021-12-09
  • 2023-03-26
  • 2015-04-13
  • 2015-05-20
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多