如何使用两个迭代循环而不是update_batch创建单个数组

时间:2019-04-12 13:54:45

标签: codeigniter codeigniter-3

如何在id数组的每次迭代中使用check_seeds并将每个事件添加到seeded[]数组中。

更简单地说,我想从第一次迭代中获取一个项目并添加到第一次迭代中,从第二次迭代中获取一个项目并添加到第二次迭代中,依此类推...

实际上,在update_batch上,我们需要第三个参数(主键,索引)来更新数据库行中的数组值,其中数据库行中的idid中的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
        )

)

1 个答案:

答案 0 :(得分:0)

在模型函数get_seeds()中,您可以查询id的当前最大值作为别名,并将其与查询结果一起返回:

i1 <- text_table[, Reduce(`+`, lapply(text_patterns, 
       function(x) str_detect(text, x))) >1]
text_table[i1]
#    ID                                         text
#1:  1 lucy, sarah and paul live on the same street
#2:  3                   lucy and sarah are cousins

然后在控制器的for_each()中增加该值:

function get_seeds($tournament_id) {
    $this->db->select_max('id', 'max_id');
    $this->db->where('tournament_id', $tournament_id);
    $result = $this->db->get('tournament_seed');
    return $result->result();
}

Codeigniter文档:Selecting Data,向下滚动至select_max(),因为没有内部书签