第六节视频:6.CI框架学习-实例操做添加栏目如何定义使用模型以及配置数据库与利用AR增javascript
本节要点:php
一、定义模型Modelhtml
二、输入类使用java
三、数据库配置mysql
四、使用AR类操做数据库sql
五、激活调试模式数据库
在application/controllers/admin/category.php栏目管理控制器中:数组
// 添加动做 public function add(){ //载入表单验证类库 $this ->load ->library('form_validation'); $status = $this ->form_validation ->run('cate'); if($status){ // echo '数据库操做'; //首先定义一个模型,也就是说验证成功,操做模型,而后模型操做数据库; }else{ $this ->load ->helper('form'); $this ->load ->view('admin/add_cate.html'); } }
在application/models/下新建一个模型:category_model.php(注意:定义文件名时必定要加上:_model.php):安全
操做数据库:app
一、建立数据库:
create database article charset utf8;
use article;
create table hd_category(cid int unsigned primary key auto_increment, //cid做为主键
cname varchar(15) not null default '' //cname做为栏目名称,默认为空;
);
show tables; //查看表
desc hd_category; //查看表的详细信息
二、操做表以前,须要配置数据库:
在application/config/database.php下配置:
$active_group = 'default';//项目测试时使用test,向里面填充一些数据; $active_record = TRUE;//为TRUE时,表明其继承AR模型,(AR模型:system/database/DB_active_rec.php) $db['default']['hostname'] = '127.0.0.1'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'article'; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = 'hd_'; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE;
三、在application/config/autoload.php中设置自动加载数据库;
$autoload['libraries'] = array('database');
--------------------------------------------------------------------------------------------------
一、在application/models/category_model.php中添加数据库:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/10/8 * Time: 16:40 */ if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //栏目管理模型 class Category_model extends CI_Model{ //添加 public function add($data){ // echo 'hello'; $this ->db->insert('category',$data);//第一个参数为表名,第二个参数为传递的数组; } }
在application/controllers/admin/category.php中:
// 添加动做 public function add(){ //载入表单验证类库 $this ->load ->library('form_validation'); $status = $this ->form_validation ->run('cate'); if($status){ // echo '数据库操做'; //首先定义一个模型,也就是说验证成功,操做模型,而后模型操做数据库; $data = array( 'cname' => $_POST['cname'] //cname为字段名,数组的键名也是数据库的字段名; ); //载入模型 $this ->load ->model('category_model','cate');//第二个参数为第一个参数的别名; //执行模型里面的方法 //$this ->category_model ->add(); $this ->cate ->add($data); }else{ $this ->load ->helper('form'); $this ->load ->view('admin/add_cate.html'); } }
查看数据库表里插入的数据:select * from hd_category;
===============================================
输入类:
一、为了安全,预处理输入数据;
二、提供helper的一些方法,取得输入数据,并预处理输入数据;
三、防止跨站:$config['global_xss_filtering'] = TRUE;
在system/core/Common.php定义全局函数,会被全局加载:
//下面是自定义函数 /* 格式化打印函数*/ function p($arr){ echo '<pre>'; print_r($arr); echo '</pre>'; } /*成功提示函数*/ function success($url,$msg){ //$url表示跳转地址,$msg表示跳转提示信息 header('Content-Type:text/html;charset = utf-8');//防止出现乱码 $url = site_url($url); echo "<script type='text/javascript'>alert('$msg');location.href = '$url'</script>"; die; } /*错误提示信息*/ function error($msg){ header('Content-Type:text/html;charset = utf-8'); echo "<script type='text/javascript'>alert('$msg');window.history.back();</script>"; }
附:application/contronllers/admin/category.php:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/10/8 * Time: 12:03 */ if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Category extends CI_Controller{ //查看栏目 public function index(){ echo '查看栏目'; } //添加栏目 public function add_cate(){ $this ->load ->helper('form'); $this ->load ->view('admin/add_cate.html'); } // 添加动做 public function add(){ //载入表单验证类库 $this ->load ->library('form_validation'); $status = $this ->form_validation ->run('cate'); if($status){ // echo '数据库操做'; //首先定义一个模型,也就是说验证成功,操做模型,而后模型操做数据库; //var_dump($this->input ->post('abc'));die; // $this->input->get('name'); // $a = $this ->input->server('HTTP_HOST'); // echo $a; // p($_SERVER);die; //接收数据预处理,更安全; $data = array( 'cname'=>$this->input->post('cname') ); // $data = array( // 'cname' => $_POST['cname'] //cname为字段名,数组的键名也是数据库的字段名; // ); //载入模型 $this ->load ->model('category_model','cate');//第二个参数为第一个参数的别名; //执行模型里面的方法 //$this ->category_model ->add(); $this ->cate ->add($data); //成功提示信息 success('/admin/category/index','添加成功'); }else{ $this ->load ->helper('form'); $this ->load ->view('admin/add_cate.html'); } } //编辑栏目 public function editor_cate(){ $this ->load ->helper('form'); $this ->load ->view('/admin/editor_cate.html'); } //编辑栏目动做 public function editor(){ //载入表单验证类库 $this ->load ->library('form_validation'); $status = $this ->form_validation ->run('cate'); if($status){ echo '数据库操做'; }else{ $this ->load ->helper('form'); $this ->load ->view('admin/editor_cate.html'); } } }
附:application/models/category_madel.php:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/10/8 * Time: 16:40 */ if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //栏目管理模型 class Category_model extends CI_Model{ //添加 public function add($data){ // echo 'hello'; $this ->db->insert('category',$data);//第一个参数为表名,第二个参数为传递的数组; } }
激活调试模式:
$this ->output ->enable_profiler(TRUE);
会在模板尾部显示
将$this ->output ->enable_profiler(TRUE);写入application/controllers/admin/category;
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/10/8 * Time: 12:03 */ if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Category extends CI_Controller{ //查看栏目 public function index(){ echo '查看栏目'; } //添加栏目 public function add_cate(){ $this ->output ->enable_profiler(TRUE);//激活调试模式,需注意要载入模板,在模板的底部显示; $this ->load ->helper('form'); $this ->load ->view('admin/add_cate.html'); } // 添加动做 public function add(){ //载入表单验证类库 $this ->load ->library('form_validation'); $status = $this ->form_validation ->run('cate'); if($status){ // echo '数据库操做'; //首先定义一个模型,也就是说验证成功,操做模型,而后模型操做数据库; //var_dump($this->input ->post('abc'));die; // $this->input->get('name'); // $a = $this ->input->server('HTTP_HOST'); // echo $a; // p($_SERVER);die; //接收数据预处理,更安全; $data = array( 'cname'=>$this->input->post('cname') ); // $data = array( // 'cname' => $_POST['cname'] //cname为字段名,数组的键名也是数据库的字段名; // ); //载入模型 $this ->load ->model('category_model','cate');//第二个参数为第一个参数的别名; //执行模型里面的方法 //$this ->category_model ->add(); $this ->cate ->add($data); //成功提示信息 success('/admin/category/index','添加成功'); }else{ $this ->load ->helper('form'); $this ->load ->view('admin/add_cate.html'); } } //编辑栏目 public function editor_cate(){ $this ->load ->helper('form'); $this ->load ->view('/admin/editor_cate.html'); } //编辑栏目动做 public function editor(){ //载入表单验证类库 $this ->load ->library('form_validation'); $status = $this ->form_validation ->run('cate'); if($status){ echo '数据库操做'; }else{ $this ->load ->helper('form'); $this ->load ->view('admin/editor_cate.html'); } } }