2013-02-20 65 views
0

我有一个网站在codeigniter开发,我想做一个功能,编辑表,但我有两个页面更新与不同领域的表。 这是我的模型功能:更新所有codeigniter

public function editHotel($service_id) { 
     $hotel_id = $this->getHotelByServiceId($service_id); 
     $hotel = array(
       'rating_id'=>$this->input->post('rating'), 
       'treatment_id'=>$this->input->post('treatment'), 
       'nation_id'=>$this->input->post('nation'), 
       'region_id'=>$this->input->post('region'), 
       'city_id'=>$this->input->post('city'), 
       'area_id'=>$this->input->post('area'), 
       'address'=>$this->input->post('address'), 
       'phone'=>$this->input->post('phone'), 
       'fax'=>$this->input->post('fax'), 
       'email'=>$this->input->post('email'), 
       'site'=>$this->input->post('site'), 
       'description_it'=>$this->input->post('description_it'), 
       'description_en'=>$this->input->post('description_en'), 
       'latitudine_google'=>$this->input->post('latitudine_google'), 
       'longitudine_google'=>$this->input->post('longitudine_google'), 
       'modified'=> date('Y-m-d H:i:s'), 
     ); 
     $this->db->where('service_id', $service_id); 
     $this->db->update('hotel',$hotel); 
    } 

在这个函数中我有upadtae我所有的表,但我有一个页面,只有一些价值,我只想更新这个是这样的:

public function editDescriptionHotel($service_id) { 
      $hotel_id = $this->getHotelByServiceId($service_id); 
      $hotel = array(
        'description_it'=>$this->input->post('description_it'), 
        'description_en'=>$this->input->post('description_en'), 
        'modified'=> date('Y-m-d H:i:s'), 
      ); 
      $this->db->where('service_id', $service_id); 
      $this->db->update('hotel',$hotel); 
     } 

而我有其他页面更新表的某些值。 如果我在所有页面中使用“editHotel”功能,如果我没有输入codeigniter,则将0插入表 的字段可能仅使用第一个功能“editHotel”并仅更新我发布的字段? 我想在所有页面中只使用一个函数,它不是更新所有字段,而是只更新页面中的字段。有可能吗? 我不想每个页面都更新表格来编写表格的不同功能更新

回答

2

那么,如果你让它的输入字段与你的表格列名称相同,那很简单:

public function updateHotels($service_id) 
{ 
    $hotel_id = $this->getHotelByServiceId($service_id); 
    $fields = array(); 

    foreach($this->input->post() as $k => $v){ 
    if($k != 'submit'){ //or anything you always want to esclude 
     $fields[$k] = $v; 
    } 
    } 

    $fields['modified'] = date('Y-m-d H:i:s'); 

    $this->db->where('service_id', $service_id); 
    $this->db->update('hotel', $fields); 
} 


为了更好的控制和更好的可重用性,可以传递作为参数escludable指标阵列(那些谁也不会和你不想来匹配列名)。 你可以更进一步,使额外领域你想添加的列表:

$exclude = array('submit', 'other_field'); 
$extra = array('modified' => date('Y-m-d H:i:s')); 

public function updateHotels($service_id, $exclude, $extra) 
    { 
     $hotel_id = $this->getHotelByServiceId($service_id); 
     $fields = array(); 

     foreach($this->input->post() as $k => $v){ 
     if(!in_array($k, $exclude){ 
      $fields[$k] = $v; 
     } 
     } 

     $columns = array_merge($fields, $extra); 

     $this->db->where('service_id', $service_id); 
     $this->db->update('hotel', $columns); 
    } 
+0

这将抛出一个错误,因为'submit'按钮将是'POST'阵列和'更新可用函数也会尝试更新'submit'字段。 – 2013-02-20 21:27:08

+0

你是对的,没有打消我的想法。我更新了我的答案 – 2013-02-20 21:28:52

+0

有趣的我会尝试这个解决方案!我正在使用与表格名称相同的字段! – 2013-02-20 21:31:30