我有一个表,每行有3列:name,enable,disable。启用和禁用列有一个复选框,正如名称所示,我试图每行只勾选一个复选框。到目前为止,这些是我的代码(对于每一行和两列):
<?php foreach($customers as $customer):?>
<tr>
<td align="center"><?php echo $customer;?></td>
<?php
$check = FALSE;
if($user_customer != FALSE)
foreach($user_customer as $object)
foreach($object as $current_customer)
if($current_customer == $customer)
$check = TRUE;
?>
<td align='center'>
<?php
if($check == FALSE)
$data = array('name' => 'enable[]', 'value' => $customer, 'checked' => FALSE);
else
$data = array('name' => 'enable[]', 'value' => $customer, 'checked' => TRUE);
echo form_checkbox($data);
?>
</td>
<td align='center'>
<?php
if($check == TRUE)
$data = array('name' => 'disable[]', 'value' => $customer, 'checked' => FALSE);
else
$data = array('name' => 'disable[]', 'value' => $customer, 'checked' => TRUE);
echo form_checkbox($data);
?>
</td>
</tr>
<?php endforeach?>
在我提交表单后,它会相应地将值存储到数据库中,但我不希望用户能够一次勾选两个复选框。我尝试使用单选按钮,但每列只检查一次。我试图按行,任何帮助吗?
P.S。我不想使用任何Javascript。
编辑,我为每一行添加了一个计数器,因此对于第一行等它将是radio_1
,这是我编辑的部分:
<td align='center'>
<?php
if($check == FALSE)
$data = array('name' => "radio_$counter", 'value' => $customer, 'checked' => FALSE);
else
$data = array('name' => "radio_$counter", 'value' => $customer, 'checked' => TRUE);
echo form_radio($data);
?>
</td>
<td align='center'>
<?php
if($check == TRUE)
$data = array('name' => "radio_$counter", 'value' => $customer, 'checked' => FALSE);
else
$data = array('name' => "radio_$counter", 'value' => $customer, 'checked' => TRUE);
echo form_radio($data);
?>
</td>
编辑2,这就是我在控制器功能中检索它的方式:
// These were the arrays that I assigned to each checkbox according to the column
$enable = $this->input->post('enable');
$disable = $this->input->post('disable');
// I used it for inserting and deleting values
$insert = $this->home_model->insert_customer(array('user_id'=>$user_id, 'customer'=>$enable));
$delete = $this->home_model->delete_customer($user_id, $disable);
答案 0 :(得分:0)
最终我设法在不使用javascript的情况下解决了我的问题。
首先,在我看来,我在td中使用了一个隐藏输入和一个复选框输入。如下所示,有两种不同的数组。隐藏的输入数组名为disable[]
,复选框数组将包含提交表单时所有选中的复选框值,名为enable[]
。
隐藏的输入数组disable[]
将始终提交值,无论如何。现在我们在disable[]
数组中拥有所有客户名称。
查看:
<td align='center'>
<input type="hidden" value="<?php echo $customer?>" name='disable[]'>
<?php
if($check == FALSE)
{
$data = array('name' => 'enable[]', 'value' => $customer, 'checked' => FALSE);
}
else
$data = array('name' => 'enable[]', 'value' => $customer, 'checked' => TRUE);
echo form_checkbox($data);
?>
</td>
现在我操作数组以获取我想要的值。就我而言,我的数据库表只有两列user_id
和customer
。每次用户想要启用或禁用他/她的客户时,我都会添加和删除。这就是我想要获得两个数组的原因。
控制器:
// Array containing selected/checked customers
$enable = $this->input->post('enable');
// Array containing all customers
$disable = $this->input->post('disable');
// Loop through selected customers
foreach($enable as $index => $value)
{
// Check if customer exists in database
if( $this->home_model->check_customer($user_id, $enable[$index]) )
{
// If exists, unset from enable to prevent duplicate insert
unset($enable[$index]);
}
/* Unset the remaining
(or all selected customers if it doesn't exists in DB)
selected customers from disable array */
$key = array_search($value, $disable);
unset($disable[$key]);
}
现在在同一个控制器中,只需进行3次简单检查,看看是否确保执行哪个查询,如果有客户要插入和删除,是否只有客户要插入,或者是否只有客户要删除
// Check enable and disable array are not empty
if( ! empty($enable) && ! empty($disable) )
{
$insert = $this->home_model->insert_customer(array('user_id'=>$user_id, 'customer'=>$enable));
$delete = $this->home_model->delete_customer($user_id, $disable);
}
// If enable array is not empty but disable is empty, insert only
elseif( ! empty($enable) && empty($disable) )
{
$insert = $this->home_model->insert_customer(array('user_id'=>$user_id, 'customer'=>$enable));
}
// If enable is empty but disable is not empty, delete only
elseif( empty($enable) && ! empty($disable) )
{
$delete = $this->home_model->delete_customer($user_id, $disable);
}
/*** Proceed with whatever you want to do like go to new page etc ***/