php CodeIgniter 学习笔记

来源:互联网 发布:淘宝pc链接转无线链接 编辑:程序博客网 时间:2024/05/17 10:53

一、CI是什么?
CI的全称是CodeIgniter, 一个小巧但功能强大的 PHP 框架,作为一个简单而“优雅”的工具包,它可以为 PHP 程序员建立功能完善的 Web 应用程序。基于MVC设计模式,轻量易学,注重安全,文档健全,有丰富的类库。


二、CI能做什么?
CI提供了许多丰富的类库,比如文件上传、图片处理、文件压缩、邮件发送等。这些类库的存在大大减低了开发的难度与周期,也减少了整合外部类库的需求。能快速高效的开发各种web应用: 比如电子商务网站、SNS、论坛、内容管理系统(CMS)、客户关系管理系统(CRM)等等。


三、视频内容
01.CI简介与MVC设计模式
02.CI中控制器controller
user extends CI_Controller{
}
03.CI中的视图view
$this->load->view('user/index');
$this->load->var('name','cq');
04.CI超级对象中的load装载器
当前控制器对象提供了很多属性:
$this->load 
装载器类的实例system/core/Loader.php
装载器类提供方法:
view() 装载视图
vars()   分配变量到视图
database() 装载数据库操作对象
model() 装载模型对象
helper()  
05.CI超级对象中的uri
不使用$_GET 
使用$this->uri->segment(n)获取url中的第n个值
segment()用于获取url中的参数
入口文件.php/控制器/动作/参数1/参数2
06.CI超级对象中的input输入类
$this->input是CI_Input类的实例 system/core/Input.php
CI_Input类提供方法:
$this->input->post('user_name');  //$_post['user_name']
$this->input->server('DOCUMENT_ROOT');//$_SERVER['DOCUMENT_ROOT'] 
在视图中,直接用$this来访问超级对象中的属性
07.CI中的数据库操作(1)
装载数据库操作类
$this->load->database()
$this->db //装载成功后,会放入超级对象的属性中,默认属性名是db
$res = $this->db->query($sql)//mysql_query()
$res 返回值是一个对象
$users = $res->result(); 
数据库访问
修改配置文件
application/config/database.php


讲数据库访问对象,装载到超级对象的属性 $this->db
$this->load->database();  


$res = $this->db->query($sql) 返回对象
   $res->result()//返回数组,数组中是一个一个的对象
$res->result_array();//返回二维数组,里面是关联数组
$res->row()//返回第一条数据,直接是一个对象
08.CI中的数据库操作(2)
参数绑定
$sql = "select * from blog_user where name=?";
$this->db->query($sql,$name)//如果有多个问号时,传入一个索引数组


