模型不插入选择选项

时间:2015-03-19 02:08:08

标签: codeigniter

我的模型没有插入我在视图中选择的修改和访问数据。如果我的视图上的选择选项是选择选项1,即使选择了1,它也会插入值0。不确定为什么在模型上或查看它没有插入正确的?

每个控制器都有自己的修改和访问选择选项我希望能够插入,如果选择1表示启用或0分配?

如何使两个选择选项与我的模型一起使用,以便更正所选内容。

模型功能

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

    }

}

查看

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

1 个答案:

答案 0 :(得分:1)

您的视图文件似乎具有每个控制器的访问和修改选项。但是你的输入名称是相同的。所以$ access和$ modify的值是最后一个输入值。您可以通过这种方式解决。访问并修改输入名称为数组

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

现在你的模特

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

}

希望您理解并解决您的问题