CI学习笔记之增删改

所需数据库表结构为:

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>

转载于:https://my.oschina.net/adamboy/blog/41369

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值