2015-10-13 95 views
1

我有一個表格,每行有3列:名稱,啓用,禁用。啓用和禁用列有一個複選框和名稱狀態,我試圖做每行只有一個打勾的複選框。這些是我的代碼(每個行和兩列)爲止:每個行只檢查複選框

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

EDIT 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

只能使用單選按鈕並在每行中更改其名稱。可能會使用一些計數器或數據庫ID。 –

+0

@NiranjanNRaju我把名字改成了什麼?我使用了兩個複選框,因爲如果我使用了一個複選框,我也想獲得不確認的值,如果可能的話。 – hzq

+0

第一行的單選按鈕名稱=> radio_1,第二行,radio_2 ...等 –

回答

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_idcustomer。每次用戶想要啓用或禁用他/她的客戶時,我都會添加和刪除。這就是爲什麼我想要兩個數組。

控制器:

// 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 ***/ 
相關問題