Search code examples
phpcodeigniter

using BETWEEN in WHERE condition


I'd like the following function to select hotels with an accomodation between a certain $minvalue and $maxvalue. What would be the best way to do that?

function gethotels($state_id,$city,$accommodation,$minvalue,$maxvalue,$limit,$pgoffset)
    {
        $this->db->limit($limit, $pgoffset);
        $this->db->order_by("id", "desc");
        $this->db->where('state_id',$state_id);
        $this->db->where('city',$city);

        // This one should become a between selector
        $this->db->where($accommodation,$minvalue); 

        $result_hotels = $this->db->get('hotels');
        return $result_hotels->result();

   }

Solution

  • You should use

    $this->db->where('$accommodation >=', $minvalue);
    $this->db->where('$accommodation <=', $maxvalue);
    

    I'm not sure of syntax, so I beg your pardon if it's not correct.
    Anyway BETWEEN is implemented using >=min && <=max.
    This is the meaning of my example.

    Looking at this link I think you could write:

    $this->db->where("$accommodation BETWEEN '$minvalue' AND '$maxvalue'");