2012-10-11 19 views
1

我是php/codeignitor的新手,嘗試將表單複選框上的更改發佈到我的數據庫。要修改的行由下面的gl_id分配。 我不知道如何使用我的模型中的update_checked函數 後TRUE到這些字段中的每個gl_id = x和FALSE使用其餘字段不在數組中。我可以在控制器 中使用$ _POST ['checked']創建並查看選中值的數組,並將其傳遞給模型。現在我需要使用這個數組發佈到db,其中gl_id =視圖中表單的值。codeigniter更新數據庫的檢查項目爲「TRUE」,剩餘的未檢查項目爲「FALSE」

在View

<h1>Update GL id <?php echo $gl_field['gl_id'];?></h1> 
     <label>Stat</label> 
      <input type="checkbox" <?php if ($gl_field['stat'] == "TRUE") {echo "checked = checked";} ?> 
      name = "checked[]" id="stat" value= "stat" /><BR CLEAR="all"> 
     <label>Exclude</label> 
      <input type="checkbox" <?php if ($gl_field['exclude'] == "TRUE") {echo "checked = checked";} ?> 
      name="checked[]" id="exclude" value= "exclude"><BR CLEAR="all"> 
     <label>Override</label> 
      <input type="checkbox" <?php if ($gl_field['override'] == "TRUE") {echo "checked = checked";} ?>   
      name= "checked[]" id= "override" value = "override"/><BR CLEAR="all"> 

在控制器

  public function update_table(){ 
       $this->load->helper('form'); 
       $this->page_nav_model->update_table(); 
       $checked_array = $_POST['checked']; 
       $this->page_nav_model->update_checked($checked_array); 
       $this->load->view('pages/success'); 
      } 

在模型

foreach($checked_array as $true){ 
      echo $true;        
     } 

爲了澄清當我拉信息到表單在上面的例子中的所有複選框的是在數據庫中設置爲「TRUE」,以便檢查它們。如果我取消選中統計並點擊提交,我可以看到一個包含數值的數組(排除,覆蓋)。

我感謝任何幫助,使我朝着正確的方向前進。

回答

0

要一次更新與CI的表格,你需要一個這樣的數組:

$data = array(
     'column_1_name' => 'value_to_insert' 
     'column_2_name' => 'value_to_insert' 
     'column_2_name' => 'value_to_insert' 
    ); 

現在,你有這樣的$ checked_array =(「統計」,「排除」的數組,「重寫') - 擁有更多或更少的成員。

要創建可在更新語句中使用的數組(如上面的$ data),您需要做的是創建另一個數組,其中包含每列的名稱和值等於1的數組(如果它在$ checked_array中如果它不在$ checked數組中,則爲0。

下面的函數add_values()可以爲你做到這一點。你必須提供兩個參數:

  1. $ all_possibilities是,你將要更新爲1或0。在這種情況下,每個列名的數組,它應該看起來像$ all_possibilities =陣列(「統計」 ,'排除','重寫');
  2. $ selected:這是一個包含實際選中框的名稱的數組 - 它是$ all_possibilities的子集(或者如果所有框都被選中,則是相同的)。

此功能在您的控制器中。

function add_values($all_possibilities, $selected) { 

     $array_for_query = array(); 
     // this goes through each possible box that can be checked to see if that possibility was actually selected 
     foreach($all_possibilities as $array_member) { 
      // this checks to see if the current member of $all_possibilities is found in the $selected array 
      if (in_array($array_member, $selected)) { 
        // if it is, it adds that as a member of a new query $array_for_query and gives it a value of 1 (true) 
        $array_for_query[$array_member] = 1; 
       } else { 
        // if it is not, same as above, but gives it a value of 0 
        $array_for_query[$array_member] = 0;   
       } 
     } 

     // once all the possibilities have been checked and added to the query array, that array is returned 
     return $array_for_query; 
    } 

現在你可以調用的函數,並通過這兩個論點,返回的數組分配到$ checked_and_unchecked。

$data['checked_and_unchecked'] = add_values($possibilities, $checked_array); 

確保您還從窗體中獲取gl_id。你可以做到這一點的隱藏字段在您的視圖:

<input type="hidden" name="gl_id" value="<?php echo $gl_field['gl_id'];?>" /> 

在控制器中,抓住與

$data['gl_id'] = $this->input->post('gl_id'); 

該值然後您可以將信息傳遞給你的模型和更新表格傳入表的名稱和您創建的數組。

$this->model_name->model_method($data); 

那麼這將大大模型:

$this->db->update('table_name', $checked_and_unchecked); 
    $this->db->where('gl_id', $gl_id); 
+0

感謝Tayler的解釋和功能。它工作得很好! – vizyourdata

0

如果未在HTML中選中某個複選框,則該值不會通過$ _POST發送。這在CodeIgniter中沒有說明。這是我處理這個問題的方法。在控制器:

/** 
* Set post values for unchecked boxes. 
* 
* @access private 
* @return void 
*/ 
private function _section_checkboxes() 
{ 
    if (!$this->input->post('is_active')) {$_POST['is_active'] = 0;} 
    // other checkboxes here... 
} 

/** 
* if form validation passes, run this to edit a section 
* 
* @access public 
* @return void 
*/ 
public function do_edit_section() 
{ 
    $this->_section_checkboxes(); 
    if ($this->section_model->edit_section($this->input->post())) 
    { 
     redirect('success_page'); 
    } 
} 

和模型:

/** 
* updates a section with active record, returns success. 
* 
* @access public 
* @param array $data 
* @return bool 
*/ 
public function edit_section(array $data) 
{ 
    $this->db->where('id', $data['id']); 
    return $this->db->update('sections', $data); 
} 

並在視圖,使用表單助手:

<?=form_checkbox(array('name' => 'is_active', 'id' => 'is_active_field', 'value' => '1', 'checked' => (bool)$item->is_active))?> 

現在你選中的複選框被設置爲0時未選中,選中時爲1。我希望這回答了你的問題。

+0

感謝您的建議邁克! – vizyourdata

+0

不客氣! [這是一個幫手](https://gist.github.com/4004986),使其更容易 –

0

所以我仍然需要幫助獲得對整個項目進行最後的潤色,所以我想在這裏發表最後的結果讓任何綠色的我希望從複選框中控制數據庫和數據庫的複選框。

控制器:

[function from Tayler and] 
    $all_checkbox_fields = array('stat','exclude','override'); 
     $checked_array = $_POST['checkbox']; 
     $data['checkbox'] = $this->add_values($all_checkbox_fields, $checked_array); 
     $data['gl_id'] = $this->input->post('gl_id'); 
     $this->page_nav_model->update_checked($data); 
     $this->load->view('pages/success'); 

型號:

public function update_checked($data){ 
     $this->default_db->where('gl_id', $data['gl_id']); 
     $this->default_db->update('oracle_table', $data['checkbox']); 
    } 

查看:

<label>Stat</label> 
     <input type="checkbox" <?php if ($gl_field['stat'] == "1") {echo "checked = checked";} ?> 
     name = "checkbox[]" id="stat" value= "stat"/><BR> 
    <label>Exclude</label> 
     <input type="checkbox" <?php if ($gl_field['exclude'] == "1") {echo "checked = checked";} ?> 
     name="checkbox[]" id="exclude" value= "exclude"/><BR> 
    <label>Override</label> 
     <input type="checkbox" <?php if ($gl_field['override'] == "1") {echo "checked = checked";} ?>   
     name= "checkbox[]" id= "override" value = "override" ><BR>