设置时区
入口文件 index.php
date_default_timezone_set(‘Asia/Shanghai’);
配置默认控制器
config =>routes.php default_control
载入辅助函数
$this->load->helper();
url辅助
site_url函数:http://localhost/index.php 主要用于访问控制器,方法用于跳转进行调整
base_url函数:网站根目录 http://localhost/
redirect('控制器名/方法名') 跳转
辅助函数经常使用可设置自动加载
config =》autoload.php =>$autoload['helper'] = array();
扩展自定义函数
system Common.php(全局加载,自动载入)
加载模型
$this->load->model(‘模型名’,’别名’);
$this->别名->模型中的方法();
CI的表单验证
http://codeigniter.org.cn/userguide2/libraries/form_validation.html#theform
先要加载表单验证类
$this->load->library(“form_validation”);
辅助函数加载
$this->load->helper(‘form);
添加共用验证
在config文件中添加form_validation.php文件
可在控制器中进行验证函数中填写数组名字
$this->form_validation->run(‘数据名字’);
在页面中显示错误和保存错误前的数据
开启调试模式
$this->output->enable_profiler(TRUE);任意位置
AR类的操作
增:$ths->db->insert(‘表名’,$data);
改:$this->db->update(‘表名’,$data,array(‘’=>)); // array(‘’=>) 为条件
删:$this->db->delete(‘表名’,array(‘’=>));
查:
$this->db->get(‘表名’)->result_array();查询所有的数据返回数组
result_object()返回对象
$query = $this->db->query("SELECT * FROM users;");
$query->result_array();查询所有的数据返回数组
$this->db->where(array(‘字段名’=>值))->get(‘表名’)->result_array();
$query->row();这个方法返回单独一行结果。如果你的查询不止一行结果,它只返回第一行。返回的结果是 对象
如果你要返回特定行的数据,你可以将行号作为第一个参数传给这个方法:
$row = $query->row(5);
row_array() 方法
这个方法除了返回结果是一个数组而不是一个对象之外,其他的和上面的 row() 方法完全一样
*如果你要返回特定行的数据,你可以将行号作为第一个参数传给这个方法
结果集中获取前一个、后一个、 第一个或者最后一个结果:
$row = $query->first_row()
$row = $query->last_row()
$row = $query->next_row()
$row = $query->previous_row()
这些方法默认返回对象,如果需要返回数组形式,将单词 "array" 作为参数传入方法即可:
$row = $query->first_row('array')
$row = $query->last_row('array')
$row = $query->next_row('array')
unbuffered_row() 方法
这个方法和 row() 方法一样返回单独一行结果,但是它不会预读取所有的结果数据到内存中。 如果你的查询结果不止一行,它将返回当前一行,并通过内部实现的指针来移动到下一行。
$query = $this->db->query("YOUR QUERY");
while ($row = $query->unbuffered_row()){
echo $row->title;
echo $row->name;
echo $row->body;}
为了指定返回值的类型,可以传一个字符串参数 'object'(默认值) 或者 'array' 给这个方法:
$query->unbuffered_row(); // object$query->unbuffered_row('object'); // object$query->unbuffered_row('array'
num_rows() 方法
该方法返回查询结果的行数。注意:在这个例子中,$query 变量为查询结果对象:
$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();
Uri
$this->uri->segment(要取得片段数字);
图片上传处理
http://codeigniter.org.cn/user_guide/libraries/file_uploading.html
$config['upload_path'] = './upload/';//保存的路径
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = ‘10000’;//最大大小
$config['file_name'] = time().rand(9999,10000);//文件名字
$config['max_width'] = ‘’;//最大宽度
$config['max_height'] =’’ ;//最大高度
//载入上传类
$this->load->library('upload', $config);
//进行上传处理
$this->upload->do_upload(‘要上传的字段名’);//返回布尔值
$this->upload->display_errors();//返回布尔值 为真上传错误
$this->upload->data();//返回信息
缩略图
http://codeigniter.org.cn/user_guide/libraries/image_lib.html
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';//图片原路径
$config['create_thumb'] = TRUE;//创建预览图像 可改为FALSE
$config['maintain_ratio'] = TRUE;//是否保存横纵比例
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();//返回布尔值
分页
http://codeigniter.org.cn/user_guide/libraries/pagination.html
$this->load->library('pagination');
$config['base_url'] = site_url();
$config['total_rows'] = 200;//总行数
$config['per_page'] = 20;//每页显示数量
$config['uri_segment'] = 4;//默认第3个片段
$this->pagination->initialize($config);
$this->pagination->create_links();//返回一个连接,输出在视图上
$offist=$this->uri->segment(4);//开始的位置
$this->db->limit(‘每页显示的数量’,$offist);
验证码辅助函数
$this->load->helper('captcha');//加载辅助函数
$vals = array(
'word' => 'Random word',
'img_path' => './captcha/',
'img_url' => 'http://example.com/captcha/',
'font_path' => './path/to/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200,//图片过期时间
'word_length' => 8,
'font_size' => 16,
'img_id' => 'Imageid',
'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
// White background and border, black text and red grid
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(0, 0, 0),
'grid' => array(255, 40, 40)
));
$cap = create_captcha($vals);
$data[‘captcha_image’] = $cap['image'];//传递到页面输出
加入自己的类库
Application/library/文件名首字母大写
在控制器中加载类库:$this->load->library(‘类名’);
SESSION类
在config配置session为自动加载
$this->session->set_userdata($data);
取数据
$this->session->userdata();//取出全部,可用数组下标去具体的值
判断用户是否登录 在自定义applaction/core 加自己的文件 在构造函数中判断,控制器中都继承它
载入公共视图
把要独立出来的公共文件,在模板上用
<?php $this->load->view(“公共页面”); ?>
控制器中把公共 部分的数据处理
路由控制简洁处理
定义可以用两种方式: 通配符(wildcards) 或者 正则表达式(Regular Expressions)
当"product"作为URL中第一个分段时, 无论第二分段是什么都将被重定向到"catalog"类的"product_lookup"方法.
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
正则表达式
如果你喜欢可以使用正则表达式来自定义你的路由规则. 任何有效的正则表达式都是被允许的, 甚至逆向引用.
$route['products/([a-z]+)/(\d+)'] = "$1/id_$2";
隐藏单一入口index.php
条件必须保证服务器开启重写模块:
Apache httpd.conf 中打开mod_rewite.so
权限设置
AllowOverride all
对应目录一定是对应目录不然报错403
例如在根目录 htdocs在的test目录则 要加
<Directory "E:/server/apache/htdocs/test">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
在程序根目录建立 .htaccess 文件
文件内容:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|statics|images|js|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
将.htaccess文件上传到CI所在项目的根目录(即与index.php同级目录下) 修改application/config.php中的如下参数如下:
$config['index_page'] = "index.php";
将其index.php改为空
伪静态
在applaction conf.php 中 值为文件格式 .htm|.html
$config['url_suffix'] = '.html';
开启缓存
$this->output->cache(n); n缓存时间
在缓存那个页面就放在对应的控制器中
放在构成函数中可全局缓存