CI学习笔记之增删改

原创
2012/02/22 20:18
阅读数 488

所需数据库表结构为:

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>&nbsp;<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:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='title' value='' /></p>
<p>Content:&nbsp;<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:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' name='title' value="<?=$result[0]->title?>" /></p>
<p>Content:&nbsp;<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>

展开阅读全文
CI
打赏
0
3 收藏
分享
加载中
Adam-Lee博主
哈哈,谢谢你的建议哈,唉,到一家新公司就得学一个新的框架,谢谢指教……
2012/02/23 09:05
回复
举报
没必要这样,直接new一个modle,在save,add就好了啊
2012/02/22 22:16
回复
举报
更多评论
打赏
2 评论
3 收藏
0
分享
返回顶部
顶部