一、分页
示例代码:
//装载类文件 $this -> load -> library('pagination'); $controller = $this->router->fetch_class(); $action = $this->router->fetch_method(); //每页显示10条数据,共有101条数据 $config['base_url'] = site_url("$controller/$action"); $config['total_rows'] = 101; $config['per_page'] = 10; $config['first_link'] = "首页"; $config['prev_link'] = "上一页"; $config['next_link'] = "下一页"; $config['last_link'] = "最后一页"; //如果setment不是3需要设置,默认为3 $config['uri_segment'] = 3; $this -> pagination -> initialize($config); //偏移量 $offset = intval($this -> uri -> segment(3)); $sql = "SELECT * FROM `TEST` LIMIT $offset," . $config['per_page']; $pager = $this -> pagination -> create_links(); $this -> load -> view('article/index', array('pager' => $pager, 'sql' => $sql));
需要注意的是,如果配置了url中隐藏index.php,在site_url中生成的路径中默认还是有index.php,需要在application/config/config.php中改成如下配置:
$config['index_page'] = '';
即可。
二、文件上传
示例代码:
//目录需要手动创建 $config['upload_path'] = './uploads/'; $config['allowed_types'] = "gif|png|jpg|jpeg"; $config['max_size'] = 512; $config['file_name'] = uniqid(); $this -> load -> library('upload', $config); //pic为input type=file的name $this -> upload ->do_upload('pic'); //上传文件信息 $uploadInfo = $this -> upload -> data(); var_dump($uploadInfo); /* array (size=14) 'file_name' => string '55a61d04e96f4.jpg' (length=17) 'file_type' => string 'image/jpeg' (length=10) 'file_path' => string 'E:/wamp/www/citest/uploads/' (length=27) 'full_path' => string 'E:/wamp/www/citest/uploads/55a61d04e96f4.jpg' (length=44) 'raw_name' => string '55a61d04e96f4' (length=13) 'orig_name' => string '55a61d04e96f4.jpg' (length=17) 'client_name' => string 'hk.jpg' (length=6) 'file_ext' => string '.jpg' (length=4) 'file_size' => float 125.09 'is_image' => boolean true 'image_width' => int 675 'image_height' => int 900 'image_type' => string 'jpeg' (length=4) 'image_size_str' => string 'width="675" height="900"' (length=24) */
即可。
三、session
1、开启
$this -> load -> library('session');
默认已经开启,用上面的代码加载session库。
2、写入
$user = array( 'id' => 3, 'name' => 'jim' ); $this -> session -> set_userdata("user", $user);
3、读取
$this -> session -> userdata('user')
注:建议在 application/config/config.php 中配置加密key:
$config['encryption_key'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
四、验证码
示例:
$this->load->helper('captcha'); $vals = array( //'word' => 'Random word', 'img_path' => './captcha/', 'img_url' => base_url() . 'captcha/', //'font_path' => './path/to/fonts/texb.ttf', 'img_width' => '200', 'img_height' => 80, 'expiration' => 60, 'word_length' => 10, 'font_size' => 26, '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); var_dump($cap); /* array (size=4) 'word' => string 'hFan3NM4xB' (length=10) 'time' => float 1436954914.3053 'image' => string '<img id="Imageid" src="http://localhost/citest/captcha/1436954914.3053.jpg" style="width: 200; height: 80; border: 0;" alt=" " />' (length=129) 'filename' => string '1436954914.3053.jpg' (length=19) */ echo $cap['image'];
注意:需要手动创建存放验证码的文件夹,验证码图片会在超时后自动删除。