limit no codeigniter

Asked

Viewed 2,299 times

1

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

EX: $this->db->limit(0,5); In this code he would take the total number of records minus the five that’s in the code and show what’s left. I need him to take the total and show only two of these 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"));

    }
  • I see you’re using the library https://github.com/IgnitedDatatables/Ignited-Datatables . This library I use for a table when you have a lot of data and you need ajax that will do that limit by "behind the scenes". I would advise to change things a little, if you just really want to show two records, so I imagine you do not need to bring these two data by ajax, so why not use do the normal way? But then there’d be some changes.

3 answers

0

Only the first parameter of the method shall be used limit(), which is the quantity, because the second parameter is the offset (dislocation):

$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 also the clause is not executed. This makes you think you didn’t program right.

0

Reverse the order of limit arguments: $this->db->limit(5,0), because it will generate an expression LIMIT 0,5, where 0 is offset and 5 is Count. As it is doing, it is offset by 5 and limiting to 0, which returns everything else.

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

Hug.

  • I made the change, but she returned all the bank records the way you sent me. I need him to only show up two of all the records. Thanks in advance. Hug

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 way it works for all banks:

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

See the Codeigniter Documentation

Browser other questions tagged

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