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 ครับ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 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 ครับ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <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> |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
ลองแนวทางประมาณนี้ดู
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <?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 | อ่าน 19,665 |

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

ขอบคุณทุกการสนับสนุน
![]()