[eluser]ReyPM[/eluser]
Hi, I'm having a problem while trying to paginate a result because I get a non results page at last and don't know the cause. This is the query I run for get the total:
Code: SELECT COUNT(autores.id_autor) AS quantity
FROM (`autores`)
LEFT JOIN `autores_filiacion` ON `autores`.`id_autor` = `autores_filiacion`.`id_autor`
LEFT JOIN `filiacion` ON `autores_filiacion`.`id_filiacion` = `filiacion`.`id_filiacion`
WHERE `autores`.`genero` = 'M'
AND `autores`.`nombre` LIKE '%jose%'
This query returns 105 as COUNT. Then this is the query for each iteration:
Code: 1. SELECT DISTINCT `autores`.`id_autor`, `autores`.`nombre`, `autores`.`apellido`, `autores`.`nacionalid`
FROM (`autores`)
LEFT JOIN `autores_filiacion` ON `autores`.`id_autor` = `autores_filiacion`.`id_autor`
LEFT JOIN `filiacion` ON `autores_filiacion`.`id_filiacion` = `filiacion`.`id_filiacion`
WHERE `autores`.`genero` = 'M'
AND `autores`.`nombre` LIKE '%jose%'
LIMIT 25
2. SELECT DISTINCT `autores`.`id_autor`, `autores`.`nombre`, `autores`.`apellido`, `autores`.`nacionalid`
FROM (`autores`)
LEFT JOIN `autores_filiacion` ON `autores`.`id_autor` = `autores_filiacion`.`id_autor`
LEFT JOIN `filiacion` ON `autores_filiacion`.`id_filiacion` = `filiacion`.`id_filiacion`
WHERE `autores`.`genero` = 'M'
AND `autores`.`nombre` LIKE '%jose%'
LIMIT 25, 25
3. SELECT DISTINCT `autores`.`id_autor`, `autores`.`nombre`, `autores`.`apellido`, `autores`.`nacionalid`
FROM (`autores`)
LEFT JOIN `autores_filiacion` ON `autores`.`id_autor` = `autores_filiacion`.`id_autor`
LEFT JOIN `filiacion` ON `autores_filiacion`.`id_filiacion` = `filiacion`.`id_filiacion`
WHERE `autores`.`genero` = 'M'
AND `autores`.`nombre` LIKE '%jose%'
LIMIT 50, 25
4. SELECT DISTINCT `autores`.`id_autor`, `autores`.`nombre`, `autores`.`apellido`, `autores`.`nacionalid`
FROM (`autores`)
LEFT JOIN `autores_filiacion` ON `autores`.`id_autor` = `autores_filiacion`.`id_autor`
LEFT JOIN `filiacion` ON `autores_filiacion`.`id_filiacion` = `filiacion`.`id_filiacion`
WHERE `autores`.`genero` = 'M'
AND `autores`.`nombre` LIKE '%jose%'
LIMIT 75, 25
5. SELECT DISTINCT `autores`.`id_autor`, `autores`.`nombre`, `autores`.`apellido`, `autores`.`nacionalid`
FROM (`autores`)
LEFT JOIN `autores_filiacion` ON `autores`.`id_autor` = `autores_filiacion`.`id_autor`
LEFT JOIN `filiacion` ON `autores_filiacion`.`id_filiacion` = `filiacion`.`id_filiacion`
WHERE `autores`.`genero` = 'M'
AND `autores`.`nombre` LIKE '%jose%'
LIMIT 100, 25
When the LIMIT 100,25 is reached I get "No results" for pagination. Why? This is the code behind those queries:
controller:
Code: public function find() {
$this->session->set_userdata($_POST);
$search_by = $this->uri->segment(3, 'by_id');
$this->data_view['search_criteria'] = $data = $this->session->all_userdata();
switch ($search_by) {
case "by_id":
redirect('cauthor/update/' . $this->input->post('number_aut'));
break;
case "by_data":
case "by_filiation":
$this->load->library('pagination');
$config['base_url'] = site_url('cauthor/find/by_data') . '/pagina';
$config['total_rows'] = $this->Mauthor->getCountAuthorData($data);
$config['per_page'] = 25;
$config['uri_segment'] = 5;
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$this->data_view['searchres'] = $this->Mauthor->getAllAuthorData($data, $config['per_page'], $page);
$this->data_view['pagination'] = $this->pagination->create_links();
break;
default:
break;
}
$this->load->view('author/search_results', $this->data_view);
}
model:
Code: public function getAllAuthorData($data = array(), $limit = 10, $offset = 0, $criteria = NULL) {
$this->db->query("SET NAMES 'utf8'");
$this->db->select('autores.id_autor, autores.nombre, autores.apellido, autores.nacionalid');
$this->db->from('autores');
$this->db->distinct();
if (!empty($data['nombre']) && $data['nombre'] != "") {
$this->db->like('autores.nombre', $data['nombre'], 'both');
}
if (!empty($data['apellido']) && $data['apellido'] != "") {
$this->db->like('autores.apellido', $data['apellido'], 'both');
}
if (!empty($data['gender']) && $data['gender'] != "") {
$this->db->or_where('autores.genero', $data['gender']);
}
if (!empty($data['nacionalidad']) && $data['nacionalidad'] != "") {
$this->db->or_where('autores.nacionalid', $data['nacionalidad']);
}
if (!empty($data['cedula']) && $data['cedula'] != "cedula") {
$this->db->or_where('autores.ci', $data['cedula']);
}
if (!empty($data['fecha_nacimiento']) && $data['fecha_nacimiento'] != "fecha_nacimiento") {
$this->db->or_where('autores.fecha_naci', $data['fecha_nacimiento']);
}
if (!empty($data['email']) && $data['email'] != "") {
$this->db->or_where('autores.email', $data['email']);
}
if (!empty($data['email_alternativo']) && $data['email_alternativo'] != "") {
$this->db->or_where('autores.email_1', $data['email_alternativo']);
}
$this->db->join($this->tbl_autores_filiacion, 'autores.id_autor = autores_filiacion.id_autor', 'left');
$this->db->join($this->tbl_filiacion, 'autores_filiacion.id_filiacion = filiacion.id_filiacion', 'left');
if (!empty($data['ciudad']) && $data['ciudad'] != "") {
$this->db->join($this->tbl_ciudad, 'filiacion.id_ciudad = ciudades.id_ciudad', 'left');
$this->db->or_like('ciudades.ciudad', $data['ciudad'], 'both');
}
if (!empty($data['pais']) && $data['pais'] != "") {
$this->db->join($this->tbl_pais, 'filiacion.id_pais = paises.id_pais', 'left');
$this->db->like('paises.pais', $data['pais'], 'both');
}
$this->db->limit($limit, $offset);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
}
What I'm doing wrong
NOTE: code continue in next post
[eluser]ReyPM[/eluser]
This is the rest of the code, the COUNT function:
Code: public function getCountAuthorData($data = array(), $limit = 10, $offset = 0, $criteria = NULL) {
$this->db->select('COUNT(autores.id_autor) AS quantity');
$this->db->from('autores');
if (!empty($data['nombre']) && $data['nombre'] != NULL) {
$this->db->like('autores.nombre', $data['nombre'], 'both');
}
if (!empty($data['apellido']) && $data['apellido'] != NULL) {
$this->db->like('autores.apellido', $data['apellido'], 'both');
}
if (!empty($data['gender']) && $data['gender'] != "") {
$this->db->or_where('autores.genero', $data['gender']);
}
if (!empty($data['nacionalidad']) && $data['nacionalidad'] != "") {
$this->db->or_where('autores.nacionalid', $data['nacionalidad']);
}
if (!empty($data['cedula']) && $data['cedula'] != "cedula") {
$this->db->or_where('autores.ci', $data['cedula']);
}
if (!empty($data['fecha_nacimiento']) && $data['fecha_nacimiento'] != "fecha_nacimiento") {
$this->db->or_where('autores.fecha_naci', $data['fecha_nacimiento']);
}
if (!empty($data['email']) && $data['email'] != "") {
$this->db->or_where('autores.email', $data['email']);
}
if (!empty($data['email_alternativo']) && $data['email_alternativo'] != "") {
$this->db->or_where('autores.email_1', $data['email_alternativo']);
}
$this->db->join($this->tbl_autores_filiacion, 'autores.id_autor = autores_filiacion.id_autor', 'left');
$this->db->join($this->tbl_filiacion, 'autores_filiacion.id_filiacion = filiacion.id_filiacion', 'left');
if (!empty($data['ciudad']) && $data['ciudad'] != "") {
$this->db->join($this->tbl_ciudad, 'filiacion.id_ciudad = ciudades.id_ciudad', 'left');
$this->db->or_like('ciudades.ciudad', $data['ciudad'], 'both');
}
if (!empty($data['pais']) && $data['pais'] != "") {
$this->db->join($this->tbl_pais, 'filiacion.id_pais = paises.id_pais', 'left');
$this->db->like('paises.pais', $data['pais'], 'both');
}
$query = $this->db->get();
return $query->row()->quantity;
}
[eluser]Aken[/eluser]
Try removing $this->db->distinct(). Not sure why you have that on there anyway.
|