通常我的習慣是會先規劃資料庫需要哪些欄位,之後把會用到的新增、修改、刪除、列表、取單一值的功能都先寫好,最後才會處理頁面,所以接下來我要寫一個簡單的範例會是用這樣的順序。
Step 1 : 先建立一個叫news的資料表,裡面有id、title、content三個欄位,id為主鍵。
CREATE TABLE `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
)
Step 2 :新增模型(Model)檔案名稱為news_model.php存放在application/models資料夾中。
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();//每次都會先load資料庫
}
//新增
public function create($title, $content)
{
//設值
$this->db->set('title', $title);
$this->db->set('content', $content);
$this->db->set('create_time', date('Y-m-d H:i:s'));
//存入資料庫
$this->db->insert('news');
// 產生: INSERT INTO news (title, content, create_time) VALUES ('$title', '$content', '2013-04-05 12:00:00')
}
//修改
public function edit($id, $title, $content)
{
//設值
$this->db->set('title', $title);
$this->db->set('content', $content);
//指定要更新的id
$this->db->where('id', $id);
//存入資料庫
$this->db->update('news');
//產生: UPDATE news SET title = '$title', content = '$content' WHERE id = $id
}
//刪除
public function delete($id){
$this->db->delete('news', array('id' => $id));
//產生: DELETE FROM news WHERE id = $id
}
//列表
public function all(){
//指定資料表
$query = $this->db->get('news');
return $query->result_array();//把找到的結果轉成array
}
//取單一值
public function one($id){
//指定條件,資料表
$query = $this->db->get_where('news',array('id'=>$id));
return $query->row_array();//把找到的結果轉成array
}
}
?>
Step 3 : 新增檢視(View)檔案名稱為index.php及form.php 存放在application/views資料夾中。
index.php列表
<?php foreach($news as $row){?>
<div class="news-cont-box">
<h3><?= substr($row['create_time'],0,10)?></h3>
<h2><?= $row['title']?></h2>
<p><?= $row['content']?></p>
</div>
<?php }?>
form.php主要用來做新增跟修改的
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script language="javascript">
$('document').ready(function(){
$('#save').click(function(){
if($("#title").val() == ""){
msg = msg+"標題說明不能為空\n";
}
if($("#content").val() == ""){
msg = msg+"內容不能為空\n";
}
if(msg == ""){
$.post('<?= site_url('news/save')?>',$("#contentForm").serialize(), function(result){
if (result == 'success'){
window.location = "<?= site_url('news')?>";
}else{
alert(result);
}
},
'text'
);
}else{
alert(msg);
}
});
});
</script>
<form id="contentForm">
<table border="0" width="100%" cellspacing="0" cellpadding="5" >
<tbody>
<tr>
<td>標題</td>
<td><input type="text" name="title" id="title" value="<?= $news['title']?>"/></td>
</tr>
<tr>
<td>內文</td>
<td><textarea id="content" name="content" cols="60" rows="5"><?= $news['content']?></textarea></td>
</tr>
</tbody>
</table>
<input type="hidden" name="id" id="id" value="<?= $news ['id']?>"/>
<input type="button" id="save" name="save" value="儲存" class="button" />
</form>
Step 4 : 控制器(Controller)檔案名稱為news.php 存放在application/controllers資料夾中。
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->all();
$this->load->view('index',$data);
}
public function form($id=null)
{
$data['news'] = $this->news_model->one($id);
$this->load->view('form',$data);
}
public function save(){
try{
$title = $this->input->post('title');
$content = $this->input->post('content');
$id = $this->input->post('id');
if($id == ""){
$this->news_model->create($title, $content);
}else{
$this->news_model->edit($id, $title, $content);
}
echo 'success';
}catch(Exception $e){
echo $e;
}
}
public function delete(){
try{
$this->news_model->delete($this->input->post('id'));
}catch(Exception $e){
echo 'error';
}
echo 'success';
}
}
?>