簡體   English   中英

如何在CodeIgniter中更新圖像

[英]How to update image in CodeIgniter

我是Codeigniter的新手。 我正在為此項目使用codeigniter。 我還沒有如何更新數據庫中的圖像表單數據。 我已經插入,顯示數據庫中的數據已完成。 但我不明白如何更新數據庫中的圖像。 這是我用來更新現有圖像但無法更新圖像的方法。 我已成功將圖像上傳到數據庫中。

public function update($id) {
    $this->form_validation->set_rules('tite', 'title', 'required');
    $this->form_validation->set_rules('slug', 'Slug', 'required');    
    if ($this->form_validation->run() === FALSE) {
        $data['category'] = $this->category_model->all();
        $this->load->view('backend/templates/header');
        $this->load->view('backend/templates/navigation');
        $this->load->view('backend/pages/edit', $data);
        $this->load->view('backend/templates/footer');  
    } else {
        if (!empty($_FILES['userfile']['name'])) {              
            // upload featured image
            $config['upload_path'] = './uploads/page';
            $config['allowed_types']    = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '1080';
            $config['max_height'] = '768';
            $this->load->library('upload', $config);                
            if ( ! $this->upload->do_upload('userfile')) {
                $data['page'] = $this->page_model->find($id);
                $data['category'] = $this->category_model->all();
                $data['error'] = $this->upload->display_errors();
                $this->load->view('backend/templates/header');
                $this->load->view('backend/templates/navigation');
                $this->load->view('backend/pages/edit', $data);
                $this->load->view('backend/templates/footer');
            } else {
                $data = array('upload_data' => $this->upload->data());
                $featured_img = $_FILES['userfile']['name'];
                var_dump($data);
                var_dump($featured_img);
            }
        } else {

            $this->page_model->update($featured_img, $id);
            redirect('backend/page', 'refresh');    
        }    
    }
}

這是我用來更新現有圖像但無法更新圖像的方法。 這是我發送數據的模型。 我已成功將圖像上傳到數據庫中。

public function update($featured_img, $id) {
    $data = array(
        'title' => ucwords($this->input->post('title')),
        'slug' => strtolower($this->input->post('slug')),
        'content' => $this->input->post('content'),
        'featured_img' => $featured_img,
        'category_id' => $this->input->post('category_id')
    );
    $this->db->where('id', $id);
    return $this->db->update('pages', $data);
}

嘗試以下代碼,僅當其中包含數據(如果用戶上傳圖片)時,它才會更新featured_img

控制器:

public function update($id) {
    $this->form_validation->set_rules('tite', 'title', 'required');
    $this->form_validation->set_rules('slug', 'Slug', 'required');    
    if ($this->form_validation->run() === FALSE) {
        $data['category'] = $this->category_model->all();
        $this->load->view('backend/templates/header');
        $this->load->view('backend/templates/navigation');
        $this->load->view('backend/pages/edit', $data);
        $this->load->view('backend/templates/footer');  
    } else {
        if (!empty($_FILES['userfile']['name'])) {              
            // upload featured image
            $config['upload_path'] = './uploads/page';
            $config['allowed_types']    = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '1080';
            $config['max_height'] = '768';
            $this->load->library('upload', $config);                
            if ( ! $this->upload->do_upload('userfile')) {
                $data['page'] = $this->page_model->find($id);
                $data['category'] = $this->category_model->all();
                $data['error'] = $this->upload->display_errors();
                $this->load->view('backend/templates/header');
                $this->load->view('backend/templates/navigation');
                $this->load->view('backend/pages/edit', $data);
                $this->load->view('backend/templates/footer');
            } else {
                $data = array('upload_data' => $this->upload->data());
                $featured_img = $_FILES['userfile']['name'];
                var_dump($data);
                var_dump($featured_img);
            }
        } // else {

            if (isset($featured_img)) {
                $param['featured_img'] = $featured_img;
            }
            $param['id'] = $id;
            $this->page_model->update($param);

            redirect('backend/page', 'refresh');    
        // }    
    }
}

型號:

public function update($param) {
    $data = array(
        'title' => ucwords($this->input->post('title')),
        'slug' => strtolower($this->input->post('slug')),
        'content' => $this->input->post('content'),
        // 'featured_img' => $featured_img,
        'category_id' => $this->input->post('category_id')
    );
    if (isset($param['featured_img'])) {
        $data['featured_img'] = $param['featured_img'];
    }
    $this->db->where('id', $param['id']);
    return $this->db->update('pages', $data);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM