limit in codeigniter

1

I have a code that takes the data from the database and shows it in a table. I need only the first two records to appear. For this I put a limit on my code. But it is not limiting, but taking the total and decreasing by the number.

EX: $this->db->limit(0,5); In that code it would take the total number of records minus the 5 that is in the code and show what was left. I need him to get the total and show only 2 of those records.

MODEL:

public function getDoisLaudos($vei, $est, $urb, $rur, $aca) {
    $this->datatables->select('ls.la_id, ls.la_tipo, ls.la_data, usu.usr_nome, disp.dsp_nome, ls.la_densidade, ls.la_nivel, ls.la_zona, ls.la_status');
            $this->datatables->from('ls');
            $this->datatables->join('usu', 'ls.usr_id = usu.usr_id');
            $this->datatables->join('disp', 'ls.dsp_id = disp.dsp_id');
            $this->datatables->join('empresa', 'empresa.emp_id = usu.emp_id');
            $this->datatables->where('usu.emp_id = ' . $this->session->userdata('emp_id') . ' AND usu.usr_nivel<>0');
            $this->db->limit(0,5);

            $this->datatables->unset_column('ls.la_id');
            $this->datatables->unset_column('ls.la_status');
            $this->datatables->add_column('Ações', '$1', 'get_buttons_laudos(ls.la_id, ls.la_status, ls.la_tipo,' . $aca . ')');
            $this->datatables->edit_column('ls.la_tipo', '$1', 'trataLauTipo(ls.la_tipo,' . $vei . ',' . $est . ')');
            $this->datatables->edit_column('ls.la_data', '$1', 'tratarDataHora(ls.la_data)');
            $this->datatables->edit_column('ls.la_zona', '$1', 'trataLauPerimetro(ls.la_zona,' . $urb . ',' . $rur . ')');
            $this->datatables->edit_column('ls.la_status', '$1', 'trataLauStatus(ls.la_status)');
            return $this->datatables->generate();
}

Controller:

public function generateTable() {
            echo $this->ML->getDoisLaudos($this->lang->line("con_laudo_tip_insp_v"), $this->lang->line("con_laudo_tip_insp_e"), $this->lang->line("con_laudo_per_insp_u"), $this->lang->line("con_laudo_per_insp_r"), $this->lang->line("con_laudo_acoes_insp"));

    }
    
asked by anonymous 21.07.2015 / 15:04

3 answers

0

Invert the order of the limit arguments: $this->db->limit(5,0) , as this will generate a LIMIT 0,5 expression, where 0 is offset and 5 is count. In the way you're doing, it's giving an offset of 5 and limiting to 0, which makes all the rest go back.

In the official documentation you can better understand these details: Active Record: CodeIgniter User Guide .

Hug.

    
21.07.2015 / 15:45
0

You should only use the first parameter of the limit() method, which is the quantity, because the second parameter is the offset (offset):

$this->db->limit(2);

However, not all banks have an implementation of this functionality, such as Microsoft SQL Server 2000. If you are using such a database, no error is generated, but the clause is not executed either. This makes you think you did not program right.

    
27.07.2017 / 17:03
0

If it's in Mysql you can use it like this:

$this->db->limit(0, 5);   // Produz: LIMIT 5, 0 (no MySQL. Outros bancos de dados possuem uma sintaxe ligeiramente diferente)

This works for all banks:

$this->db->limit(5);   // produz: LIMIT 5

See Codeigniter Documentation

    
19.07.2018 / 20:10