CodeIgniter(1)简单入门---使用mvc来完成对新闻的一组操作----增删改查
来源:互联网 发布:北国知春物业 编辑:程序博客网 时间:2024/04/30 04:43
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>
完成!
- CodeIgniter(1)简单入门---使用mvc来完成对新闻的一组操作----增删改查
- 使用iBATIS3.0完成增删改查、 使用iBATIS3.0注解完成对数据库的简单操作
- 使用iBATIS3.0完成增删改查、 使用iBATIS3.0注解完成对数据库的简单操作。
- shell语句完成对文件操作的增删改查
- 使用Spring框架下的JdbcTemplate 完成对数据库的增删改查操作
- Hibernate中JPA的简单使用,实现简单的对表的增删改查操作
- MyBatis对数据库的增删改查操作,简单示例
- MyBatis对数据库的增删改查操作,简单示例
- CodeIgniter 数据库操作 -- 增删改查
- CodeIgniter中的增删改查操作
- CodeIgniter中的增删改查操作
- 使用MyBatis进行对数据表的增删改查操作
- mybatis+springMVC对新闻的增删改查操作(查询分页)
- 数据库---简单的使用Java操作数据库增删改查
- 使用nutz进行简单的增删查改操作
- Mybatis(入门)简单的增删改查
- SSM项目从零开始到入门007-使用接口完成增删改查操作
- mybatis-简单的增删改查操作
- 如何使用CSDN知识库
- js前端资源
- FUEL9编译指导
- STL源码——特殊的空间配置器
- linear-gradient 用法小结,不做渐变,做斜型背景。
- CodeIgniter(1)简单入门---使用mvc来完成对新闻的一组操作----增删改查
- i2cdetect i2cdump i2cget i2cset用法
- ES6 解析赋值
- C++ STL set集合容器常用用法
- javascript (function(){})()
- ssh三框架整合
- 安卓webwiew使用
- 服务器宕机记录16.12.14
- 自定义标签