การลบไฟล์ในที่นี้จะขออ้างอิงจากตอนที่แล้ว เป็นแนวทาง
การอัพโหลดไฟล์ด้วย file uploading class ใน codeigniter
https://www.ninenik.com/content.php?arti_id=670 via @ninenik
โดยเราจะดูในส่วนของการแก้ไขข้อมูล คือ ตามรูปแบบการแก้ไขข้อมูล
หากเมื่อเรามีการอัพโหลดไฟล์รูปภาพเข้าไป จากการใช้งาน file uploading
class ของตอนที่แล้ว เมื่อมีการแก้ไขและอัพโหลดไฟล์ไปใหม่ เราไม่ได้ทำการลบ
ไฟล์เดิมทิ้ง และ กรณีเช่น เราไม่ต้องการใช้รูปภาพ คือต้องการให้มีแค่หัวข้อ กับ
รายละเอียด ดังนั้นจึงควรที่จะสามารถ เลือกที่จะลบไฟล์ที่อัพโหลดไปแล้วได้
เหตุผลทั้งสองกรณีจึงมาสู่เนื้อหาของตอนนี้
การใช้ path helper ช่วยในการระบุตำแหน่งของไฟล์ที่แท้จริง
สำหรับ path helper ที่จะเราจะนำมาใช้งานร่วมนี้ จะมีฟังก์ชั่นแค่ฟังก์ชั่นเดียว
เพื่อใช้ในการระบุตำแหน่งของไฟล์ที่เราต้องการตรวจสอบ
ก่อนอื่นให้เราโหลด path helper ก่อนดังนี้
1 | $this ->load->helper( 'path' ); |
โดยให้กำหนดในไฟล์ Service_model.php ในโฟลเดอร์ apps > medels > admin
ในส่วนของฟังก์ชั่น __construct() จะได้เป็น
1 2 3 4 5 6 7 8 | public function __construct() { parent::__construct(); $this ->load->library( 'form_validation' ); $this ->form_validation->set_error_delimiters( '<div class="bg-danger" style="padding:3px 10px;">' , '</div>' ); $this ->load->library( 'upload' ); $this ->load->helper( 'path' ); } |
จากนั้นมาที่ไฟล์ views ไฟล์ admin_service.php ในส่วน $action เท่ากับ edit
สิ่งที่เราจะทำคือ
1. สร้างตัวแปรกำหนด relative path ไฟล์
1 | $fileCheck = './upload/' . $row [ 'service_img' ]; |
2. ใช้ฟังก์ชั่น set_realpath() ที่ได้จากการโหลด path helper สร้างตำแหน่งไฟล์ที่แท้จริง
1 | $full_fileCheck = set_realpath( $fileCheck ); |
ตัวอย่างเช่น
1 2 3 | $fileCheck = './upload/mypicture4.gif' ; $full_fileCheck = set_realpath( $fileCheck ); // จะได้ค่าตัวแปร $full_fileCheck เป็น C:xampphtdocslearnciuploadmypicture4.gif |
3. ตรวจสอบว่ามีไฟล์นี้จริงหรือไม่ และเป็นประเภทไฟล์หรือไม่(ไม่ใช่โฟลเดอร์)
1 2 3 | if ( file_exists ( $full_fileCheck ) && is_file ( $full_fileCheck )){ } |
4. ถ้าเป็นไฟล์จริงให้แสดงรูปภาพเดิมขนาดเล็กๆ ไว้ตรวจทานความถูกต้องการอัพโหลด
1 | <img src= "<?=base_url('upload/'.$row['service_img'])?>" style= "width:150px;" > |
5. จากนั้นให้เพิ่ม input checkbox ให้สามารถเลือกได้ว่า จะลบไฟล์นี้หรือไม่ โดยให้มีค่าเท่ากับ
ตำแหน่งจริงของไฟล์ที่ต้องการลบจากตัวแปร $full_fileCheck และในที่นี้ใช้ชื่อว่า d_service_image
1 | <input type= "checkbox" name= "d_service_image" value= "<?=$full_fileCheck?>" > คลิกเลือกหากต้องการลบไฟล์ |
ทั้งหมดใน 5 ขั้นตอน เราจะได้ในส่วนที่ต้องการดังนี้
1 2 3 4 5 6 7 8 9 10 11 12 | <input type= "file" name= "service_image" > <input type= "hidden" name= "h_service_image" value= "<?=$row['service_img']?>" > <?php $fileCheck = './upload/' . $row [ 'service_img' ]; $full_fileCheck = set_realpath( $fileCheck ); if ( file_exists ( $full_fileCheck ) && is_file ( $full_fileCheck )){ ?> <img src= "<?=base_url('upload/'.$row['service_img'])?>" style= "width:150px;" > <br> <input type= "checkbox" name= "d_service_image" value= "<?=$full_fileCheck?>" > คลิกเลือกหากต้องการลบไฟล์ <br> <?php } ?> |
โค้ดทั่้งหมดในส่วนของ $action เท่ากับ edit
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 | <?php if ( $action == "edit" ){?> <a href= "<?=base_url('admin/service')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <?php $this ->form_validation->set_rules( 'service_title' , 'Title' , 'required' ); $this ->form_validation->set_rules( 'service_detail' , 'Detail' , 'required' ); if ( $this ->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน // echo "Error"; } else { // กรณีตรวจสอบผ่าน $query = $this ->service_model->edit( $id ); if ( $query ){ // เมื่อแก้ไขข้อมูลเรียบร้อยแล้ว redirect( 'admin/service' ); // ไปหน้า service } } if (validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้ echo validation_errors(); echo "<br>" ; } if ( $this ->upload->display_errors()){ echo $this ->upload->display_errors( '<div class="bg-danger" style="padding:3px 10px;">' , '</div>' ); echo "<br>" ; } // เรียกใช้ฟังก์ชั่น view() ดึงข้อมูลมาแสดงก่อนแก้ไข $row = $this ->service_model->view( $id ); ?> <form action= "<?=base_url('admin/service/edit/'.$id)?>" method= "post" enctype= "multipart/form-data" > <table class = "table table-bordered" > <thead> <tr class = "active" > <th colspan= "2" >Edit Service</th> </tr> </thead> <tbody> <tr > <th width= "120" >Title:</th> <td> <input type= "text" name= "service_title" value= "<?=$row['service_title']?>" style= "width:500px;" > </td> </tr> <tr> <th width= "120" >Detail:</th> <td> <textarea name= "service_detail" cols= "85" rows= "10" ><?= $row [ 'service_detail' ]?></textarea> </td> </tr> <tr> <th width= "120" >Images:</th> <td> <input type= "file" name= "service_image" > <input type= "hidden" name= "h_service_image" value= "<?=$row['service_img']?>" > <?php $fileCheck = './upload/' . $row [ 'service_img' ]; $full_fileCheck = set_realpath( $fileCheck ); if ( file_exists ( $full_fileCheck ) && is_file ( $full_fileCheck )){ ?> <img src= "<?=base_url('upload/'.$row['service_img'])?>" style= "width:150px;" > <br> <input type= "checkbox" name= "d_service_image" value= "<?=$full_fileCheck?>" > คลิกเลือกหากต้องการลบไฟล์ <br> <?php } ?> </td> </tr> <tr> <th></th> <td> <input type= "submit" class = "btn btn-success btn-sm" name= "btn_add" value= "Edit Service" > </td> </tr> </tbody> </table> </form> <?php } ?> |
ต่อไปเรากลับมาดูในส่วนของ models ไฟล์ Service_model.php ในโฟลเดอร์ apps > models >admin
ในส่วนของฟังก์ชั่นการแก้ไข edit()
โค้ดเดิม
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 | public function edit( $id ){ $config [ 'upload_path' ] = './upload/' ; // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค $config [ 'allowed_types' ] = 'gif|jpg|png' ; // ปรเเภทไฟล์ $config [ 'max_size' ] = '0' ; // ขนาดไฟล์ (kb) 0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB $config [ 'max_width' ] = '1024' ; // ความกว้างรูปไม่เกิน $config [ 'max_height' ] = '768' ; // ความสูงรูปไม่เกิน $config [ 'file_name' ] = 'mypicture' ; // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม $this ->upload->initialize( $config ); // เรียกใช้การตั้งค่า $this ->upload->do_upload( 'service_image' ); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image $file_upload = $this ->input->post( 'h_service_image' ); // เก็บชื่อไฟล์เพิมถ้ามี if (! $this ->upload->display_errors()){ // ถ้าไม่มี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล $file_upload = $this ->upload->data( 'file_name' ); // เก็บชื่อไฟล์ใหม่ } else { // ถ้า error ในกรณีเลือกไฟล์แล้วไม่ผ่าน if ( $this ->upload->data( 'file_type' )){ // เช่น ประเภทไม่ถูกต้อง return ; // ต้อง return เพื่อให้แสดง error } } $newdata = array ( 'service_title' => $this ->input->post( 'service_title' ), 'service_detail' => $this ->input->post( 'service_detail' ), 'service_img' => $file_upload , 'service_update' => date ( "Y-m-d H:i:s" ) ); return $this ->db->update( 'tbl_service' , $newdata , array ( 'service_id' => $id )); } |
เราจะปรับแก้ไขใหม่เล็กน้อยตามเงื่อนการทำงานดังนี้
1. ก่อนที่จะทำการอัพโหลดไฟล์ใหม่ ถ้ามีการเลือกอัพโหลด ให้ตรวจสอบว่ามีการส่งคำขอ
ว่าต้องการลบไฟล์เดิมหรือไม่ โดยตรวจสอบจาก input checkbox ที่ชื่อ d_service_image
ที่มีการส่งตำแหน่งไฟล์ทีแท้จริงมา
1 | $fileExist = $this ->input->post( 'd_service_image' ); |
2. ตรวจสอบก่อนลบไฟล์ เมื่อมีการส่งไฟล์ที่ต้องการลบมา ให้ตรวจสอบว่า มีไฟล์นี้จริงหรือไม่
และเป็นประเภทไฟล์หรือไม่(ไม่ใช่โหลเดอร์) โดยถ้าเป็นจริงก็ให้ทำการลบไฟล์นั้นด้วยคำสั่ง
unlink() และกำหนดชื่อไฟล์ใหม่เป็นค่าว่างไปก่อน
1 2 3 4 5 6 | if ( file_exists ( $fileExist ) && is_file ( $fileExist )){ unlink( $fileExist ); $file_upload = "" ; } else { } |
3. ถ้าไม่มีไฟล์นั้นอยู่จริง หรือไม่ได้มีการส่งคำสั่งลบไฟล์เข้ามา ก็ให้ชื่อไฟล์ มีค่าเท่ากับชื่อไฟล์เดิม(ถ้ามี)
โดยได้ถูกส่งมาด้วย input hidden ที่ชื่อ h_service_image
1 | $file_upload = $this ->input->post( 'h_service_image' ); // เก็บชื่อไฟล์เพิมถ้ามี |
4. มีบางกรณีที่มีชื่อไฟล์ในฐานข้อมูล ทำให้ตัวแปร hidden ส่งค่าชื่อไฟล์มา แต่ความเป็น
ในระบบไม่มีชื่อไฟล์นี้ เราจึงเพิ่มในส่วนของการตรวจสอบไฟล์อีกครั้งว่า ชื่อไฟล์เดิมที่ส่งมา
พอตรวจสอบจากตำแหน่งจริงๆ แล้วมีไฟล์อยู่จริงหรือไม่ ถ้าไม่มี เราก็จะให้ชื่อไฟล์เป็นค่าว่าง
1 2 3 4 5 6 7 8 9 10 11 | if ( file_exists ( $fileExist ) && is_file ( $fileExist )){ unlink( $fileExist ); $file_upload = "" ; } else { $file_upload = $this ->input->post( 'h_service_image' ); // เก็บชื่อไฟล์เพิมถ้ามี $fileCheck = './upload/' . $file_upload ; $full_fileCheck = set_realpath( $fileCheck ); if (! file_exists ( $full_fileCheck ) || ! is_file ( $full_fileCheck )){ $file_upload = "" ; } } |
5. จากนั้นก็ให้ทำการอัพโหลดไฟล์ปกติถ้ามีการเลือกไฟล์เพื่ออัพโหลด
1 | $this ->upload->do_upload( 'service_image' ); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image |
ดูโค้ดเต็มในส่วนของฟังก์ชั่น edit() จะได้ดังนี้
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 | public function edit( $id ){ $config [ 'upload_path' ] = './upload/' ; // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค $config [ 'allowed_types' ] = 'gif|jpg|png' ; // ปรเเภทไฟล์ $config [ 'max_size' ] = '0' ; // ขนาดไฟล์ (kb) 0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB $config [ 'max_width' ] = '1024' ; // ความกว้างรูปไม่เกิน $config [ 'max_height' ] = '768' ; // ความสูงรูปไม่เกิน $config [ 'file_name' ] = 'mypicture' ; // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม $this ->upload->initialize( $config ); // เรียกใช้การตั้งค่า $fileExist = $this ->input->post( 'd_service_image' ); if ( file_exists ( $fileExist ) && is_file ( $fileExist )){ unlink( $fileExist ); $file_upload = "" ; } else { $file_upload = $this ->input->post( 'h_service_image' ); // เก็บชื่อไฟล์เพิมถ้ามี $fileCheck = './upload/' . $file_upload ; $full_fileCheck = set_realpath( $fileCheck ); if (! file_exists ( $full_fileCheck ) || ! is_file ( $full_fileCheck )){ $file_upload = "" ; } } $this ->upload->do_upload( 'service_image' ); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image if (! $this ->upload->display_errors()){ // ถ้าไม่มี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล $file_upload = $this ->upload->data( 'file_name' ); // เก็บชื่อไฟล์ใหม่ } else { // ถ้า error ในกรณีเลือกไฟล์แล้วไม่ผ่าน if ( $this ->upload->data( 'file_type' )){ // เช่น ประเภทไม่ถูกต้อง return ; // ต้อง return เพื่อให้แสดง error } } $newdata = array ( 'service_title' => $this ->input->post( 'service_title' ), 'service_detail' => $this ->input->post( 'service_detail' ), 'service_img' => $file_upload , 'service_update' => date ( "Y-m-d H:i:s" ) ); return $this ->db->update( 'tbl_service' , $newdata , array ( 'service_id' => $id )); } |
เราจะได้ไฟล์ Service_model.php ในโฟลเดอร์ apps > models > admin ดังนี้
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 82 83 84 85 86 87 88 89 90 91 92 93 94 | <?php class Service_model extends CI_Model { public function __construct() { parent::__construct(); $this ->load->library( 'form_validation' ); $this ->form_validation->set_error_delimiters( '<div class="bg-danger" style="padding:3px 10px;">' , '</div>' ); $this ->load->library( 'upload' ); $this ->load->helper( 'path' ); } public function getlist(){ $query = $this ->db->get( 'tbl_service' ); return $query ->result_array(); } public function create(){ $config [ 'upload_path' ] = './upload/' ; // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค $config [ 'allowed_types' ] = 'gif|jpg|png' ; // ปรเเภทไฟล์ $config [ 'max_size' ] = '0' ; // ขนาดไฟล์ (kb) 0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB $config [ 'max_width' ] = '1024' ; // ความกว้างรูปไม่เกิน $config [ 'max_height' ] = '768' ; // ความสูงรูปไม่เกิน $config [ 'file_name' ] = 'mypicture' ; // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม $this ->upload->initialize( $config ); // เรียกใช้การตั้งค่า $this ->upload->do_upload( 'service_image' ); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image $file_upload = "" ; // กำหนดชื่อไฟล์เป็นค่าว่าง if (! $this ->upload->display_errors()){ // ถ้าไม่มี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล $file_upload = $this ->upload->data( 'file_name' ); } $newdata = array ( 'service_id' => NULL, 'service_title' => $this ->input->post( 'service_title' ), 'service_detail' => $this ->input->post( 'service_detail' ), 'service_img' => $file_upload , 'service_update' => date ( "Y-m-d H:i:s" ) ); return $this ->db->insert( 'tbl_service' , $newdata ); } public function view( $id ){ // มี $id เป็น parameter ไว้กำหนดเงื่อนไข $query = $this ->db->get_where( 'tbl_service' , array ( 'service_id' => $id )); return $query ->row_array(); // ส่งข้อมูลผลัพธ์กลับเป็น array แถวข้อมูล } public function edit( $id ){ $config [ 'upload_path' ] = './upload/' ; // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค $config [ 'allowed_types' ] = 'gif|jpg|png' ; // ปรเเภทไฟล์ $config [ 'max_size' ] = '0' ; // ขนาดไฟล์ (kb) 0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB $config [ 'max_width' ] = '1024' ; // ความกว้างรูปไม่เกิน $config [ 'max_height' ] = '768' ; // ความสูงรูปไม่เกิน $config [ 'file_name' ] = 'mypicture' ; // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม $this ->upload->initialize( $config ); // เรียกใช้การตั้งค่า $fileExist = $this ->input->post( 'd_service_image' ); if ( file_exists ( $fileExist ) && is_file ( $fileExist )){ unlink( $fileExist ); $file_upload = "" ; } else { $file_upload = $this ->input->post( 'h_service_image' ); // เก็บชื่อไฟล์เพิมถ้ามี $fileCheck = './upload/' . $file_upload ; $full_fileCheck = set_realpath( $fileCheck ); if (! file_exists ( $full_fileCheck ) || ! is_file ( $full_fileCheck )){ $file_upload = "" ; } } $this ->upload->do_upload( 'service_image' ); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image if (! $this ->upload->display_errors()){ // ถ้าไม่มี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล $file_upload = $this ->upload->data( 'file_name' ); // เก็บชื่อไฟล์ใหม่ } else { // ถ้า error ในกรณีเลือกไฟล์แล้วไม่ผ่าน if ( $this ->upload->data( 'file_type' )){ // เช่น ประเภทไม่ถูกต้อง return ; // ต้อง return เพื่อให้แสดง error } } $newdata = array ( 'service_title' => $this ->input->post( 'service_title' ), 'service_detail' => $this ->input->post( 'service_detail' ), 'service_img' => $file_upload , 'service_update' => date ( "Y-m-d H:i:s" ) ); return $this ->db->update( 'tbl_service' , $newdata , array ( 'service_id' => $id )); } public function delete ( $id ){ return $this ->db-> delete ( 'tbl_service' , array ( 'service_id' => $id )); // คืนค่าผลการคิวรี่ } } |
และไฟล์ admin_service.php ในโฟลเดอร์ apps > views > admin ดังนี้
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | <div class = "container" > Service <br><br> <?php if ( $action ==null){?> <?php $result = $this ->service_model->getlist(); ?> <a href= "<?=base_url('admin/service/create')?>" class = "btn btn-primary btn-sm" >Create</a> <br><br> <table class = "table table-striped table-bordered table-condensed" > <thead> <tr> <th width= "50" class = "text-center" >#</th> <th>Title</th> <th width= "150" class = "text-center" >Modify Date </th> <th width= "150" class = "text-center" >Manage</th> </tr> </thead> <tbody> <?php $i_num =0; if ( count ( $result )>0){ foreach ( $result as $row ){ $i_num ++; ?> <tr> <td class = "text-center" ><?= $i_num ?></td> <td><?= $row [ 'service_title' ]?></td> <td class = "text-center" ><?= $row [ 'service_update' ]?></td> <td class = "text-center" > <a href= "<?=base_url('admin/service/edit/'.$row['service_id'])?>" class = "btn btn-success btn-sm" > <span class = "glyphicon glyphicon-pencil" aria-hidden= "true" ></span> </a> <a href= "<?=base_url('admin/service/delete/'.$row['service_id'])?>" class = "btn btn-danger btn-sm" > <span class = "glyphicon glyphicon-remove" aria-hidden= "true" ></span> </a> </td> </tr> <?php } ?> <?php } ?> </tbody> </table> <?php } ?> <?php if ( $action == "create" ){?> <a href= "<?=base_url('admin/service')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <?php $this ->form_validation->set_rules( 'service_title' , 'Title' , 'required' ); $this ->form_validation->set_rules( 'service_detail' , 'Detail' , 'required' ); if ( $this ->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน // echo "Error"; } else { // กรณีตรวจสอบผ่าน $query = $this ->service_model->create(); if ( $query ){ // เมื่อเพิ่มข้อมูลเรียบร้อยแล้ว redirect( 'admin/service' ); // ไปหน้า service } } if (validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้ echo validation_errors(); echo "<br>" ; } if ( $this ->upload->display_errors()){ echo $this ->upload->display_errors( '<div class="bg-danger" style="padding:3px 10px;">' , '</div>' ); echo "<br>" ; } ?> <form action= "<?=base_url('admin/service/create')?>" method= "post" enctype= "multipart/form-data" > <table class = "table table-bordered" > <thead> <tr class = "active" > <th colspan= "2" >Add New Service</th> </tr> </thead> <tbody> <tr > <th width= "120" >Title:</th> <td> <input type= "text" name= "service_title" value= "<?=set_value('service_title')?>" style= "width:500px;" > </td> </tr> <tr> <th width= "120" >Detail:</th> <td> <textarea name= "service_detail" cols= "85" rows= "10" ><?=set_value( 'service_detail' )?></textarea> </td> </tr> <tr> <th width= "120" >Images:</th> <td> <input type= "file" name= "service_image" > </td> </tr> <tr> <th></th> <td> <input type= "submit" class = "btn btn-success btn-sm" name= "btn_add" value= "Add Service" > </td> </tr> </tbody> </table> </form> <?php } ?> <?php if ( $action == "edit" ){?> <a href= "<?=base_url('admin/service')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <?php $this ->form_validation->set_rules( 'service_title' , 'Title' , 'required' ); $this ->form_validation->set_rules( 'service_detail' , 'Detail' , 'required' ); if ( $this ->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน // echo "Error"; } else { // กรณีตรวจสอบผ่าน $query = $this ->service_model->edit( $id ); if ( $query ){ // เมื่อแก้ไขข้อมูลเรียบร้อยแล้ว redirect( 'admin/service' ); // ไปหน้า service } } if (validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้ echo validation_errors(); echo "<br>" ; } if ( $this ->upload->display_errors()){ echo $this ->upload->display_errors( '<div class="bg-danger" style="padding:3px 10px;">' , '</div>' ); echo "<br>" ; } // เรียกใช้ฟังก์ชั่น view() ดึงข้อมูลมาแสดงก่อนแก้ไข $row = $this ->service_model->view( $id ); ?> <form action= "<?=base_url('admin/service/edit/'.$id)?>" method= "post" enctype= "multipart/form-data" > <table class = "table table-bordered" > <thead> <tr class = "active" > <th colspan= "2" >Edit Service</th> </tr> </thead> <tbody> <tr > <th width= "120" >Title:</th> <td> <input type= "text" name= "service_title" value= "<?=$row['service_title']?>" style= "width:500px;" > </td> </tr> <tr> <th width= "120" >Detail:</th> <td> <textarea name= "service_detail" cols= "85" rows= "10" ><?= $row [ 'service_detail' ]?></textarea> </td> </tr> <tr> <th width= "120" >Images:</th> <td> <input type= "file" name= "service_image" > <input type= "hidden" name= "h_service_image" value= "<?=$row['service_img']?>" > <?php $fileCheck = './upload/' . $row [ 'service_img' ]; $full_fileCheck = set_realpath( $fileCheck ); if ( file_exists ( $full_fileCheck ) && is_file ( $full_fileCheck )){ ?> <img src= "<?=base_url('upload/'.$row['service_img'])?>" style= "width:150px;" > <br> <input type= "checkbox" name= "d_service_image" value= "<?=$full_fileCheck?>" > คลิกเลือกหากต้องการลบไฟล์ <br> <?php } ?> </td> </tr> <tr> <th></th> <td> <input type= "submit" class = "btn btn-success btn-sm" name= "btn_add" value= "Edit Service" > </td> </tr> </tbody> </table> </form> <?php } ?> <?php if ( $action == "delete" ){?> <?php $query = $this ->service_model-> delete ( $id ); ?> <a href= "<?=base_url('admin/service')?>" class = "btn btn-warning btn-sm" >< Back</a> <br><br> <?php if ( $query ){?> <div class = "bg-success text-center" style= "padding:10px;" > <p class = "text-success" > Delete data complete</p> <a href= "<?=base_url('admin/service')?>" class = "text-success" >< Back > </a> </div> <?php } ?> <?php } ?> </div> |
เป็นอันเสร็จสำหรับในการใช้งานการแก้ไข หรือลบรูปภาพ รวมถึงการใช้งาน path helper
และก็ขอจบในส่วนของการใช้งานเกี่ยวกับระบบ admin service สำหรับแสดงข้อมูลในหน้า บริการของเราไว้เท่านี้
ตอนต่อไปเราจะมาดูในเรื่องของการใช้งาน cookie
และก็ขอจบในส่วนของการใช้งานเกี่ยวกับระบบ admin service สำหรับแสดงข้อมูลในหน้า บริการของเราไว้เท่านี้
ตอนต่อไปเราจะมาดูในเรื่องของการใช้งาน cookie