How to create a single array using two iterating loop and than update_batch
09:29 12 Apr 2019

How do I take id on every iteration from check_seeds array and add on each iteration into seeded[] array.

In more simple words, I want to take an item from the first iteration and add into the first iteration, take an item from the second iteration and add into the second iteration and so on...

Actually, on update_batch we need third parameter (primary key, index) to update array values in database rows where id from database rows matches with the id in update_batch.

$check_seeds = $this->tournament_model->get_seeds($tournament_id);
$seeds = $this->input->post('seed');
foreach ($seeds as $key => $value) {
    if (!empty($key) && !empty($value)) {
        $seeded[] = array(
            'id' => (Add id here),
            'tournament_id' => $tournament_id,
            'stage_id' => $stage_id,
            'seed_id' => $value,
            'team_name' => $key,
        );
        $this->db->update_batch('tournament_seed', $seeded, 'id');
        redirect('organizer/tournaments);
    }
}

print_r($check_seeds)

Array
(
    [0] => Array
        (
            [id] => 3
            [tournament_id] => 713161746
            [stage_id] => 3
            [seed_id] => 3
            [team_name] => -V-ATTAX
        )

    [1] => Array
        (
            [id] => 4
            [tournament_id] => 713161746
            [stage_id] => 3
            [seed_id] => 3
            [team_name] => -V-ATTAX
        )

    [2] => Array
        (
            [id] => 5
            [tournament_id] => 713161746
            [stage_id] => 3
            [seed_id] => 3
            [team_name] => -V-ATTAX
        )

)

I need to update table "tournament_seed" in the database, but first I need to get row ids which we will be going to update using update_batch. Look at print_r($check_seeds). Let's suppose in our case those are three ids 3, 4 & 5. Now I want to place them one by one into seeded[] array and update my database. On the first iteration I will get 3 and than we will place that 3 in seeded[] array and update. than second iteration start and we take 4 and place it in second iteration on seeded[] array and so on.

php codeigniter sql-update codeigniter-3 query-builder