第一天ci框架开发商城2
ci框架开发商城2
1/28/2016 9:45:52 PM
mvc完整案例 mvc完成新闻的增删改查
-
news控制器news.php
class News extends CI_controller{
public __construct(){
parent::__construct();
//载入news_model
$this->load->model('news_model');}
public function add(){
$this->load_view('add.html');
}
} -
设计一张表news.sql
create database ci;
use ci
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; -
连接数据库
-
数据库相关配置 config/database.php
-
连接数据库 数据库操作类两种方式只需要加载该类即可,手动载入 或者自动载入
$this->load->database
//需要的地方手动载入
自动载入config/autoload.php
$autoload['libraries'] =array('database');加入database -
操作数据库,
-
定义news模型 继承模型基类CI_Model Model_name 类名首字母大写其他字母小写 文件名 类名的小写版News_model news_model.php
class News_model extends CI_Model{
//构造函数
public function __construct(){
//调用父类构造函数 必不可少
parent::__construct();
//手动载入数据库操作类
$this->load->database(); }
} -
操作数据库 $this->db->query($sql);//select返回结果集 insert update delete返回布尔值更希望有一个能够自动完成增删改查的一个功能CI中提供了一个active record类 $this->db->insert/update/delete/get 自动能够拼凑sql语句 支持多种数据库
-
news_model.php 写完后控制器中调用模型
const TBL='news';
public function add_news($data){
return $this->db- >insert(self::TBL,$data);//ar类完成操作
}
/**
*@param $data array
@return bool 成功返回 true 失败返回FALSE
*/ -
控制器 news.php 在构造函数中载入news_model,并写入insert方法
class News extends CI_controller
{
public __construct()
{
parent::__construct();
//载入news_model
$this->load->model('news_model');
}
public function add(){
$this->load_view('add.html');
}
public function insert()
{
//调用news_model
if($this->news_model->add_news()
{echo "插入成功";
}else
{echo "插入失败";}
}
} -
视图文件中表单action怎么写?
涉及到ci的辅助函数url
配置config.php中 $config['base_url']="http://localhost/citest"
默认情况下 url函数没有加载
可在配置文件中自动加载 config/autoload.php $autoload['helper']=array('url');
提供了 site_url= base_url+index.php
action=""
//========控制器news.php
class News extends CI_controller
{
public __construct()
{
parent::__construct();
//载入news_model
$this->load->model('news_model');
}
public function add(){
$this->load_view('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 "插入成功";
}else
{echo "插入失败";}
}
} -
new.php中显示新闻列表 news_model加入list _news方法
//======news_model
class News_model extends CI_Model{
//构造函数
public function __construct()
{
//调用父类构造函数 必不可少
parent::__construct();
//手动载入数据库操作类
$this->load->database();
}
/**
@param $data array
@return bool 成功返回 true 失败返回FALSE
/
const TBL='news';
public function add_news($data)
{
return $this->db->insert(self::TBL,$data);//AR类完成操作
}
/
*@acess public
*@return array
*/
public function list_news()
{
$query = $this->db->get(self::TBl,);
return $query->result_array();
}
}//========控制器news.php
class News extends CI_controller
{
public __construct()
{
parent::__construct();
//载入news_model
$this->load->model('news_model');
}
public function add(){
$this->load_view('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 "插入成功";
}else
{echo "插入失败";}
}
public function index()
{//调用list_news方法
$data['news'] =$this->news_model->list_news();
//在视图文件中展示
$this->load->view('list.html',$data);
}}
-
list.html视图文件
-
-

浙公网安备 33010602011771号