一个简单的例子

我们通过一个例子来说明使用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);

}