表前缀
$db['default']['dbprefx] = 'blog_';
$db['default']['swap_pre'] = 'blog_';
配置为一样,代码中,直接硬编码表前缀就行了,如果以后项目数据库表前缀发生变化
只需要修改$db['default']['dbprefix'] = 'new_';代码中的blog_会自动替换为new_

db的自动加载
//配置文件autoload.php设置$autoload['libraries'] = array('database')
//就不需要每次都写$this->load->database();  ,配置自动加载db
09.CI中的AR(数据库增、删、改、查)
自增id
$this->db->insert_id();
受影响行数
$this->db->affected_rows();
Active Record
1.application/config/database.php
$active_record = TRUE;
2.application/config/autoload.php
在配置文件中,配置表前缀后,会自动添加
       $res = $this->db->get('表名') //返回结果集对象
$bool = $this->db->insert('表名',关联数组)
$bool = $this->db->update('表名',关联数组,条件)
$bool = $this->db->delete('表名',条件)
10.CI中的AR(连惯操作)
$res = $this->db->select('id,name')
->from('user')
->where('id >=',3)
->limit(3,2)//跳过2条,取出三条数据
->order_by('id desc ')
->get();
var_dump($res->result);
//显示最近一条sql
echo $this->db->last_query();

where
$res = $this->db->where('name','mary')->get('user');
$res = $this->db->where('name !=','mary')->get('user');
$res = $this->db->where(array('name'=>'mary'))->get('user');
$res = $this->db->where(array('name'=>'mary','id >'=>2))->get('user');


复制查询,请用$this->db->query($sql,$data),//使用问号绑定参数
11.扩展CI的控制器
application/cpre/MY_Controller.php
控制器就要以继承自MY_Controller


application/config/config.php
$config['syvckass_prefix'] = 'MY_';
12.CI中的模型model
 命名文件名 全小写  类名首字母大写 User_model
控制器调用:$this->load->model('User_model'); //加载模型
$list = $this->User_model->getAll();//getAll是User_model中自定义的方法
起别名:
$this->load->model('User_model','user');
$list = $this->user->getAll()
模型
继承自CI_Model
在模型中,可以直接使用超级对象中的属性
文件名,全小写
类名首字母大写
建议使用_model作为后缀,防止和控制器类名冲突
13.CI中的url相关函数
$this->load->helper('url') //可以根据需要配置自动加载
<form action=<?php echo site_url('user/insert')?>>
<img src="<?php echo base_url();?>/uploads/xxx.jpg">
14.CI中的路由与伪静态、隐藏index.php入口文件
路由
routes.php
$route['default_controller'] = 'welcome';//默认控制器
$route['404_override'] = '';
$route['show/([\d]+)\.html'] = 'article/show/$1';//自定义 路由
15.CI中的分页
   装载类文件
   $this->load->library('pagination');
   $this->load->helper('url);
   //每夜显示10条数据
   $page_size= 10;

$config['base_url'] = site_url('user/test);
// 一共有多少条数据
   $config['total_rows'] = 100;
   //每页显示条数
   $config['per_page'] = $page_size;
   $config['first_link'] = '首页';
   $config['next_link'] = '下一页';
   $config['url_segment'] = 3 //分页的数据查询的偏移量在哪一段上

$this->pagination->initialize($config);
$offset = intval($this->uri->segment(3));//对应$config['url_segment']
$sql = "select * from blog_user limit $offset,$page_size";


$this->pagination->create_links();
16.CI中的文件上传
手动创建好上传目录
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|png|jpg|jpeg';
$config['file_name'] = uniqid();//生成新文件名
$this->load->library('upload',$config);
$this->upload->do_upload('pic');


$data = $this->upload->data();
echo $data['file_name'];
17.CI中的Session
ci 接触cookie机制 实现session
//生成一个随机不重复的字符串作为加密用的key
//保存到application/config/config.php 
//$config['encryption_key']
$this->load->library('session');
$user= array('id'=>3,'name'=>'jack');
$this->session->set_userdata('user',$user);
//不在这里获得刚放入的数据
//只有页面在重新加载或跳转到别的url中,才能获取到
$this->load->library('session');
//取CI session中的数据
$this->session->userdata('user');


//生成一次使用的数据
$this->session->set_flashdate('test','aaaaaaaa');
//使用一次数据就没有了,只能读取一次
$this->session->flashdate('test');
18.CI中的验证码
$this->load->helper('url');
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',//此目录需要手动创建
''img_url' => base_url().'/captcha',
//'img_width' => '150',
//'img_height' => 30,
//'expiration' => 7200  //ci会自动生成验证码图片,超过这个时间会删除图片
);
$cap = create_captcha($vals);

session_start();
$_SEESION['cap'] = $cap['word'];
验证时,对$_SESSION['cap'];
19.CI中的表单验证
$this->load->library('form_validation');


$this->form_validation->set_rules('name','用户名','required');


$bool = $this->form_validation->run();//自动验证,成功返回true


if($bool){
//调用模型保持到数据库
}else{
//显示错误信息
$this->load->view('user/index');

}


<input type="text" name="name" value="<?php echo set_value('name');?>" />
<?php echo form_error('name','<span>','</span>') ?>
//错误信息语言包改成中文  $config['language'] = 'zh_cn';
<?php echo validation_errors();//打印所有错误?>
0 0