2013-03-02 177 views
1

我試圖用Codeigniter更新MySQL表。Codeigniter - 從表單複選框更新表

我的模型代碼是:

function update_customer_records($updatedrow) 
{ 
    $this->db->where('id',$this->input->post('id')); 
    $this->db->update('customers',$updatedrow); 
} 

我的看法是:

$attributes=array(
     'name'=>'updatecustomer', 
     'id'=>'updatecustomer', 
     ); 
    echo form_open('masterdata/manage_customers',$attributes); 
?> 

<table> 
    <tr> 
     <td>&nbsp;</td><td>&nbsp;</td><td>Customer Name</td><td>postalcode</td> 
    <tr> 

<?php if(isset($records)) : foreach ($records as $row) : ?> 
    <tr> 
     <td> 
<?php echo anchor('masterdata/customers_updated/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?> 
     </td> 
     <td> 
      <input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>"> 
     </td> 
     <td> 
      <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" > 
     </td> 
     <td> 
      <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" > 
     </td> 
    </tr> 
<?php endforeach ; ?> 
    </table> 
<input type="submit" value="Update Selected"> 
<?php else : ?> 
<h2> No Records Found</h2> 
<?php endif; ?> 
<?php echo form_close(); ?> 

我的控制器:

function manage_customers() 
    { 

     $data['title']="Manage Customers"; 

      //query model to get data results for form 
      $data=array(); 

      if($query=$this->model_master_data->get_records()){ 
       $data['records']=$query; 


      $this->load->view("master_data/view_master_data_header",$data); 
      $this->load->view("master_data/view_master_data_nav"); 
      $this->load->view("master_data/view_content_master_data_manage_customers",$data); 
      $this->load->view("master_data/view_master_data_footer"); 


      $editcustomer = $this->input->post('editcustomer'); 

      if(isset($editcustomer)){ 

       //begin outputting id of selected checkbox 
       foreach ($editcustomer as $row) : 
        echo $row; 

       $updatedrow=array(
        'id'=>$row, 
        'postalcode'=>'44444' 
        ); 


       $this->model_master_data->update_customer_records($updatedrow); 

       endforeach; 
      } 

我有兩個問題:

  1. 如果未檢查複選框,如何停止運行的foreach。
  2. 如何正確地將數組傳遞到模型,以便更新運行?

在此先感謝一如既往。

回答

2

首先我發現在具有相同的名稱和ID(如下)形式的兩個領域,一個領域要設置它的價值customer_name和另一個你正在設置postalcode

<td> 
    <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" > 
           --^^-- 
</td> 
<td> 
    <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" > 
           --^^-- 
</td> 

所以我認爲(可能)的名稱和第二場的編號應根據它的值是postalcode

此外,您不必擔心foreach循環,因爲循環中的代碼只有在檢查窗體上的複選框時纔會運行,因爲不會提交未選中的複選框,但您可以檢查並運行循環使用下面的代碼

if($this->input->post('editcustomer') != false) 
{ 
    foreach ($editcustomer as $row) 
    { 
     // code here 
    } 
} 

如果editcustomer找不到或不與表單提交的if條件將返回false。還沒有id領域的形式,在這種情況下,你不能使用$this->input->post('id'),所以如果你需要檢查你的模型where clause複選框ID,那麼你可以使用

在控制器:

if($this->input->post('editcustomer') != false) 
{ 
    $this->load->model('model_master_data'); 
    foreach ($editcustomer as $row_id) 
    { 
     $data = array('postalcode' => '44444'); 
     $this->model_master_data->update_customer_records($row_id, $data); 
    } 
} 

我不認爲你需要通過'id'=>$row,,因爲你可能不wan't更新這一領域。在更新記錄之前,您還應該使用form validation來檢查表單輸入(您可以設置綁定用戶輸入郵政編碼所需的郵編字段)。

在模型:

function update_customer_records($id, $data) 
{ 
    $this->db->where('id', $id); 
    $this->db->update('customers', $data); 
} 

所以它會做這樣的事情(僞代碼)

update the customers table set `postalcode` = '44444' where `id` = $id 

更新: 我想你也可以使用update_batch

在控制器:

if($this->input->post('editcustomer') != false) 
{ 
    $data = array(); 
    foreach ($editcustomer as $row_id) 
    { 
     $data[] = array('id' => $row_id, 'postalcode' => '44444'; 
    } 
    $this->load->model('model_master_data'); 
    $this->model_master_data->update_customer_records($data); 
} 

在模型:

function update_customer_records($data) 
{ 
    $this->db->update_batch('customers', $data, 'id'); 
} 
+0

嗨謝克,真的很感謝你的解釋。 – Smudger 2013-03-03 07:14:21

+0

@Smudger,很高興知道:-) – 2013-03-03 07:31:43

1

"Codeigniter update mysql table from form with CI"重複的話題?

如果未檢查複選框,如何停止運行的foreach。

如果未選中複選框,則不必停止運行的foreach。 因爲$editcustomer變量只包含複選框。

如何正確地將數組傳遞到模型,以便更新運行?

您的模型是錯誤的。它應該是:

public function update_customer_records($updatedrow) 
{ 
    $this->db->update('customers', $updatedrow); 
} 

你不需要寫$this->db->where('id',$this->input->post('id'));(它是錯的,因爲你不能在模型中使用$這個 - >輸入 - >後()),因爲你已經在通過id$updaterow

編輯:我很抱歉,我太快讀了你的代碼!

function update_customer_records($updatedrow) 
{ 
    $this->db->where('id',$this->input->post('id')); 
    $this->db->update('customers',$updatedrow); 
} 

...是正確的。或者,您可以在更短的方式把它寫:

function update_customer_records($updatedrow) 
{ 
    $this->db->update('customers', $updatedrow, array('id' => $this->input->post('id'))); 
} 
+0

你能告訴爲什麼'OP'不能使用'$這個 - >輸入 - >在模型中發佈('id')'? – 2013-03-02 23:25:55

+1

這是(技術)可能的,但它不是一個最佳實踐。從控制器訪問$ this-> input-> post()更靈活,並且更符合MVC設計模式(在我看來)。 – Guicara 2013-03-03 00:03:21

+0

我問你是因爲你寫了'你不能用':-) – 2013-03-03 00:07:04