sirahanzo
sirahanzo

Reputation: 5

how save from multi checkbox item with codeiginiter

I'am have assigment scholl project with codeigniter, i want to save a value form many checkbox item with codeigniter with simple script this is my script: my controller :

 class Item extends CI_Controller{  
     function save() { 
        $item1 = $this->input->post('item1');
        $item2 = $this->input->post('item2');
          ...............
        $item100 = $this->input->post('item100');       
        $this->item_model->save($item1,$item2,..........$item100);        
      } 
}

and this my model:

class Item_model extends CI_Model{
     function save($item1,$item2,............,$item100) { 
        $data= array( 'item1'=> $item1,'item2'=> $item2,......... ,'item100'=> $item100);
        $this->db->insert('tbl_item',$data);
    }
 }

can someone help me ,how to simple it

Upvotes: 0

Views: 61

Answers (2)

Nabeel Qadri
Nabeel Qadri

Reputation: 109

First Change the name of Checkbox like

<input type="checkbox" name="item[]">

Then Change Save code like

$items = $this->input->post('item');
$loopcount = sizeof($items);
for($i =0 ; $i<= $loopcount; $i++)
{
    //your save code here
     $this->item_model->save($items[$i]);
}

Upvotes: 0

user3705761
user3705761

Reputation:

Use array as name of your all checkbox

<input type="checkbox" name="input[]">
<input type="checkbox" name="input[]">
.
.
<input type="checkbox" name="input[]"><!--all 100 checkbox-->

So you can get array as input

$items = $this->input->post('item');

So you can simply call save function with one parameter

$this->item_model->save($items); 

Note: Request only get value of checked checkbox.

Upvotes: 1

Related Questions