我想将所有复选框值存储到具有相关menuid的数据库,但我的数组不存储根据menuid的数据。我附上了我正在使用的所有代码,如果有任何机构可以帮助我,我会提前感谢。
查看档案
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading bg-white">
<strong>Users Groups</strong><br>
<small class="text-muted">Create User Groups</small>
</div>
<div class="panel-body">
<div class="col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">User Group Title*</label>
<input value="" type="text" name="title" class="form-control" id="shop_name" placeholder="Ex: xyz shop">
</div>
</div>
<div class="col-md-12">
<div class="table-responsive">
<table id="example" class="table table-hover b-t b-b">
<thead>
<tr>
<th>Item</th>
<th>Add</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$n = 1;
foreach ( $list_users as $row) { ?>
<tr>
<?php if ($row['IsChild'] != null and $row['IsChild'] != 1) { ?>
<td><?php echo $row['alias']; ?></td>
<td>
<input value="<?php echo $row['id']; ?>" type="text" name="menu_id[]">
<input checked style="opacity:0;" value="<?php echo $row['id']; ?>--0--a" id="add1<?php echo $n;?>" type="checkbox" name="add[]">
<input value="<?php echo $row['id']; ?>--1--a" id="add<?php echo $n;?>" type="checkbox" name="add[]">
</td>
<td>
<input checked style="opacity:0;" value="<?php echo $row['id']; ?>--0--e" id="edit1<?php echo $n;?>" type="checkbox" name="edit[]">
<input value="<?php echo $row['id']; ?>--1--e" id="edit<?php echo $n;?>" type="checkbox" name="edit[]">
</td>
<td>
<input checked style="opacity:0;" value="<?php echo $row['id']; ?>--0--d" id="delete1<?php echo $n;?>" type="checkbox" name="delete[]">
<input value="<?php echo $row['id']; ?>--1--d" id="delete<?php echo $n;?>" type="checkbox" name="delete[]">
</td>
<?php } ?>
</tr>
<?php $n++; } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading bg-dark">
<strong>Publish</strong><br>
<small class="text-muted">Click to publish</small>
</div>
<div class="panel-body">
<p><strong>Status: </strong> </p>
<p><strong>Date Created: </strong></p>
<p><strong>Date Updated: </strong></p>
</div>
<div class="panel bg-light" style="margin-bottom: 0px; padding: 10px; text-align: right;" >
<input value="Update Now" style="padding: 7px 20px; border: 0px; border-radius: 10px;" type="submit" class=" btn-success">
</div>
</div>
</div>
控制器功能
function save_user_groups()
{
//For generating user_group_master_id manually
$this->load->model('settings/model_users');
$banner_id_main = $this->model_users->get_user_master_id();
foreach ($banner_id_main as $row)
{
$user_group_m_id = $row['user_group_master_id'];
}
$title = $this->input->post('title');
$menu_id = $this->input->post('menu_id');
$add = $this->input->post('add');
$edit = $this->input->post('edit');
$delete = $this->input->post('delete');
$count = count($menu_id);
//array for insert main banner detail in database
$user_group_master = array(
'user_group_master_id' => $user_group_m_id,
'user_group_title' => $title
);
$g_rights=array();
for($i=0; $i < $count; $i++)
{
$g_rights[] = array(
'user_group_master_id' => $user_group_m_id,
'MenuID' => $menu_id[$i],
'chkadd'=>$add[$i],
'chkedit'=>$edit[$i],
'chkdel' => $delete[$i]
);
}
print_r($user_group_master);
print_r($g_rights);
exit();
$data['rights'] = $g_rights ;
$this->load->model('settings/model_users');
if ($create = $this->model_users->save_user_group($user_group_master, $g_rights))
{
$data['product_created'] = 'thank you';
$this->load->view('setting/users' , $data);
}
}
数组的结果 菜单ID未在chkadd,chkedit,chkdelete中正确填充,请帮助
Array
(
[user_group_master_id] => 1
[user_group_title] =>
)
Array
(
[0] => Array
(
[user_group_master_id] => 1
[MenuID] => 51
[chkadd] => 51--0--a
[chkedit] => 51--0--e
[chkdel] => 51--0--d
)
[1] => Array
(
[user_group_master_id] => 1
[MenuID] => 10
[chkadd] => 51--1--a
[chkedit] => 51--1--e
[chkdel] => 10--0--d
)
[2] => Array
(
[user_group_master_id] => 1
[MenuID] => 11
[chkadd] => 10--0--a
[chkedit] => 10--0--e
[chkdel] => 11--0--d
)
[3] => Array
(
[user_group_master_id] => 1
[MenuID] => 12
[chkadd] => 11--0--a
[chkedit] => 10--1--e
[chkdel] => 12--0--d
)
[4] => Array
(
[user_group_master_id] => 1
[MenuID] => 14
[chkadd] => 12--0--a
[chkedit] => 11--0--e
[chkdel] => 14--0--d
)
[5] => Array
(
[user_group_master_id] => 1
[MenuID] => 15
[chkadd] => 14--0--a
[chkedit] => 11--1--e
[chkdel] => 15--0--d
)
[6] => Array
(
[user_group_master_id] => 1
[MenuID] => 16
[chkadd] => 15--0--a
[chkedit] => 12--0--e
[chkdel] => 16--0--d
)
[7] => Array
(
[user_group_master_id] => 1
[MenuID] => 17
[chkadd] => 16--0--a
[chkedit] => 14--0--e
[chkdel] => 17--0--d
)
[8] => Array
(
[user_group_master_id] => 1
[MenuID] => 18
[chkadd] => 17--0--a
[chkedit] => 15--0--e
[chkdel] => 18--0--d
)
[9] => Array
(
[user_group_master_id] => 1
[MenuID] => 19
[chkadd] => 18--0--a
[chkedit] => 16--0--e
[chkdel] => 19--0--d
)
[10] => Array
(
[user_group_master_id] => 1
[MenuID] => 20
[chkadd] => 19--0--a
[chkedit] => 17--0--e
[chkdel] => 20--0--d
)
[11] => Array
(
[user_group_master_id] => 1
[MenuID] => 21
[chkadd] => 20--0--a
[chkedit] => 18--0--e
[chkdel] => 21--0--d
)
[12] => Array
(
[user_group_master_id] => 1
[MenuID] => 22
[chkadd] => 21--0--a
[chkedit] => 19--0--e
[chkdel] => 22--0--d
)
[13] => Array
(
[user_group_master_id] => 1
[MenuID] => 29
[chkadd] => 22--0--a
[chkedit] => 20--0--e
[chkdel] => 29--0--d
)
[14] => Array
(
[user_group_master_id] => 1
[MenuID] => 30
[chkadd] => 29--0--a
[chkedit] => 21--0--e
[chkdel] => 30--0--d
)
[15] => Array
(
[user_group_master_id] => 1
[MenuID] => 31
[chkadd] => 30--0--a
[chkedit] => 22--0--e
[chkdel] => 31--0--d
)
[16] => Array
(
[user_group_master_id] => 1
[MenuID] => 32
[chkadd] => 31--0--a
[chkedit] => 29--0--e
[chkdel] => 32--0--d
)
[17] => Array
(
[user_group_master_id] => 1
[MenuID] => 33
[chkadd] => 32--0--a
[chkedit] => 30--0--e
[chkdel] => 33--0--d
)
[18] => Array
(
[user_group_master_id] => 1
[MenuID] => 35
[chkadd] => 33--0--a
[chkedit] => 31--0--e
[chkdel] => 35--0--d
)
[19] => Array
(
[user_group_master_id] => 1
[MenuID] => 36
[chkadd] => 35--0--a
[chkedit] => 32--0--e
[chkdel] => 36--0--d
)
[20] => Array
(
[user_group_master_id] => 1
[MenuID] => 37
[chkadd] => 36--0--a
[chkedit] => 33--0--e
[chkdel] => 37--0--d
)
[21] => Array
(
[user_group_master_id] => 1
[MenuID] => 38
[chkadd] => 37--0--a
[chkedit] => 35--0--e
[chkdel] => 38--0--d
)
[22] => Array
(
[user_group_master_id] => 1
[MenuID] => 39
[chkadd] => 38--0--a
[chkedit] => 36--0--e
[chkdel] => 39--0--d
)
[23] => Array
(
[user_group_master_id] => 1
[MenuID] => 40
[chkadd] => 39--0--a
[chkedit] => 37--0--e
[chkdel] => 40--0--d
)
[24] => Array
(
[user_group_master_id] => 1
[MenuID] => 41
[chkadd] => 40--0--a
[chkedit] => 38--0--e
[chkdel] => 41--0--d
)
[25] => Array
(
[user_group_master_id] => 1
[MenuID] => 43
[chkadd] => 41--0--a
[chkedit] => 39--0--e
[chkdel] => 43--0--d
)
[26] => Array
(
[user_group_master_id] => 1
[MenuID] => 44
[chkadd] => 43--0--a
[chkedit] => 40--0--e
[chkdel] => 44--0--d
)
[27] => Array
(
[user_group_master_id] => 1
[MenuID] => 45
[chkadd] => 44--0--a
[chkedit] => 41--0--e
[chkdel] => 45--0--d
)
[28] => Array
(
[user_group_master_id] => 1
[MenuID] => 46
[chkadd] => 45--0--a
[chkedit] => 43--0--e
[chkdel] => 46--0--d
)
[29] => Array
(
[user_group_master_id] => 1
[MenuID] => 47
[chkadd] => 46--0--a
[chkedit] => 44--0--e
[chkdel] => 47--0--d
)
[30] => Array
(
[user_group_master_id] => 1
[MenuID] => 48
[chkadd] => 47--0--a
[chkedit] => 45--0--e
[chkdel] => 48--0--d
)
[31] => Array
(
[user_group_master_id] => 1
[MenuID] => 49
[chkadd] => 48--0--a
[chkedit] => 46--0--e
[chkdel] => 49--0--d
)
[32] => Array
(
[user_group_master_id] => 1
[MenuID] => 50
[chkadd] => 49--0--a
[chkedit] => 47--0--e
[chkdel] => 50--0--d
)
)
答案 0 :(得分:1)
我认为您的问题是,在发布表单数据时,未检查的复选框不会显示在post数组中。知道如果没有选中某些复选框,您不能简单地尝试使用添加,编辑和删除数组的数字索引来确定是否已选中/取消选中。考虑以下测试:
<?php
// You have three menu IDs
$menu_id = array(
'1','2','3'
);
// Only one checkbox was checked for "add"
$add = array(
'1--0--a'
);
// Only two checkboxes were checked for "edit"
$edit = array(
'1--0--e',
'2--0--e'
);
// All three checkboxes were checked for "delete"
$delete = array(
'1--0--d',
'2--0--d',
'3--0--d'
);
// Loop through the menu IDs, because they will always be posted
foreach( $menu_id as $id )
{
$temp = array('MenuId' => $id);
// Check if this menu ID has a checkbox checked for add
foreach( $add as $a )
{
$parts = explode( '--', $a );
if( $parts[0] == $id )
{
$temp['chkadd'] = $a;
break;
}
}
// Check if this menu ID has a checkbox checked for edit
foreach( $edit as $e )
{
$parts = explode( '--', $e );
if( $parts[0] == $id )
{
$temp['chkedit'] = $e;
break;
}
}
// Check if this menu ID has a checkbox checked for delete
foreach( $delete as $d )
{
$parts = explode( '--', $d );
if( $parts[0] == $id )
{
$temp['chkdel'] = $d;
break;
}
}
$final[] = $temp;
}
echo '<pre>';
print_r( $final );
echo '</pre>';
此测试应该可以帮助您了解复选框数据如何不再成为问题。现在你可以修复你的代码了。