Codeigniter 2.1 - returns identifiers after insertion

How to return all identifiers after insert_bunch? Function:

public function insert_slike($id, $slike, $folder, $polje, $tabela)
{
        $slike = explode(',', $slike);
        $i = 1;
        $data = array();
        foreach ($slike as $slk) {
            $this->img_upload_resize($slk, $folder);
            $data[] = array(
                $polje => $id,
                'path' => $slk
                );
            $i++;
        }

        $this->db->insert_batch($tabela, $data);
}
+5
source share
5 answers

Here is an effective method:

Since your records have result identifiers, I'm going to assume that they also automatically increase the number.

If so, you do it and still use insert_batch.

Here is what you do:

1. You take the number of elements that you insert:

$count = count($data);

2. Run the batch insert:

$this->db->insert_batch($table, $data);

3 . Get the first inserted batch id:

$first_id = $this->db->insert_id();

4. Add a counter (minus 1) to your insert ID to get the last record ID.

$last_id = $first_id + ($count-1);

! , , .

:

, , ; InnoDB , , .

+9

MySQL:

MySQL ?

CodeIgniter $this->db->insert_id() .

, , .

+2

1. .

2. , .

3. ID

4. ID

0

insert_batch () returns only the first identifier of your batch. use the insert () function for the loop to get all identifiers.

public function insert_slike($id, $slike, $folder, $polje, $tabela)
{
    $slike = explode(',', $slike);
    $data = array();
    $ids = array();
    foreach ($slike as $slk) {
        $this->img_upload_resize($slk, $folder);
        $data = array(
             $polje => $id,
             'path' => $slk
             );
        $this->db->insert($tabela, $data);
        $ids[]=$this->db->insert_id();
    }
}
0
source

I agree with Mark. CI insert_batch is simply used by foreach for each insert, so if you use a transaction to complete these tasks. You will get the same effect or the best effect, and you can also get all insert_id after insertion.

-2
source

All Articles