Codeigniter อยากใส่ do_upload 3 ครั้ง เพื่ออ่านค่าที่อัพโหลดต่างกันใน database

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา Codeigniter อยากใส่ do_upload 3 ครั้ง เพื่ออ่านค่าที่อัพโหลดต่างกันใน database

Codeigniter อยากใส่ do_upload 3 ครั้ง เพื่ออ่านค่าที่อัพโหลดต่างกันใน database

สวัสดีครับ มือใหม่ครับ

ผมอยากจะใส่ do_upload 3 รอบ เพื่อรับค่าอัพโหลดจาก 3 field upload มาลงยัง Database ให้ต่างกัน ได้อย่างไรบ้างครับ

ตอนนี้ผมติดปัญหาที่ว่า 3 field upload ที่สร้างไว้ มันอัพโหลดเป็นไฟล์เดียวกันครับ

เช่น ที่อยากให้เป็นคือ

ตาราง img1 อัพโหลด 01.jpg

img2 อัพโหลด 02.jpg 

img3 อัพโหลด 03.jpg

แบบนี้ครับ

 

Model ครับ

public function update($value = '')
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2000';
        $config['max_width'] = '2000';
        $config['max_height'] = '2000';
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('img'))
        {
            $arr1 = array(
                'img_name_th' => $this->input->post('img_name_th'),
                'img_subname_th' => $this->input->post('img_subname_th'),
                'img_color' => $this->input->post('img_color'),
                'img_detail_th' => $this->input->post('img_detail_th'),
                'img_price' => $this->input->post('img_price'),
                'img_formular_th' => $this->input->post('img_formular_th'),
                'img_howto_th' => $this->input->post('img_howto_th'),
                'img_ingredients_th' => $this->input->post('img_ingredients_th'),
                'img_usage_th' => $this->input->post('img_usage_th'),
                'img_name_en' => $this->input->post('img_name_en'),
                'img_subname_en' => $this->input->post('img_subname_en'),
                'img_detail_en' => $this->input->post('img_detail_en'),
                'img_formular_en' => $this->input->post('img_formular_en'),
                'img_howto_en' => $this->input->post('img_howto_en'),
                'img_ingredients_en' => $this->input->post('img_ingredients_en'),
                'img_usage_en' => $this->input->post('img_usage_en'),
                'img_typename' => $this->input->post('img_typename'),
                'img' => $this->input->post('img'),
                'img2' => $this->input->post('img2'),
                'img3' => $this->input->post('img3'),
            );
            $this->db->where('img_id', $this->input->post('img_id'));
            $this->db->update('img', $arr1);
            $this->session->set_flashdata(
                array(
                    'msginfo' => 'OK',
                )
            );

            redirect('', 'refresh');

        } else
        {
            $data = $this->upload->data();
            $filename = $data['file_name'];
	    $filename2 = $data['file_name'];
            $filename3 = $data['file_name'];
            $arr = array(
                'img_name_th' => $this->input->post('img_name_th'),
                'img_subname_th' => $this->input->post('img_subname_th'),
                'img_color' => $this->input->post('img_color'),
                'img_detail_th' => $this->input->post('img_detail_th'),
                'img_price' => $this->input->post('img_price'),
                'img_formular_th' => $this->input->post('img_formular_th'),
                'img_howto_th' => $this->input->post('img_howto_th'),
                'img_ingredients_th' => $this->input->post('img_ingredients_th'),
                'img_usage_th' => $this->input->post('img_usage_th'),
                'img_name_en' => $this->input->post('img_name_en'),
                'img_subname_en' => $this->input->post('img_subname_en'),
                'img_detail_en' => $this->input->post('img_detail_en'),
                'img_formular_en' => $this->input->post('img_formular_en'),
                'img_howto_en' => $this->input->post('img_howto_en'),
                'img_ingredients_en' => $this->input->post('img_ingredients_en'),
                'img_usage_en' => $this->input->post('img_usage_en'),
                'img_typename' => $this->input->post('img_typename'),
                'img' => $filename,
	        'img2' => $filename2,
                'img3' => $filename3
            );
            $this->db->where('img_id', $this->input->post('img_id'));
            $this->db->update('img', $arr);
            $this->session->set_flashdata(
                array(
                    'msginfo' => 'ทำรายการสำเร็จ',
                )
            );
            redirect('', 'refresh');
        }
    }

 

VIEW ครับ

<div class="form-group">
                                <label for="exampleInputEmail1">
                                <h4> Image / รูปผลิตภัณฑ์ </h4> (<a href="<?php echo base_url('uploads/' . $doc->img); ?>" target="_blank"> ไฟล์เก่า </a>)
                                    <br>
                                    <a href="<?php echo base_url('uploads/' . $doc->img); ?>" target="_blank"><img src="<?php echo base_url('uploads/' . $doc->img); ?>" width="100px"></a>
                                </label>
                                    <input type="file" name="img" id="img" >

