Error Pagination CodeIgniter 2

0

I made a system in CI 2.0 and detected a paging error. The configuration script is as follows:

     $this->load->library('pagination');
   $query = $this->usuarioModel->obterRelacaoUsuarios($pagina);
            $config = array();
            $config['use_page_numbers'] = TRUE;
            $config['uri_segment'] = 3; // depends on how you passing your page number
            $config['base_url'] = base_url('usuario/listar');
            $config['total_rows'] = $qtdUsuario = $this->db->get_where('usuario', array('bol_excluido' => 'N', 'lotacao' => $this->session->userdata('lotacao')))->num_rows();
            $config['per_page'] = 10;
            $config['full_tag_open'] = '<ul class="pagination pagination-sm">';
            $config['full_tag_close'] = '</ul>';
            $config['first_link'] = 'Primeiro';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['last_link'] = 'Último';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';

            $config['cur_tag_open'] = '<li class="active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';

            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';

            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';

            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $this->pagination->initialize($config);

I noticed that when calling Sql:

 select * from usuarios LIMIT 10 OFFSET $pagina

Instead of paging it every 10, the sqls mounted are:

select * from usuarios LIMIT 10 OFFSET 0
 select * from usuarios LIMIT 10 OFFSET 2
 select * from usuarios LIMIT 10 OFFSET 3

To solve the problem I have added the following logic, before running sql:

 $pagina = ($pagina == 0) ? 0 : ($pagina * 10) - 10;

So sql becomes:

    select * from usuarios LIMIT 10 OFFSET 10
    select * from usuarios LIMIT 10 OFFSET 20
    select * from usuarios LIMIT 10 OFFSET 30

My question is how to fix this bug without having to add any logic and why the variable $ config ['per_page'] = 10; not working?

    
asked by anonymous 03.12.2014 / 12:23

1 answer

1

In your sql in the model, limit the values sent by the $this->uri->segment('segmento') of the CI and its $config['per_page'] .

For easy viewing I'll use:

LIMIT X,Y
<?php 
//pagina_model.php

function obterRelacaoUsuarios($pagina = 'usuario');
    return($this->db->query(
       "SELECT * FROM $pagina LIMIT ?,?", 
          array(
              (is_null($this->uri->segment("3 ou 4 ou 5"))) ? 0 : intval($this->uri->segment("3 ou 4 ou 5")),
               $config['per_page'])
          )
    );
}
    
20.03.2015 / 20:59