phpcodeigniter

model not inserting select option


My model is not inserting my modify and access data that I select on my view.If my select option on my view is select option 1 it inserts value 0 even though 1 selected. Not sure why on model or view its not inserting the correct one?

Each controller has its own modify and access select option i would like to be able to insert if select 1 for enable or 0 disbale?

How can I make the both select options work with my model so inserts correct what have chosen.

Model Function

<?php

class Model_user_group extends CI_Model {

public function addUserGroup() {

    $name = $this->input->post('name');
    $controllers = $this->input->post('controller');   
    $access = $this->input->post('access');
    $modify = $this->input->post('modify');

    for($i=0;$i<count($controllers);$i++) { 

    $data = array(
        'name' => strtolower($name),
        'controller' => $controllers[$i],
        'access' => $access,
        'modify' => $modify
    );


    $this->db->insert($this->db->dbprefix . 'user_group', $data);

    }

}

View

<?php echo Modules::run('admin/common/header/index');?>

<div id="wrapper">
<?php echo Modules::run('admin/common/menu/index');?>
<div id="page-wrapper" >
<div id="page-inner">

<div class="panel panel-default">
<div class="panel-heading clearfix">
<div class="pull-left" style="padding-top: 7.5px"><h1 class="panel-title"><?php echo $title;?></h1></div>
<div class="pull-right">
<a href="<?php echo base_url('admin/users_group');?>" class="btn btn-primary">Cancel</a>
<button type="submit" onclick="submit()" class="btn btn-primary">Save</button>
</div>
</div>
<div class="panel-body">

<?php echo validation_errors('<div class="alert alert-warning text-center">', '</div>'); ?>

<?php $data = array('class' => 'form-horizontal', 'id' => 'form-users-group');?>
<?php echo form_open_multipart('admin/users_group_add', $data);?>

<div class="form-group">
<?php $data = array('class' => 'col-sm-2');?>
<?php echo form_label('User Group Name', 'name', $data);?>
<div class="col-sm-10">
<?php $data1 = array('id' => 'name', 'name' => 'name', 'class' => 'form-control', 'value' => set_value('name'));?>
<?php echo form_input($data1);?>
</div>
</div>

<table class="table table-striped table-bordered">
<thead>
    <tr>
        <td>Controller Name</td>
        <td>Access</td>
        <td>Modify</td>
    </tr>
</thead>
<?php foreach ($controllers as $controller) {?> 
<tbody>
<tr>
<td>
<?php echo ucfirst(str_replace("_"," ", $controller));?>
<input type="hidden" name="controller[]" value="<?php echo $controller;?>" />
</td>
<td>
<select name="access" class="form-control">
    <option value="0">Disabled</option>
    <option value="1">Enabled</option>
</select>   
</td>
<td>
<select name="modify" class="form-control">
    <option value="0">Disabled</option>
    <option value="1">Enabled</option>
</select>
</td>
</tr>
</tbody>
<?php } ?>
</table>

<?php echo form_close();?>
</div>
</div>

</div><!-- # Page Inner End -->
</div><!-- # Page End -->

</div><!-- # Wrapper End -->
<?php echo Modules::run('admin/common/footer/index');?>

Solution

  • Your view file seems has access and modify option for each controller. But your input name is same.So the value of $access and $modify is the last input value. You can solve this way.Give access and modify input name as array

    <select name="access[]" class="form-control">
    ...
    <select name="modify[]" class="form-control">
    

    Now your model

    $name = $this->input->post('name');
    $controllers = $this->input->post('controller');   
    $accesses = $this->input->post('access');
    $modifies = $this->input->post('modify');
    
    for($i=0;$i<count($controllers);$i++) { 
    
      $data = array(
        'name' => strtolower($name),
        'controller' => $controllers[$i],
        'access' => $accesses[$i],
        'modify' => $modifies[$i]
       );
    
    
       $this->db->insert($this->db->dbprefix . 'user_group', $data);
    
    }
    

    Hope you understand and solves your problem