ใช้งาน Form Validation สำหรับบันทึกและแก้ไขข้อมูลใน model ตอนที่ 3

เขียนเมื่อ 8 ปีก่อน โดย Ninenik Narkdee
codeigniter codeigniter 3 form validation model

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ codeigniter codeigniter 3 form validation model

ดูแล้ว 12,796 ครั้ง


เราเหลือฟังก์ชั่นใน service model อีกสองรายการคือ create() และ edit()
อ่านตอนที่แล้วที่
 
ใช้ Query Builder Class ในฟังก์ชั่นของ model ตอนที่ 2 
 
สำหรับการใช้งานฟอร์มเพื่อบันทึกข้อมูลของตอนนี้เราจะศึกษาเพิ่มเติมในส่วนของการ
ตรวจสอบฟอร์มก่อนทำการบันทึกข้อมูล หรือที่เรียกว่า Form Validation ที่ทาง
codeigniter ก็อำนวยความสะดวกมาให้เป็นอย่างดี
 
เริ่มต้นให้เราไปที่ไฟล์ model ของเราก่อน
ชื่อไฟล์ Service_model.php ในโฟลเดอร์ apps > models > admin
จากนั้นทำการโหลด library ชื่อ form_validation มาใช้งานดังนี้
 
<?php
class Service_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }
    
..................
............
......
 
*ขอยกมาเฉพาะบางส่วนของโค้ด
 
สำหรับการทำงานของ form validation ก็จะตรวจสอบ input type ต่างๆ ตามเงื่อนไข
เช่น จำเป็นต้องกรอกไหม ต้องตรงกับรายการไหนหรือไม่ เป็นตัวเลข รูปแบบอีเมลถูกต้องหรือไม่
แบบนี้เป็นต้น และกรณีที่มีการตรวจสอบ แล้วไม่ตรงตามเงื่อนไข ก็จะมีฟังก์ชั่นแสดง error แจ้งเตือน
ในส่วนนี้ เราสามารถกำหนดรูปแบบการแสดง ของข้อความ error นั้นได้ โดยให้เพิ่มในส่วนของการกำหนด
ส่วนเปิด ปิด แท็กคลุมข้อความแจ้ง error ตามต้องการ หากไม่ได้กำหนด จะเป็นแท็ก <p>
สำหรับของเรา เราใช้งานร่วมกับ bootstrap css ก็จะใช้งาน class css ของ bootstrap ตามนี้
 
รูปแบบการกำหนด
 
$this->form_validation->set_error_delimiters('แท็กเปิด', 'แท็กปิด');
 
ของเราจะได้เป็น
 
$this->form_validation->set_error_delimiters('<div class="bg-danger" style="padding:3px 10px;">', '</div>');
 
แล้วแทรกส่วนนี้เข้าไป ต่อจากส่วนโหลด library จะได้เป็น
 
<?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>');
    }

..................
............
......
 
การกำหนดลักษณะดังกล่าวข้างต้น จะมีผลกับรูปแบบการแสดงข้อความแจ้งเตือน error ของการ
ใช้งาน form validation ของ controller ที่เรียกใช้งานอยู่
 
หากเราต้องการจะกำหนดให้มีผลกับทั้งหมดของโปรเจ็ค สามารถทำได้ดังนี้คือ
สร้างไฟล์ ชื่อ form_validation.php ไว้ในโฟลเดอร์ apps > config 
แล้วกำหนดโค้ดังนี้ จัดรูปแบบตามต้องการ
 
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$config['error_prefix'] = '<div class="bg-success" style="padding:3px 10px;">';
$config['error_suffix'] = '</div>';
 
นอกจากนี้ ยังมีการใช้งานแบบยืดหยุ่น คือเราสามารถกำหนดรูปแบบ ในหน้า views 
ที่เรียกใช้งานได้เลย เช่น
 
<?php echo   validation_errors('<div class="bg-danger" style="padding:3px 10px;">', '</div>'); ?>
 
 

การใช้งาน form validation ในไฟล์ views 

ให้เราเปิดไฟล์ admin_service.php ในส่วนของ $action เท่ากับ create 
เราจะทำการตรวจสอบฟอร์ม ตามลำดับ ดังนี้
 
1. กำหนด rules หรือเงื่อนไขการตรวจสอบ
    * เราสามารถดูเอกสารเกี่ยวกับรูปแบบของ rule ต่างๆ ได้ที่คู่มือ
    ในที่นี้เราจะกำหนดแบบง่าย คือ ใช้ required rule หรือก็คือจำเป็นต้องกรอก
 
โดยใช้ฟังก์ชั่น ดังนี้
 
$this->form_validation->set_rules();
 
ตัวอย่างการกำหนด
 
$this->form_validation->set_rules('service_title', 'Title', 'required');
$this->form_validation->set_rules('service_detail', 'Detail', 'required');

