I got a form with 3 different dropdowns, the idea is to paginate the search results, which works fine on the first page, but when I get two or more pages it displays the entire database and not only my search.
[edit]: okay I updated my code with CI's paginator class. I'm still having trouble with a similar issue. Now it paginates correctly, but when I click on the link it tells me this: Unknown column ' IS NULL LIMIT 9, 9' in 'where clause'
[edit2]: just in case anybody out there finds this, I was able to fix my problem using this method here http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-search-results-without-query-strings-2/
Here is my code.
Model: (I got two functions, one for giving back the entire search results, and the other one to give back results with limit and offset)
function buscarHotel($dpto, $ciudad, $id_precio, $limit = 10, $offset = 0)
{
$w=array();
if (!empty($dpto))
{
$w[]="cod_ciudad IN (SELECT cod_ciudad FROM ciudad WHERE departamento_id = $dpto)";
}
if (!empty($ciudad))
{
$w[]="cod_ciudad IN (SELECT cod_ciudad FROM ciudad WHERE cod_ciudad = $ciudad)";
}
if (!empty($id_precio))
{
$w[]="des_precio_referencial IN (SELECT rango FROM preciohotel WHERE id = $id_precio)";
}
if (count($w)) $where="WHERE ".implode(' AND ',$w); else $where='';
$query = $this->db->query("select id, nom_comercial, cod_ciudad, des_foto
FROM hotel
$where");
#ORDER BY id desc
#LIMIT $limit
#OFFSET $offset ");
return $query->result();
}
function buscarHotelLimit($dpto, $ciudad, $id_precio, $limit)
{
$w=array();
if (!empty($dpto))
{
$w[]="cod_ciudad IN (SELECT cod_ciudad FROM ciudad WHERE departamento_id = $dpto)";
}
if (!empty($ciudad))
{
$w[]="cod_ciudad IN (SELECT cod_ciudad FROM ciudad WHERE cod_ciudad = $ciudad)";
}
if (!empty($id_precio))
{
$w[]="des_precio_referencial IN (SELECT rango FROM preciohotel WHERE id = $id_precio)";
}
if (count($w)) $where=implode(' AND ',$w); else $where='';
/*$query = $this->db->query("select id, nom_comercial, cod_ciudad, des_foto
FROM hotel
$where");*/
$this->db->select('id, nom_comercial, cod_ciudad, des_foto');
$this->db->where("$where");
$query = $this->db->get('hotel', $limit, $this->uri->segment(3));
return $query->result();
}
Controller:
function buscar()
{
$dpto = (int)$this->input->post('departamentos');
$ciudad = (int)$this->input->post('ciudades');
$precio = (int)$this->input->post('precios');
$this->load->model('mhotel');
$this->load->model('mciudad');
$total = (int)count($this->mbuscador->buscarHotel($dpto, $ciudad, $precio));
$limit = 9;
$this->load->library('pagination');
$config['base_url'] = 'http://infohotelperu.com/buscador/buscar';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['num_links'] = 5;
$this->pagination->initialize($config);
$info = $this->mbuscador->buscarHotelLimit($dpto, $ciudad, $precio, $limit);
$results['info'] = $info;
$this->load->view('resultados', $results);
}
And in my view I just got
<?php echo $this->pagination->create_links(); ?>