Update array in codeigniter

MSE picture MSE · Jan 29, 2016 · Viewed 9.4k times · Source

I have a table menu w/c contains of(recipe_id,ingredient_id,category_id).I was trying to update my ingredients yet it only update 1 ingredient_id. Like this

menu

Here is my code :

CONTROLLER:

 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);

MODEL:

  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;


}

Answer

Abdulla Nilam picture Abdulla Nilam · Jan 30, 2016

Remove all data in controller and add in to model.

In Controller

$this->products_model->updatemenu();

In Model

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);
    }
}