2017-05-23 1 views
0

Je suis en train d'écrire une requête de recherche avancée en utilisant Codeigniter Query Builder mais il donne des résultats erronés. J'ai 3 tables de base de données (vehicles, vehicle_pictures, date_ranges) d'où j'obtiens les résultats. Le tableau date_ranges contient des dates non disponibles pour chaque véhicule et il peut y avoir une ou plusieurs dates d'indisponibilité pour chaque véhicule, je veux savoir si cette approche est bonne? et quel est le problème avec ma requête. Voici une image et un code de mes filtres. Picture of FiltersPHP Codeigniter Recherche avancée Filtre

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; 
} 
+0

Quelle est votre version CI? – sintakonte

+0

@sintakonte la version CI est 3.1.3. – BTree

+0

Gardez la partie 'or_like' dans le groupe – Rajesh

Répondre

1

Essayez cette

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

Merci beaucoup @Rajesh ça marche maintenant, vous avez sauvé ma journée. :-) – BTree

+0

Heureux qui a aidé :) – Rajesh

+0

Rajesh maintenant j'ai un autre problème, s'il y a 2 ou plusieurs plages de dates indisponibles pour un véhicule alors la vérification de la date ne fonctionne pas alors que cela fonctionne bien pour une gamme. – BTree