// set_rules('ชื่อ input ต่างๆ', 'ชื่อเรียกที่เข้าใจ อาจใช้หัวข้อ', 'ชื่อของ rule ที่ต้องการตรวจสอบ');
// ดังนั้น set_rules('service_title', 'Title', 'required'); จึงหมายถึง
// ค่าจาก input ที่ชื่อ service_title จำเป็นต้องกรอก  แบบนี้เป็นต้น
 
2. ทำการตรวจสอบฟอร์มตามเงื่อนไข
เราจะใช้ฟังก์ชั่นคำสั่ง
 
$this->form_validation->run()
 
ตัวอย่างวิธีการใช้
 
if($this->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน
    echo "Error";
}else{  // กรณีตรวจสอบผ่าน
    echo "Ok";
}
 
 
3. แสดงข้อความ error แจ้งเตือน กรณีเงื่อนไขการตรวจสอบฟอร์ม แล้วไม่ผ่าน
เราจะใช้ฟังก์ชั่นคำสั่ง
 
validation_errors()
 
จะได้เป็น
 
if(validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้
    echo   validation_errors();
    echo "<br>";
}
 
รูปแบบตามด้านบน จะเป็นการแสดงข้อมูลไว้ที่จุดเดียว รวมกันเป็นชุดข้อมูลแจ้ง error 
แต่ถ้าเราต้องการแสดงข้อมูลในตำแหน่งที่ต้องการ เช่น ถ้าช่องรายการใดไม่ได้กรอก ให้แสดง
error ข้างๆ หรือด้านล่างกล่องข้อความนั้น เราจะเรียกใช้งานผ่านฟังก์ชั่น
 
form_error();
 
ตัวอย่างเช่น
 
    <input type="text" name="service_title" style="width:500px;">
    <?php echo form_error('service_title');?>
 
 
4. แสดงข้อมูลเดิมกรณีเกิด error 
ยกตัวอย่างเช่น เมื่อเรากรอก title แต่ไม่ได้กรอก detail เมื่อมีการแจ้ง error ระบบก็จะแจ้งว่า
ให้เรากรอก detail ด้วย แค่ความจริงมี form validation ทำงาน ค่าของ title ก็จะถูกล้างไป 
เราต้องกรอกข้อมูลใหม่อีกครั้ง ดังนั้น วิธีการที่จะคงค่าของข้อมูลเดิม สำหรับรายการที่มี
การตรวจสอบแล้วถูกต้อง เราจะใช้งานฟังก์ชั่น
 
set_value('ชื่อ input field')
 
ตัวอย่างการใช้งาน
 
<input type="text" name="service_title" value="<?=set_value('service_title')?>" style="width:500px;">
<textarea name="service_detail" cols="85" rows="10"><?=set_value('service_detail')?></textarea>
 
ด้วย 4 ขั้นตอนข้างต้น ก็เป็นอันเสร็จในส่วนของการตรวจสอบฟอร์ม ก่อนส่งหรือใช้งานข้อมูล
*ในที่นี้เราจะไม่ได้พูดถึงส่วนของการใช้งาน input file จะไว้กลับมาดูอีกครั้งในเรื่องการใช้งาน
fileupload class

 
เราจะได้ไฟล์ admin_service.php ในส่วน $action เท่ากับ create ดังนี้
 
<?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{  // กรณีตรวจสอบผ่าน
    echo "Ok";
}
                            
if(validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้
    echo   validation_errors();
    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 } ?>
 
 

การเพิ่มข้อมูลในฐานข้อมูลด้วย query builder class

 
ทีนี้เรากลับมาไฟล์ model ของเรา ไฟล์ Service_model.php ในโฟลเดอร์
apps > models > admin
 
โดยเราจะทำการบันทึกข้อมูลลงฐานข้อมูลด้วยการกำหนดฟังก์ชั่น create() 
เพื่อบันทึกข้อมูล
 
    public function create(){
        echo "Create";
    }
 
ในการบันทึกหรือเพิ่มข้อมูลเราจะใช้
 
$this->db->insert()
 
ตัวอย่างการใช้งาน
 
