所需数据库表结构为:
CREATE TABLE IF NOT EXISTS `topics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(80) COLLATE utf8mb4_bin NOT NULL,
`content` varchar(100) COLLATE utf8mb4_bin NOT NULL,
`createtime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=12 ;
控制器Topics Controller:
topics.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Topics extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
function index(){
$this->db->order_by('id','desc');
$query = $this->db->get('topics',10);
if($query->num_rows() > 0){
$data['result'] = $query->result();
}else{
$data['result'] = '';
}
$this->load->view('/topics/index',$data);
}
function add(){
$this->load->view('topics/add');
if(trim($this->input->post('content'))!='' && trim($this->input->post('title'))!=''){
if($this->db->insert('topics',$_POST)){
redirect('topics/index','location',301);
}
}
}
function edit($id){
if($id != ''){
if(!$this->input->post('title')){
$query = $this->db->get_where('topics',array('id'=>$id));
$data['result'] = $query->result();
$this->load->view('topics/edit',$data);
}else{
$this->db->where('id',$id);
if($this->db->update('topics',$_POST)){
redirect('topics/index','location',301);
}
}
}
}
function del_topics($id){
if(!$id){
redirect('topics/index');
}else{
$this->db->where('id',$id);
if($this->db->delete('topics')){
redirect('topics/index');
}
}
}
function drop_table(){
$this->load->helper('url');
if($this->db->empty_table('topics')){
redirect('topics/index');
}
}
}
?>
以下为视图文件:(index,add,edit)
View page for Topics's index function:(view/topics/index.php)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Topics index page</title>
<style>
td{
text-align:center;
}
.title{
text-align:center;
font-weight:bold;
}
.a2{
margin-left:24px;
}
.topics{
font-weight:bold;
font-size:18px;
}
</style>
</head>
<body>
<p class='topics'>Topics's Index page:</p>
<?php
if($result==''){
echo '<table width=800 border=1><tr><td class="title">ID</td><td class="title">Title</td><td class="title">Content</td><td class="title">Create Time</td><td class="title">Action</td></tr></table>';
}else{
echo '<table width=800 border=1><tr><td class="title">ID</td><td class="title">Title</td><td class="title">Content</td><td class="title">Create Time</td><td class="title">Action</td></tr>';
foreach($result as $val){
echo '<tr><td>'.$val->id.'</td><td>'.$val->title.'</td><td>'.$val->content.'</td><td>'.$val->createtime.'</td><td><a href="edit/'.$val->id.'">Edit</a> <a href="del_topics/'.$val->id.'">Delete</a></td></tr>';
}
echo '</table>';
}
?>
<p>
<?php echo anchor('topics/add','Add Topics'); ?>
<?php echo anchor('topics/drop_table','Empty Topics',array('class'=>'a2')); ?>
</p>
</body>
</html>
View page for Topics's add function(view/topics/add.php):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Topics</title>
</head>
<body>
<h3>Topics:</h3>
<?php echo form_open('topics/add'); ?>
<p>Title: <input type='text' name='title' value='' /></p>
<p>Content: <input type='text' name='content' value='' /></p>
<input type='hidden' name='createtime' value="<?=date('Y-m-d H:i:s')?>" />
<p><input type='submit' value='Add Topics' /></p>
</form>
<p>
<?php echo anchor('topics/index',"Back to Topics's index page"); ?>
</p>
</body>
</html>
View page for Topics's edit function(view/topics/edit.php):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Topics</title>
</head>
<body>
<?php echo form_open('topics/edit/'.$this->uri->segment(3)); ?>
<p>Title: <input type='text' name='title' value="<?=$result[0]->title?>" /></p>
<p>Content: <input type='text' name='content' value="<?=$result[0]->content?>" /></p>
<input type='hidden' name='createtime' value="<?=date('Y-m-d H:i:s')?>" />
<p><input type='submit' value='Edit Topics' /></p>
</form>
<p><?php echo anchor('topics/index',"Back to topics's Index page");?></p>
</body>
</html>