จากตอนที่แล้ว เราเรียนรู้วิธีการสร้างรูปแบบของ model เพื่อนำมาใช้งาน
ร่วมกับโปรเจ็คของเรา
การสร้าง model และเรียกใช้งาน พร้อมแนวทางการประยุกต์ ตอนที่ 1
https://www.ninenik.com/content.php?arti_id=667 via @ninenik
เนื้อหาตอนต่อจากนี้ จะเป็นการเข้าไปเขียนโค้ดให้กับฟังก์ชั่นของ model ที่เราสร้าง
โดยจะหยิบการศึกษาเกี่ยวกับ query builder class หรือ class เกี่ยวกับการใช้งานฐาน
ข้อมูลของ codeigniter มาใช้ด้วย
ก่อนเข้าไปในรายละเอียด ตามโตรงสร้างโปรเจ็คของเรา ให้เราสร้างตาราง tbl_service
เพื่อสำหรับเก็บข้อมูล "บริการของเรา" ตามนี้
CREATE TABLE IF NOT EXISTS `tbl_service` ( `service_id` int(10) unsigned NOT NULL, `service_title` varchar(200) NOT NULL, `service_detail` text NOT NULL, `service_img` varchar(50) NOT NULL, `service_update` date NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `tbl_service` ADD PRIMARY KEY (`service_id`); ALTER TABLE `tbl_service` MODIFY `service_id` int(10) unsigned NOT NULL AUTO_INCREMENT;
เสร็จแล้วให้เราเปิดไฟล์ model ของเรา ซึ่งชื่อ Service_model.php อยู่ในโฟลเดอร์
apps > models > admin
<?php
class Service_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function getlist(){
echo "List";
}
public function create(){
echo "Create";
}
public function view(){
echo "View";
}
public function edit(){
echo "Edit";
}
public function delete(){
echo "Delete";
}
}
ฟังก์ชั่นที่เราสนใจ และพูดถึงในตอนนี้คือ getlist() view() และ delete()
การแสดงข้อมูลด้วย query builder class
ฟังก์ชั่นแรกที่เราจะพูดถึง คือ getlist() เราจะใช้สำหรับดึงรายการข้อมูลของตาราง tbl_service
มาแสดง และเพื่อให้เห็นผลการทำงาน ให้เราลองเพิ่มข้อมูลสมมติเข้าไปในตาราง tbl_service
ผ่าน phpmyadmin ก่อนสัก 2-3 รายการ
public function getlist(){
echo "List";
}
ในการดึงข้อมูลเราจะใช้
$this->db->get()
ตัวอย่าง
$query = $this->db->get('tbl_service'); // คิวรี่คำสั่ง: SELECT * FROM tbl_service
$query = $this->db->get('tbl_service', 10, 20);
// คิวรี่คำสั่ง: SELECT * FROM tbl_service LIMIT 20, 10
ถ้าต้องการเลือกเฉพาะ ฟิลด์รายการที่ต้องการ จะใช้คำสั่ง
$this->db->select()
ตัวอย่าง
$this->db->select('title,detail');
$query = $this->db->get('tbl_service');
// คิวรี่คำสั่ง: SELECT title,detail FROM tbl_service
แบบนี้เป็นต้น
* รายละเอียดฟังก์ชั่นเพิ่มเติม ให้ดูตัวอย่างในคู่มือของ codeigniter ในที่นี้จะใช้เท่าที่จำเป็นสำหรับโปรเจ็ค
สำหรับฟังก์ชั่น getlist() เราจะดึงข้อมูลทั้งหมด และ return ผลกลับมาในรุปแบบ array
ดังนั้นเราจะใช้
$query = $this->db->get('tbl_service');
return $query->result_array(); // ส่งผลลัพธ์กลับมาเป็น array ข้อมูลไว้ใช้งาน
จะได้ฟังก์ชั่น getlist() เป็นดังนี้
public function getlist(){
$query = $this->db->get('tbl_service');
return $query->result_array();
}
การแสดงข้อมูลด้วย query builder class แบบมีเงื่อนไข
ฟังก์ชั่นที่สองคือ view() เนื่องจากฟังก์ชั่นนี้เราใช้สำหรับดังข้อมูลอ้างอิงเงื่อนไข service_id
ดังนั้นจำเป็นต้องกำหนด parameter เพิ่มเติมให้ฟังก์ชั่นนี้
public function view($id){
echo "View";
}
และเราจะใช้ การดังข้อมูลด้วย
$this->db->get_where()
ตัวอย่างการใช้งาน
$query = $this->db->get_where('tbl_service',array('service_id'=>'1'));
// คิวรี่คำสั่ง: SELECT * FROM tbl_service WHERE service_id=1
ดั้งนั้นในฟังก์ชั่น view() ของเราจะได้เป็น
public function view($id){ // มี $id เป็น parameter ไว้กำหนดเงื่อนไข
$query = $this->db->get_where('tbl_service',array('service_id'=>$id));
return $query->row_array(); // ส่งข้อมูลผลัพธ์กลับเป็น array แถวข้อมูล
}
การลบข้อมูลด้วย query builder class
ส่วนสุดท้ายที่จะกล่าวถึงคือฟังก์ชั่น delete() สำหรับฟังก์ชั่นนี้จะเป็นการลบรายการข้อมูล
อ้างอิงจาก service_id เช่นเดียวกับ view ดังนั้นจึงต้องมีการกำหนด parameter เพื่อรับค่า
จะได้เป็น
public function delete($id){
echo "Delete";
}
และเราจะใช้คำสั่งต่อไปนี้ในการลบข้อมูล
$this->db->delete()
ตัวอย่างการใช้งาน
$this->db->delete('tbl_service', array('service_id' => '1'));
// คิวรี่คำสั่ง: // DELETE FROM tbl_service WHERE service_id = 1
ดั้งนั้นในฟังก์ชั่น delete() ของเราจะได้เป็น
public function delete($id){
return $this->db->delete('tbl_service', array('service_id' =>$id));
// คืนค่าผลการคิวรี่
}
และเราจะได้ไฟล์ Service_model.php สำหรับใช้งานเบื้องต้นดังนี้
<?php
class Service_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function getlist(){
$query = $this->db->get('tbl_service');
return $query->result_array();
}
public function create(){
echo "Create";
}
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(){
echo "Edit";
}
public function delete($id){
return $this->db->delete('tbl_service', array('service_id' =>$id));
// คืนค่าผลการคิวรี่
}
}
การเรียกใช้งาน model
เมื่อเราได้ฟังก์ชั่นเบื้องต้นของ model แล้วต่อไปเราจะเรียกใช้งานในไฟล์ views
ให้เราเปิดไฟล์ admin_service.php ในโฟลเดอร์ apps > views > admin
ส่วนแรก เราจะเรียกใช้ ฟังก์ชั่น getlist() ของ model เพื่อนำข้อมูลมาแสดงในส่วน $action เท่ากับ null
โดยรุปแบบการเรียกใช้งาน model เป็นดังนี้
$this->ชื่อ model -> ชื่อฟังก์ชั่น
ดังนั้นเราจะได้เป็น
$result = $this->service_model->getlist();
เมื่อนำมาใช้งานในส่วนของ $action เท่ากับ null เราจะได้เป็นดังนี้
<?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 } ?>
จากโค้ด การทำงานก็คือ ไปดึงข้อมูลจากตาราง tbl_service แล้วคืนค่ากลับมาเป็น array ข้อมูล
มาเก็บไว้ในตัวแปร $result จากนั้นเราก็ทำการวนลูป แสดงค่าตัวแปร array ด้วยคำสั่ง foreach
ส่วนที่สอง เราจะเรียกใช้งานฟังก์ชั่น delete() ของ model ในส่วนของ $action เท่ากับ delete
รูปแบบการใช้งาน
$query = $this->service_model->delete($id);
เมื่อนำมาใช้งานในส่วนของ $action เท่ากับ delete เราจะได้เป็นดังนี้
<?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 } ?>
จากโค้ด การทำงาน เมื่อมีการลบข้อมูล และส่งกลับค่าการคิวรี่มา ก็จะแสดงข้อความ
แจ้งว่าข้อมูลได้ถูกลบเรียบร้อยแล้ว
ส่วนที่สาม ส่วนสุดท้ายของการเรียกใช้งานที่จะกล่าวถึงในเนื้อหาตอนนี้คือ การใช้งานฟังก์ชั่น view() ของ
model ในส่วนของ $action เท่ากับ edit โดยในที่นี้เราจะยังไม่เอาข้อมูลมาใช้โดยตรง แต่จะแสดงผล
ด้วยตัวอย่างข้อมูล array ที่ได้ก่อน
รูปแบบการใช้งาน
$row = $this->service_model->view($id);
เมื่อนำมาใช้งานในส่วนของ $action เท่ากับ edit เราจะได้เป็นดังนี้
<?php if($action=="edit"){?>
<pre>
<?php
$row = $this->service_model->view($id);
print_r($row);
?>
</pre>
<a href="<?=base_url('admin/service')?>" class="btn btn-warning btn-sm">< Back</a>
<br><br>
<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" style="width:500px;">
</td>
</tr>
<tr>
<th width="120">Detail:</th>
<td>
<textarea name="service_detail" cols="85" rows="10"></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 } ?>
จากโค้ด หลักการทำงาน จะทำการดึงข้อมูลจากตาราง tbl_service โดยเงื่อนไข service_id เท่ากับค่า
ตัวแปร $id ที่ส่งมา และในตัวอย่าง เป็นการรับค่าเป็นข้อมูลเดียว เรารับค่าด้วยตัวแปร $row
และเมื่อทำการ print_r() ข้อมูลดูก็จะได้รายการข้อมูลในรูปแบบ array ไว้ใช้งาน
สุดท้ายเราจะได้ไฟล์ admin_service.php ในโฟลเดอร์ apps > views > admin
เบื้องต้นดังนี้
<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>
<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" style="width:500px;">
</td>
</tr>
<tr>
<th width="120">Detail:</th>
<td>
<textarea name="service_detail" cols="85" rows="10"></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"){?>
<pre>
<?php
$row = $this->service_model->view($id);
print_r($row);
?>
</pre>
<a href="<?=base_url('admin/service')?>" class="btn btn-warning btn-sm">< Back</a>
<br><br>
<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" style="width:500px;">
</td>
</tr>
<tr>
<th width="120">Detail:</th>
<td>
<textarea name="service_detail" cols="85" rows="10"></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>
ขอจบในส่วนของ 3 ฟังก์ชั่นเบื้องต้น กับการใช้งาน query builder class ใน model ไว้เท่านี้
สำหรับในส่วนของ create() และ edit() จะขอกล่าวในลำดับต่อไป เพราะ
ในสองส่วนี่เหลือนี้ เราจะศึกษาเรื่องของการตรวจสอบฟอร์มข้อมูล ก่อนทำการบันทึกด้วย รอติดตาม