8. CodeIgniter is a PHP Framework
▪ A Lightweight PHP Framework
▪ 麻雀雖小但五臟俱全
▪ High Performance
▪ 效能佳
▪ Easy to Use
▪ 使用簡單,無須複雜的設定
▪ Clear and Thorough Documentation
▪ 有豐富的中文文件,易於學習
8
30. Active Record (Selecting)
▪ 查詢資料
$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
// 產生: SELECT title, content, date FROM mytable
▪ 加入 WHERE 條件
$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status);
// WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
30
55. 新增資料表
▪ 連線至資料庫並執行以下 SQL 指令:
CREATE TABLE guestbook (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(128) NOT NULL,
text text NOT NULL,
time datetime NOT NULL,
PRIMARY KEY (id)
);
▪ 請試著增加幾筆資料,以測試查詢結果。
55
56. Controller (controllers/guestbook.php)
▪ 在 controllers/ 目錄下新增檔案,取名為 guestbook.php。
▪ 注意類別名稱必須第一個字母大寫。
<?php
class Guestbook extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('guestbook_model');
$this->load->helper(array('url', 'form'));
}
// 載入model
// 載入輔助函式
public function index()
{
$data['message'] = $this->guestbook_model->get_messages();
$this->load->view('guestbook/index', $data);
}
}
56
57. Model (models/guestbook_model.php)
▪ 在 models/ 目錄下新增檔案,取名為 guestbook_model.php。
▪ 類別名稱必須第一個字母大寫,model命名規則包含後綴字_model。
<?php
class Guestbook_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
/* 取得留言資料 */
public function get_messages()
{
$this->db->select('*');
$this->db->from('guestbook');
$this->db->order_by('id', 'desc');
$query = $this->db->get();
return $query->result_array();
}
}
57
61. Controller (controllers/guestbook.php)
▪ 在 Guestbook 類別內撰寫 function post(),用以顯示新增留言表單。
<?php
class Guestbook extends CI_Controller {
// some code here ......
// ......
public function post()
{
$this->load->view('guestbook/post');
}
}
61
62. Controller (controllers/guestbook.php)
▪ 在 Guestbook 類別內撰寫 function add(),接收 POST 資料,並透過
model 新增至資料庫。
<?php
class Guestbook extends CI_Controller {
// some code here ......
// ......
public function add()
{
$data['name'] = $this->input->post('name');
$data['text'] = $this->input->post('text');
$data['time'] = date('Y-m-d h:i:s');
$this->guestbook_model->add_message($data);
redirect(‘guestbook’);
}
// 呼叫model函式
// 返回主畫面
}
62
63. Controller (controllers/guestbook.php)
▪ 在 Guestbook 類別內撰寫 function edit(),用以顯示修改留言表單。
<?php
class Guestbook extends CI_Controller {
// some code here ......
// ......
public function edit($id)
{
$data['message'] = $this->guestbook_model
->get_message_by_id($id);
$this->load->view('guestbook/edit', $data);
}
}
63
64. Controller (controllers/guestbook.php)
▪ 在 Guestbook 類別內撰寫 function update(),接收 POST 資料,並透過
model 更新資料庫留言資料。
<?php
class Guestbook extends CI_Controller {
// some code here ......
// ......
public function update()
{
$id = $this->input->post('id');
$data['name'] = $this->input->post('name');
$data['text'] = $this->input->post('text');
$data['time'] = date('Y-m-d h:i:s');
$this->guestbook_model->update_message($id, $data);
redirect(‘guestbook’);
}
}
64
65. Controller (controllers/guestbook.php)
▪ 在 Guestbook 類別內撰寫 function delete(),透過 model 刪除資料庫留
言資料。
<?php
class Guestbook extends CI_Controller {
// some code here ......
// ......
public function delete($id)
{
$this->guestbook_model->delete_message($id);
redirect('guestbook');
}
}
65
66. Model (models/guestbook_model.php)
▪ 在 Guestbook_model 類別內撰寫 function get_message_by_id($id) ,向
資料庫取得單筆留言資料。
<?php
class Guestbook_model extends CI_Model {
// some code here ......
// ......
public function get_message_by_id($id)
{
$this->db->select('*');
$this->db->from('guestbook');
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row_array();
}
return null;
}
}
66
67. Model (models/guestbook_model.php)
▪ 在 Guestbook_model 類別內撰寫 function add_message($data),向資料
庫新增留言資料。
<?php
class Guestbook_model extends CI_Model {
// some code here ......
// ......
public function add_message($data)
{
$this->db->insert('guestbook', $data);
$id = $this->db->insert_id();
return $id;
// 回傳 id
}
}
67
68. Model (models/guestbook_model.php)
▪ 在 Guestbook_model 類別內撰寫 function update_message($id, $data),
對資料庫更新留言資料。
<?php
class Guestbook_model extends CI_Model {
// some code here ......
// ......
public function update_message($id, $data)
{
$this->db->where('id', $id);
$this->db->update('guestbook', $data);
}
}
68
69. Model (models/guestbook_model.php)
▪ 在 Guestbook_model 類別內撰寫 function delede_message($id),對資
料庫刪除留言資料。
<?php
class Guestbook_model extends CI_Model {
// some code here ......
// ......
public function delete_message($id)
{
$this->db->where('id', $id);
$this->db->delete('guestbook');
}
}
69
82. What is Sparks
▪ Package Management System for Codeigniter
▪ Making Code Easy to Find, Create, and Distribute
▪ How to install?
$ php -r "$(curl -fsSL http://getsparks.org/go-sparks)"
82
83. How to use Sparks?
▪ Using Sparks
$ php tools/spark search redis
▪ menu - The menu library is used to ...
▪ redies - A CodeIgniter library to ...
$ php tools/spark install redis
▪ Retrieving spark detail from getsparks.org ...
▪ Sparks installed to ./sparks/redis/0.3.0 - You're on fire!
▪ Using Package
$this->load->spark('redis/0.3.0');
$this->this->set('foo', 'bar');
83