Pagination - Codeigniter

Asked

Viewed 337 times

0

Hello, I have a problem paging, the links of the pagination are displayed normally, but regardless of the page I go, the results are always the same, IE, does not change.

Below is the Controller function

    public function index()
{
    if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
    {
        redirect('auth/login', 'refresh');
    }
    else
    {

        /* Título da Página */
        $this->page_title->push(lang('menu_produtos'));
        $this->data['pagetitle'] = $this->page_title->show();

        /* Breadcrumbs */
        $this->data['breadcrumb'] = $this->breadcrumbs->show();

        /* Paginação */
        $this->load->library('pagination');
        $config['base_url'] = base_url('admin/produtos/index');
        $config['total_rows'] = $this->produtos_model->count('produtos');
        $config['per_page'] = 3;            

        $this->pagination->initialize($config);

        /* get produtos */
        //$this->data['produtos'] = $this->produtos_model->get('produtos','id,descricao,preco_compra,preco_venda,estoque,estoque_min','');
        $this->data['produtos'] = $this->produtos_model->get('produtos','id,descricao,preco_compra,preco_venda,estoque,estoque_min','',$config['per_page'],$this->uri->segment(3));

        /* Carrega Template */
        $this->template->admin_render('admin/produtos/index', $this->data);
    }
}

Below is the Model

    public function get($table,$fields,$where='',$perpage=0,$start=0,$one=false,$array='array'){

    $this->db->select($fields);
    $this->db->from($table);
    $this->db->order_by('id','desc');
    $this->db->limit($perpage,$start);
    if($where){
        $this->db->where($where);
    }        
    $query = $this->db->get();

    $result =  !$one  ? $query->result() : $query->row();
    return $result;
}

public function count($table){
    return $this->db->count_all($table);
}

Table

id INT(11) NOT NULL AUTO_INCREMENT, descricao VARCHAR(80) NOT NULL, preco_compra DECIMAL(10,2) NULL DEFAULT NULL, preco_venda DECIMAL(10,2) NOT NULL, estoque INT(11) NOT NULL, estoque_min INT(11) NULL DEFAULT NULL,

  • Have you tried debugging the sql statement to see if it is being generated correctly? Use $this->db->last_query() to retrieve the string from the SQL statement. Then you take this string and do a test in a DBMS (phpmyadmin, for example) to see what will be returned.

  • I did not debug, and I confess that I do not know how to use last_query. .

1 answer

1


Below I am putting your code with the implementation of last_query(), so you can debug.

    public function index()
    {
        if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
        {
            redirect('auth/login', 'refresh');
        }
        else
        {

            /* Título da Página */
            $this->page_title->push(lang('menu_produtos'));
            $this->data['pagetitle'] = $this->page_title->show();

            /* Breadcrumbs */
            $this->data['breadcrumb'] = $this->breadcrumbs->show();

            /* Paginação */
            $this->load->library('pagination');
            $config['base_url'] = base_url('admin/produtos/index');
            $config['total_rows'] = $this->produtos_model->count('produtos');
            $config['per_page'] = 3;            

            $this->pagination->initialize($config);

            /* get produtos */
            //$this->data['produtos'] = $this->produtos_model->get('produtos','id,descricao,preco_compra,preco_venda,estoque,estoque_min','');
            $this->data['produtos'] = $this->produtos_model->get('produtos','id,descricao,preco_compra,preco_venda,estoque,estoque_min','',$config['per_page'],$this->uri->segment(3));
            //EXECUTA O METODO last_query() PARA AUXILIO NO DEBUG
            echo $this->db->last_query();
            //PARA A EXECUÇÃO PARA QUE NADA SEJA IMPRESSO NA TELA, SOMENTE A STRING DA INSTRUÇÃO SQL
            die();

            /* Carrega Template */
            $this->template->admin_render('admin/produtos/index', $this->data);
        }
    }

Wagner, take a look at the Codeigniter documentation so you can make more efficient use of the framework’s resources.

  • Yeah, I read about it, but I didn’t pay any attention to die();. Thank you!

  • On the return, I saw no problems, SELECT id, descricao, preco_compra, preco_venda, estoque, estoque_min FROM produtos ORDER BY id DESC LIMIT 3

  • Note that your SELECT is only returning LIMIT 3, when it should return LIMIT 10, 3 where 10 is the offset value.

  • This, that’s where the problem is, I’ve tried to define a $offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; and yet it doesn’t work.

  • Debug the controller, to see if it is recovering the offset you take in the url. Then debug the model to check that it is receiving the offset parameter and adding it correctly in the SQL statement assembly. Take a look at this link and it can help you: http://www.universidadecodeigniter.com.br/paginacao-de-resultados-com-codeigniter-e-bootstrap/

  • I discovered the problem, and your video helped a lot. $offset = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;. I changed the segment to 4. Thanks

  • Nice that the video helped!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.