<div class="form-group">
                                <label for="exampleInputEmail1">
                                <h4> Image / รูปผลิตภัณฑ์ </h4> (<a href="<?php echo base_url('uploads/' . $doc->img); ?>" target="_blank"> ไฟล์เก่า </a>)
                                    <br>
                                    <a href="<?php echo base_url('uploads/' . $doc->img2); ?>" target="_blank"><img src="<?php echo base_url('uploads/' . $doc->img2); ?>" width="100px"></a>
                                </label>
                                    <input type="file" name="img2" id="img2">

                            </div>
                            <div class="form-group">
                                <label>
                                <h4> Image 2 / รูปผลิตภัณฑ์ 2</h4> (<a href="<?php echo base_url('uploads/' . $doc->img3); ?>" target="_blank"> ไฟล์เก่า </a>)
                                    <br>
                                    <a href="<?php echo base_url('uploads/' . $doc->img3); ?>" target="_blank"><img src="<?php echo base_url('uploads/' . $doc->img3); ?>" width="100px"></a>
                                </label>
     
 


Jitti Keng 58.9.182.xxx 17-04-2019 17:13:56

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
ลองแนวทางประมาณนี้ดู

<?php

			$arr_fileInput = array("img","img2","img3"); // แสดงตัวแปร array ชื่อ input file
			foreach($arr_fileInput as $nameInput){ // วนลูป
					$_FILES['file']['name']     = $_FILES[$nameInput]['name'];
					$_FILES['file']['type']     = $_FILES[$nameInput]['type'];
					$_FILES['file']['tmp_name'] = $_FILES[$nameInput]['tmp_name'];
					$_FILES['file']['error']     = $_FILES[$nameInput]['error'];
					$_FILES['file']['size']     = $_FILES[$nameInput]['size'];
					
					$config['upload_path'] = './uploads/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['max_size'] = '2000';
					$config['max_width'] = '2000';
					$config['max_height'] = '2000';
					$this->load->library('upload', $config);
					$this->upload->initialize($config);
			
				   // Upload file 
					if($this->upload->do_upload('file')){ // อัพโหลดสำเร็จ
						// Uploaded file data
						$fileData = $this->upload->data();
						$upFile[$nameInput] = $fileData['file_name']; //  ชื่อไฟล์
					}else{ // อัพโหลดไม่ได้
						$upFile[$nameInput] = ""; // ชื่อว่าง
					}
									
			}
            $arr = array(
                'img_name_th' => $this->input->post('img_name_th'),
                'img_subname_th' => $this->input->post('img_subname_th'),
                'img_color' => $this->input->post('img_color'),
                'img_detail_th' => $this->input->post('img_detail_th'),
                'img_price' => $this->input->post('img_price'),
                'img_formular_th' => $this->input->post('img_formular_th'),
                'img_howto_th' => $this->input->post('img_howto_th'),
                'img_ingredients_th' => $this->input->post('img_ingredients_th'),
                'img_usage_th' => $this->input->post('img_usage_th'),
                'img_name_en' => $this->input->post('img_name_en'),
                'img_subname_en' => $this->input->post('img_subname_en'),
                'img_detail_en' => $this->input->post('img_detail_en'),
                'img_formular_en' => $this->input->post('img_formular_en'),
                'img_howto_en' => $this->input->post('img_howto_en'),
                'img_ingredients_en' => $this->input->post('img_ingredients_en'),
                'img_usage_en' => $this->input->post('img_usage_en'),
                'img_typename' => $this->input->post('img_typename'),
                'img' => $upFile['img'],
            	'img2' => $upFile['img2'],
                'img3' => $upFile['img3']
            );
            $this->db->where('img_id', $this->input->post('img_id'));
            $this->db->update('img', $arr);
            $this->session->set_flashdata(
                array(
                    'msginfo' => 'ทำรายการสำเร็จ',
                )
            );
            redirect('', 'refresh');


บทความแนะนำที่เกี่ยวข้อง
การอัพโหลดไฟล์ด้วย file uploading class ใน codeigniterอ่าน 18,482
ninenik 223.24.191.xxx 17-04-2019
 ความคิดเห็นที่ 2

ขอบคุณมากๆ เลยนะครับ

ตอนนี้อัพโหลดได้แล้วครับ เดี๋ยวจะลองทำความเข้าใจ และนำไปปรับใช้ดูนะครับ ขอบคุณมากๆ เลยนะครับ

: )



Jitti Keng 58.9.182.xxx 18-04-2019 08:44






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