【问题标题】:Update array in codeigniter在 codeigniter 中更新数组
【发布时间】:2016-01-29 22:19:18
【问题描述】:

我有一个包含 (recipe_id,ingredient_id,category_id) 的表格菜单。我试图更新我的成分,但它只更新 1 个成分_id。像这样

这是我的代码:

控制器:

 public function save_edit_recipe()
{

foreach($this->input->post('ingredients') as $key => $value)
{
    $menuData[] = array('recipe_id'         => intval($this->input->post('recipe_id')),
                        'ingredient_id'     => intval($value),
                        'category_id'       => intval($this->input->post('recipe_category'))
    );
}
// var_dump($menuData); die();
$this->products_model->updatemenu($menuData);

型号:

  public function updatemenu($data)
{




    foreach($data as $row => $value)
    {
        $this->db->where('ingredient_id', $data['ingredient_id']);
        $query=$this->db->update('menu', $value);

    }


    return $result;


}

【问题讨论】:

  • 更新不带wh​​ere子句?
  • $this->input->post('ingredients') 的值是多少
  • 我尝试了 where 子句先生,但它仍然只更新一个成分 ID @devpro
  • 它是另一张桌子上所有成分的名称先生 @devpro
  • 这个数组的值是多少 $this->input->post('ingredients')

标签: php codeigniter updatemodel


【解决方案1】:

删除控制器中的所有数据并添加到模型中。

在控制器中

$this->products_model->updatemenu();

在模型中

public function updatemenu()
{
    foreach($this->input->post('ingredients') as $key => $value)
    {
        $menuData = array(
            'recipe_id'         => intval($this->input->post('recipe_id')),
            'ingredient_id'     => intval($value),
            'category_id'       => intval($this->input->post('recipe_category'))
        );
        $this->db->where('ingredient_id', $value);
        $this->db->update('menu', $menuData);
    }
}

【讨论】:

  • 检查一下。我不是 100% 确定。在检查之前更改您的数据字段,因为它太早了
  • @shikira 我标记了你的评论。您应该公开删除该类型的数据集。
  • 我认为不需要在更新数组中添加成分 ID 兄弟。或者任何其他列将用于 where 子句
【解决方案2】:

我的 html 代码在视图中

<tr class='test_record'>\
                <td><input type='text' name='test_name[]' value='<?= $lists['test_name'];?>' class='form-control'></td>\
                <td><textarea class='form-control' name='instruction[]' > <?= $lists['instruction'];?> </textarea>\
                </td>\
                <input type='hidden' name='test_id' value='<?= $lists['test_id'];?>'>\
                <td><button class='close remove_test_record' aria-hidden='true'>&times;</button></td>\
            </tr>";

我在codeignitor中的php代码是

$count = count($test_name); 


for ($i = 0; $i < $count; $i++) { 
    $i_update = array('test_name' => $test_name[$i], 'instruction'=> $instruction[$i]);
    $result = $this->Common_model->save_updated_info($i_update,array('test_id' =>$update['test_id']),'patients_test');
}


redirect("Doctor/doc_patient_profile/".$update['patient_id']);

【讨论】:

  • 简述你的答案,以便观众容易理解。
猜你喜欢
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
相关资源
最近更新 更多