我做了一个编辑/更新功能,效果很好。我试图允许用户提交编辑,而不必上载或更改数据库中的图像,因为单击“编辑”时不会再次加载原始图像。因此,当您不再加载图像时,它将引发错误。
我已经尝试了一些方法,但是如果有人可以给我一些提示或指点,那么这个问题仍然存在,因为我对共同创始人有点新意。
控制器:
public function edit($slug){
if($this->session->userdata('customer_id') !=$this->post_model->get_posts($slug)['customer_id']){
redirect('users/login');
}
if(!$this->session->userdata('logged_in')){
$this->session->set_flashdata('log_post','Please login or create a free account to post a ad.');
redirect('users/login');
}
////Checks the user
$data['categories'] = $this->post_model->get_categories();
$data['post_image'] = $this->post_model->get_posts('post_image');
$data['post'] = $this->post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = 'Edit Your Post';
$this->load->view('templates/header');
$this->load->view('posts/edit', $data);
$this->load->view('templates/footer');
}
public function update(){
if(!$this->session->userdata('logged_in')){
$this->session->set_flashdata('log_post','Please login');
redirect('users/login');
}
else {
if($_FILES['userfile']['name'] != "")
{
if($_FILES['userfile']['type'] == 'image/jpeg'){
$ext = ".jpeg";
}
if($_FILES['userfile']['type'] == 'image/jpg'){
$ext = ".jpg";
}
else if($_FILES['userfile']['type'] == 'image/png'){
$ext = ".png";
}
}
$imageedit = time();
$config = array(
'file_name' => $imageedit,
'upload_path' => "assets/images/posts/",
'allowed_types' => "jpg|png|jpeg",
);
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile')){
$this->session->set_flashdata( 'error_msg', $this->upload->display_errors());
redirect('posts/edit');
}
else {
$this->post_model->update_post($imageedit,$slug);
}
elseif {
$orginal = $this->post_model->orignal_image();
}
$this->post_model->update_post($imageedit,$slug,$orginal);
$this->session->set_flashdata('post_updated', 'Your post has been updated');
redirect('posts');
}
}
}//ending bracket
型号:
Public function update_post($imageedit,$slug,$orignal){
$site = $this->input->post('site');
if (!preg_match("~^(?:f|ht)tps?://~i", $site)) {
$site = "http://" . $site;
}
$data = array(
'title'=>$this->input->post('title'),
'body'=> $this->input->post('Description'),
'category_id' => $this->input->post('category'),
'post_image' => $imageedit || $orginal,
'customer_id' =>$this->session->userdata('customer_id'),
'site'=>$site
);
$this->db->where('id',$this->input->post('id'));
return $this->db->update('posts',$data);
}
public function user_posts() {
$this->db->order_by('created_time', 'DESC');
$query = $this->db->get_where('posts', array('customer_id' => $this->session->userdata('customer_id')));
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
public function orginal_image($slug=NULL,$orginal){
$this->db->select('post_image');
$this->db->from('posts');
$query = $this->db->get_where('posts',array('slug'=>$slug));
return $query->row_array();
}
}
查看代码:
<input type ="hidden" id="orginal" name="orginal" value = <?php echo $orginal; ?>