Welcome Guest, Not a member yet? Register   Sign In
Insert checkboxes values to database with assigned field
#1

[eluser]Ignis Avis[/eluser]
I am inserting values like below from my form

Code:
<span class="field">&lt;input type="text" name="item_name" id="item_name" class="input-small" value="&lt;?php echo set_value('item_name'); ?&gt;" /&gt;    
    
    <span class="field">&lt;input type="checkbox" name="tag_id[]" value="&lt;?php echo $value['tag_id']; ?&gt;" /&gt;&lt;/span>

for first field I wanna insert into following item_table
Code:
+----------+--------------+
    | item_id  |   item_name  |
    +-------------+-----------+
    | 1        | A            |
    | 2        | B            |
    | 3        | C            |
    | 4        | D            |
    | 5        | E            |
    +----------+--------------+

for second field I want to insert checkboxes values to following table like these.

Code:
+----------+--------------+
    | item_id  |     tag_id  |
    +-------------+-----------+
    | 1        | 1            |
    | 1        | 2            |
    | 2        | 1            |
    | 2        | 2            |
    | 2        | 3            |
    +----------+--------------+
In my model function

Code:
$item_name = $this->input->post('item_name');
            
            $data_to_store = array(
                'item_name' => $item_name            
            );
            $insert = $this->db->insert('item_table', $data_to_store);
            return $insert;

How should I insert checkboxes values to second table according to Item id?
#2

[eluser]rajneeshgobin[/eluser]
i have a similer problem i have somehow managed to find a way to do that, feel free to optimise my solution

view [ here i am listing the checkboxes, for me the sub checkbox falls under some main category



Code:
<table >
&lt;?php foreach ($query_item as $row): { ?&gt;
<tr>
    <th>&lt;?php echo $row['itemName'];?&gt;</th>

</tr>    


&lt;?php foreach ($query_option as $row_options): { if($row_options['item_id']==$row['id']) {?&gt;
<tr>
    <td>&lt;?php echo $row_options['itemoption'];?&gt;</td>
    <td>&lt;input type="checkbox" id="&lt;?=$row_options['itemoption']?&gt;testName" name="options[]" value="&lt;?php echo $row_options['id'];?&gt;_checked"&gt;&lt;/input></td>
    <td>&lt;input type="hidden" id="&lt;?=$row_options['itemoption']?&gt;testNameHidden" name="options[]" value="&lt;?php echo $row_options['id'];?&gt;_unchecked"&gt;&lt;/input></td>

</tr>    
&lt;?php }} ?&gt;
&lt;?php endforeach; ?&gt;
&lt;?php } ?&gt;
&lt;?php endforeach; ?&gt;
    <td>
      &lt;input name="add" type="submit" value="add" alt="add" /&gt;
    </td>
</table>
post unchecked checkbox

this is the option_submit i am calling to save the options [ &lt;?php echo form_open('usersctrl/option_submit/',array( 'id' => 'useradd')); ?&gt;]

Code:
public function option_submit($page = 'option_add')
    {
        $data['title'] = ucfirst($page);
  
  
  
    $this->load->model('item_model');    
    $this->load->model('options_model');    
    $this->load->model('user_item_options_model');    
  
    $data['query_item'] = $this->item_model->allitems();
    $data['query_option'] = $this->options_model->allOptions();
  
        //$this->form_validation->set_rules('password', 'date', 'required');
      
            $data['query'] = $this->user_item_options_model->insertuseritemOptions();
            
            if ($data['query'] == NULL) {
                
                $data['ins_msg'] = ' inset';
                $this->load->view("site_header");
                $this->load->view("site_nav");
                $this->load->view("admin/users/users_added", $data);
                $this->load->view("site_footer");
            } else {
                $data['ins_msg'] = 'Not inseted';
                $this->load->view("site_header");
                $this->load->view("site_nav");
                $this->load->view("admin/users/users_add", $data);
                $this->load->view("site_footer");
            }
        
    }

me i am saving the checkbox vs user id.
Code:
function insertuseritemOptions()
    {


foreach ($this->input->post('options') as $optn)
{
if(strpos($optn, "_unchecked")) {

$checked = 0;
}else {
$checked = 1;
}



$user_countries = array(
'userID' => $this->input->post('id'),
'item_option_id' => $optn,
'checked' => $checked
);


  $this->db->insert('user_item_options', $user_countries);
      
    }
    }

table user

id | username | pass ...

table user_item_options
id | userID|item_option_id

userID is the user
item_option is theid of the item

itemoption
id|itemoption|item_id
here its like which item suport which options

ie like item_id 1 has mouse [itemoption]
item_id 1 has keyboard
item_id 2 has ups ect



table item
id|itemName

this is just list of item
for example

computer1
computer2
#3

[eluser]rajneeshgobin[/eluser]
Code:
$( document ).ready(function() {



   $('#useradd').submit(function(e)
     {
  
     &lt;?php foreach ($query_platform as $row): { ?&gt;

&lt;?php foreach ($query_option as $row_options): { if($row_options['platform_id']==$row['id']) {?&gt;

if(document.getElementById("&lt;?=$row_options['platformoption']?&gt;testName").checked){
  document.getElementById('&lt;?=$row_options['platformoption']?&gt;testNameHidden').disabled = true;
}
&lt;?php }} ?&gt;
&lt;?php endforeach; ?&gt;
&lt;?php } ?&gt;
&lt;?php endforeach; ?&gt;
           // e.preventDefault();

     });



});

i forgot to put this jquery




Theme © iAndrew 2016 - Forum software by © MyBB