1.下載CodeIgniter框架
進入官網http://codeigniter.org.cn/,下載相應的CodeIgniter代碼,如圖1.
圖2
在數據庫test中創建數據表news
- create database test;
- use test;
- create table news(
- id int unsigned not null primary key auto_increment,
- title varchar(50) not null default '',
- author varchar(30) not null default '',
- content text,
- add_time int unsigned not null default 0
- )engine=myisam charset=utf8;
3.數據庫配置,如圖3.
圖3
4.編寫代碼--模型
文件位置application/models/News_model.php
defined('BASEPATH') OR exit('No direct script access allowed');
class News_model extends CI_Model{
const TBL='news';
//構造函數
public function __construct(){
//調用父類構造函數,必不可少
parent::__construct();
//手動載入數據庫操作類
$this->load->database();
}
/*
@access public
@param $data array
@return bool 成功返回true,失敗返回false
*/
public function add_news($data){
//使用AR類完成插入操作
return $this->db->insert(self::TBL,$data);
}
/*
@access public
@return array 查詢結果
*/
public function list_news(){
$query=$this->db->get(self::TBL);
return $query->result_array();
}
/*
@access public
@return int 成功返回受影響的條數
*/
public function delete_news($id){
return $this->db->delete(self::TBL,array('id'=>$id));
}
/*
@access public
@return array修改查詢的結果
*/
public function get_news($id){
$query=$this->db->get_where("news",array('id'=>$id));
return $query->result_array();
}
/*
@access public
@return bool 修改成功返回true
*/
public function update_news($id,$data){
return $this->db->update('news',$data,array('id'=>$id));
}
}
5.編寫代碼--控制器
文件位置application/controllers/news.php
defined('BASEPATH') OR exit("No direct script access allowed");
class News extends CI_Controller{
public function __construct(){
parent::__construct();
#載入news——Model,載入之後可以使用$this->user_model來操作
$this->load->model("news_model");
}
//顯示添加新聞表單
public function add(){
$this->load->view("news/add.html");
}
//完成新聞的添加
public function insert(){
#獲取表單提交的數據
$data['title']=$_POST['title'];
$data['author']=$_POST['author'];
$data['content']=$_POST['content'];
$data['add_time']=time();
#調用news_model的方法
if($this->news_model->add_news($data)){
echo "添加成功<a href='".site_url('news')."'>返回首頁</a>";
}else{
echo "添加失敗<a href='".site_url('news')."'>返回首頁</a>";
}
}
// 顯示新聞列表
public function index(){
#調用list_news方法得到數據
$data['news']=$this->news_model->list_news();
#分配到視圖
$this->load->view("news/list.html",$data);
}
//刪除新聞
public function delete($id){
if($this->news_model->delete_news($id)){
echo "刪除成功<a href='".site_url('news')."'>返回首頁</a>";
}else{
echo "刪除失敗<a href='".site_url('news')."'>返回首頁</a>";
}
}
//修改新聞,顯示新聞詳情頁
public function edit($id){
$data['new']=$this->news_model->get_news($id);
$data['new']=$data['new'][0];
$this->load->view("news/edit.html",$data);
}
//完成新聞的修改
public function update(){
#獲取表單提交的數據
$id=$_POST['id'];
$data['title']=$_POST['title'];
$data['author']=$_POST['author'];
$data['content']=$_POST['content'];
#調用news_model的方法
if($this->news_model->update_news($id,$data)){
echo "修改成功<a href='".site_url('news')."'>返回首頁</a>";
}else{
echo "修改失敗<a href='".site_url('news')."'>返回首頁</a>";
}
}
}
6.編寫代碼---視圖
文件位置application/view/news
(1)add.html
<!DOCTYPE html>
<html>
<head>
<title>添加新聞</title>
</head>
<body>
<div>
<form action="<?php echo site_url('news/insert');?>" method="post">
<fieldset>
<legend>添加新聞</legend>
<ul>
<li for=""><label>標題</label><input type="text" name="title"></li>
<li for=""><label>作者</label><input type="text" name="author"></li>
<li for=""><label>正文</label><textarea name="content" id="" cols="100"></textarea></li>
<li for=""><label> </label><input type="submit" name="btn" value="添加"></li>
<input type="hidden" name="act" value="add">
</ul>
</fieldset>
</form>
</div>
</body>
</html>
(2)list.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h3>新聞列表</h3><a href="<?php echo site_url('news/add')?>">添加新聞</a>
<table width="700" border="1">
<tr>
<th>編號</th>
<th>標題</th>
<th>作者</th>
<th>添加時間</th>
<th>操作</th>
</tr>
<?php foreach($news as $row): ?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['author'] ?></td>
<td><?php echo $row['add_time'] ?></td>
<td><a href="<?php $url='news/edit/'.$row['id'];echo site_url($url); ?>">編輯</a>
<a href="<?php $url='news/delete/'.$row['id'];echo site_url($url); ?>">刪除</a></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
(3)edit.html
<!DOCTYPE html>
<html>
<head>
<title>修改新聞</title>
</head>
<body>
<div>
<form action="<?php echo site_url('news/update');?>" method="post">
<fieldset>
<legend>修改新聞</legend>
<ul>
<li for=""><label>標題</label><input type="text" name="title" value="<?php echo $new['title']?>"></li>
<li for=""><label>作者</label><input type="text" name="author" value="<?php echo $new['author']?>"></li>
<li for=""><label>正文</label><textarea name="content" id="" cols="100"><?php echo $new['content']?></textarea></li>
<li for=""><label> </label><input type="submit" name="btn" value="修改"></li>
<input type="hidden" name="id" value="<?php echo $new['id']?>">
</ul>
</fieldset>
</form>
</div>
</body>
</html>
完成!