我想让我的代码变干并且我是codeigniter的新手我使用$ this-> db-> set($ data)所以我不需要复制许多这样的设置我怎么防止$ this-> db->设置($ data)跳过in_image,如果它是空的,可以在更新时将其设置为db吗?
我的控制器:
$data_arr=array(
'title'=>$this->input->post('txttitle'),
'desc'=>$this->input->post('txtdesc'),
'addr'=>$this->input->post('txtaddr'),
'fbn'=>$this->input->post('txtfbname'),
'fburl'=>$this->input->post('txturl'),
'status'=>$this->input->post('status')
);
if(!empty($_FILES['File']['tmp_name']))
{
$new_file_name=date("mdY")."_".time();
$config['upload_path'] = './assets/images/';
$config['allowed_types']= 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 640;
$config['max_height'] = 420;
$config['file_name'] = $new_file_name;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('File'))
{
$error = array('error' => $this->upload->display_errors());
$image_arr = array('in_image'=>'error');
}
else
{
$image_arr = array('in_image'=>$this->upload->data('orig_name'));
}
}
else
{
$image_arr = array('in_image'=>'');
}
$data_merge = array_merge($data_arr,$image_arr);
$this->Description_model->update_all($data_merge,$id);
我的模特
public function update_all($data, $id)
{
if($data['in_image'] != 'error' && empty($data['in_image'])
{
/*
this is where i want to check if in_image is empty than prevent
$this->db->set($data) skip to set in_image
*/
$this->db->set($data)
->where('in_id',$id);
if($this->db->update('tbl_desc'))
{return TRUE;}
else
{return FALSE;}
}
elseif($data['in_image'] != 'error' && $data['in_image'] != ''))
{
}
else
{return FALSE;}
}
提前感谢
答案 0 :(得分:0)
在codeigniter中,数据可以主要以2路方式更新,
$data = array{
'YOUR_COL_1' => 'YOUR_VALUR_1',
'YOUR_COL_2' => 'YOUR_VALUR_2',
};
$this->db->where('id', $id);
$this->db->update('YOUR_TABLE', $data);
OR
$this->db->set('YOUR_COL_1', 'YOUR_VALUR_1');
$this->db->set('YOUR_COL_2', 'YOUR_VALUR_2');//if 2 columns
$this->db->where('id', $id);
$this->db->update('YOUR_TABLE');
在if条件以及!empty
中还需要另一个结束括号if($data['in_image'] != 'error' && !empty($data['in_image']))
答案 1 :(得分:0)
您的帖子令人困惑,但似乎您希望在in_image
为空时阻止更新,是吗?
$data = array
(
'title' => $this->input->post('txttitle'),
'desc' => $this->input->post('txtdesc'),
'addr' => $this->input->post('txtaddr'),
'fbn' => $this->input->post('txtfbname'),
'fburl' => $this->input->post('txturl'),
'status' => $this->input->post('status')
);
if(!empty($_FILES['File']['tmp_name']))
{
$new_file_name = date("mdY")."_".time();
$config['upload_path'] = './assets/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 640;
$config['max_height'] = 420;
$config['file_name'] = $new_file_name;
$this->load->library('upload', $config);
if($this->upload->do_upload('File'))
$data['in_image'] = $this->upload->data('orig_name');
}
/*
* Checks if the `in_image` index exists in the array.
* Only if the index exists is when it allows the update to happen.
*/
if(isset($data['in_image']) == TRUE)
$this->Description_model->update_all($data, $id);
现在,在您的模型中,您只需要:
public function update_all($data, $id)
{
$this->db->where('id', $id)
->update('table_name', $data);
}