CodeIgniter 应用开发笔记 - 2

本文介绍从旧版CodeIgniter迁移到新版的过程,包括基类名称变更、构造函数调整及XSS过滤器设置等关键步骤,并提供了一个具体的数据库操作实例。

一个简单的例子

我们通过一个例子来说明使用CI是多么简单的事情!

我们首先下载一个IBM开发者网站上的一个例子来做移植。

下载地址:http://www.ibm.com/developerworks/web/library/wa-codeigniter/

 

我们开始吧!

基类

 

在使用老版本的CI的时候,我们要变更一下基类的名称。

  

序号

老版本(V1.6.2)

新版本(V2.1.3)

备注

1

Controller

CI_Controller

 

2

Model

CI_Model

 

 

    在新版本中已经更改了默认的构造器。

比如,老版本中在每个继承类的第一段都有:

 

       function 类名(){

              parent::Model();

       }

   或

function 类名(){

              parent::Controller();

       }

 

 

    新版本都由两个下划线和construct为构造器名

function __construct(){

              parent::__construct();

       }

 

 

 

 

 

 

 

 

 

XSS过滤器

在config目录下的config.php中:

$config['global_xss_filtering'] = FALSE;

更改为

$config['global_xss_filtering'] = TRUE;

 

 

函数更改:

把“input”变更为“security”

$this->input->xss_clean

成为

$this->security->xss_clean

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

我们的“仓库”

 

首先,填写位于config文件夹的database.php中的用户名、密码、数据库名等

 

$db['default']['username']= 'root';

$db['default']['password']= 'qazxsw';

$db['default']['database']= 'carnumber';

 

然后,创建数据表

 

CREATE TABLEcontacts (

  id int NOT NULL AUTO_INCREMENT,

  name varchar(128) NOT NULL,

  email varchar(255) NOT NULL,

  notes text NOT NULL,

  stamp timestamp NOT NULL defaultCURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

  ipaddress varchar(32) NOT NULL,

  PRIMARY KEY (id))AUTO_INCREMENT=100001;

 

最后,我们在model中编写代码即可:

 

functionaddContact(){

      $now= date("Y-m-d H:i:s");

       $data = array(

              'name' =>$this->security->xss_clean($this->input->post('name')),

              'email' =>$this->security->xss_clean($this->input->post('email')),

              'notes' =>$this->security->xss_clean($this->input->post('notes')),

              'ipaddress' =>$this->input->ip_address(),

              'stamp' => $now

      

       );

 

       $this->db->insert('contacts',$data);

 }

 

 实现的效果,如下图:


 

 

转载于:https://www.cnblogs.com/jiangu66/archive/2013/04/07/3003774.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值