$newdata = array(
        'service_title' => $this->input->post('service_title'),
        'service_detail' => $this->input->post('service_detail')

$this->db->insert('tbl_service', $newdata);
// คิวรี่คำสั่ง: INSERT INTO tbl_service (service_title,service_detail) 
//  VALUES ('ค่าตัวแปร $_POST['service_title']', 'ค่าตัวแปร $_POST['service_detail']'')
 
จะได้ฟังก์ชั่น create() เป็นดังนี้
 
    public function create(){
        $newdata = array(
            'service_id' => NULL,
            'service_title' => $this->input->post('service_title'),
            'service_detail' => $this->input->post('service_detail'),
            'service_img' => '',
            'service_update' => date("Y-m-d H:i:s")
        );
        return $this->db->insert('tbl_service', $newdata);
    }
 
จากโค้ด ค่า service_id เราจะให้เป็น NULL เนื่องจากค่าในฐานข้อมูลเป็น auto increment
อยู่แล้ว ส่วน
 
$this->input->post('service_title'),
 
เป็นการเรียกใช้งาน input class เป็นการอ้างอิงถึงตัวแปร $_POST['service_title']
ฟิลด์รูปภาพ service_img ในที่นี้เราจะใส่ค่าว่างลงไป ยังไม่ได้ทำอะไรก่อน
และสุดท้ายวันที่ service_update ก็จะใช้วันที่ของระบบ
เมื่อทำการคิวรี่แล้วก็จะคืนค่า ว่ามีการคิวรี่เกิดขึ้นหรือไม่
 
 

การอัพเดทข้อมูลในฐานข้อมูลด้วย query builder class

 
สำหรับการแก้ไขข้อมูลนั้นเราจะใช้ฟังก์ชั่น edit() และเนื่องจากการแก้ไขข้อมูล เราจะต้องอ้างอิง
กับ key หลัก หรือ primary key ที่ให้สัมพันธ์ว่า รายการไหนที่ตอ้งการอัพเดท ต้องนั้น จึงต้องเพิ่ม
parameter $id เข้ามา จะได้เป็น
 
    public function edit($id){
        echo "Edit";
    }  
 
และเราจะใช้คำสั่งในการอัพเดทข้อมูลด้วย
 
$this->db->update()
 
ตัวอย่างการใช้งาน
 
$newdata = array(
        'service_title' => $this->input->post('service_title'),
        'service_detail' => $this->input->post('service_detail')

$this->db->update('tbl_service', $newdata,array('service_id' => $id));
// คิวรี่คำสั่ง: 
// UPDATE `tbl_service`
// SET 
// `service_title` = 'ค่าตัวแปร $_POST['service_title']',
// `service_detail` = 'ค่าตัวแปร $_POST['service_detail']'
// WHERE service_id = `$id`
 
 
ดั้งนั้นในฟังก์ชั่น edit() ของเราจะได้เป็น
 
    public function edit($id){
        $newdata = array(
            'service_title' => $this->input->post('service_title'),
            'service_detail' => $this->input->post('service_detail'),
            'service_img' => '',
            'service_update' => date("Y-m-d H:i:s")
        );
        return $this->db->update('tbl_service', $newdata,array('service_id'=>$id));
    }  
 
จากโค้ดเราเลือกอัพเดทข้อมูลตามฟิลด์ที่ต้องการยกเว้น service_id ที่ไม่ต้องทำการอัพเดท
เพราะเป็น key หลัก สำหรับอ้างอิง เมื่อทำการคิวรี่คำสั่งแล้วให้คืนค่าการทำงานกลับมาว่า
ทำการคิวรี่ข้อมูลได้หรือไม่
 
เราจะได้ไฟล์ Service_model.php ดังนี้
 
<?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>');
    }
    
    public function getlist(){
        $query = $this->db->get('tbl_service');
        return $query->result_array();
    }    
    
    public function create(){
        $newdata = array(
            'service_id' => NULL,
            'service_title' => $this->input->post('service_title'),
            'service_detail' => $this->input->post('service_detail'),
            'service_img' => '',
            '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){
        $newdata = array(
            'service_title' => $this->input->post('service_title'),
            'service_detail' => $this->input->post('service_detail'),
            'service_img' => '',
            '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 ในส่วน $action เท่ากับ create ก็จะได้เป็น

 
<?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>";
}
?>
<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 } ?>
 
 

การเรียกใช้งานในไฟล์ admin_service.php ในส่วน $action เท่ากับ edit ก็จะได้เป็น

 
<?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>";
}
// เรียกใช้ฟังก์ชั่น 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" >
        </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 } ?>
 
เนื่องจากในส่วนของการแก้ไข จะมีลักษณะการใช้งานคล้ายกับในส่วนของการ เพิ่มข้อมูล
ดังนั้นจะไม่ขอกล่าวถึงในรายละเอียด สามารถดูเทียบกับในส่วนของการเพิ่มข้อมูลแทนได้
 
 
ไฟล์ admin_service.php ทั้งหมด
 
<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>
                &nbsp;&nbsp;
                 <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>";
}
?>
<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>";
}
// เรียกใช้ฟังก์ชั่น 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" >
        </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>
 
ทดสอบใช้งาน เพิ่มลบแก้ไขข้อมูล
เป็นอันจบในส่วนของการใช้งาน model ซึ่งพยายามให้จบใน สามตอน เนื้อหาทั้งหมด
จะเป็นแนวทางในการประยุกต์ต่อเพิ่มเติมได้
 
สำหรับเนื้อหาตอนต่อไปเราจะมาดูในเรื่องของการอัพโหลดไฟล์ รอติดตาม
 


กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ



อ่านต่อที่บทความ









เนื้อหาที่เกี่ยวข้อง









URL สำหรับอ้างอิง





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

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


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


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







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