代码改变世界

php的CodeIgniter学习笔记(二)

2009-06-12 20:31  Otis's Technology Space  阅读(3803)  评论(0编辑  收藏  举报

1,为CodeIgniter添加 Json功能。

php5.2已经内置了json_encode, json_decode方法。

但是,php 5.2以下的版本,并没有Json功能。加上这个功能也很简单:

http://codeigniter.com/wiki/JSON_Helper/ 去这个地方下载json的实现。

装上去后如果有错,请把json_helper.php里的两个方法改成:

 function json_encode($data = null)
  {
      $json = new Services_JSON();
  	if($data == null) return false;
  	return $json->encode($data);
  }

  function json_decode($data = null)
  {
      $json = new Services_JSON();
  	if($data == null) return false;
  	return $json->decode($data);
  }

 

2,PHP常用的几个时间函数。。

date() 返回时间字符串。

mktime() 返回unix时间戳。

strtotime() 返回Unix时间戳。

time() 返回当前Unix时间戳。

3,CodeIgniter 的一些常用查询。

        $this->db->select('page,year,month,day,count(ip) as visitorNum');
        $this->db->select_sum('totol_m');
        $this->db->group_by(array('page','year','month','day'));
        $where = array('page'=>$page,'year'=>$y,'month'=>$m);
        $this->db->where($where);
        $this->db->from('ipcount');
        $query = $this->db->get();
 
        $this->db->select('id, date_submission');
        $this->db->where('date_submission >',date('Y-m-d H:i:s',$start_date));
        $this->db->where('date_submission <',date('Y-m-d H:i:s',$end_date));
        $this->db->from('`mh_photos');
        $query = $this->db->get();
 
4,让CodeIgniter能使用$_GET。
$config['uri_protocol']    = "PATH_INFO";//在config.php里设置。
config里的hooks.php添加以下内容:
 
$hook['post_controller_constructor'] = array(
		 	'function' => 'allow_query_string',
		 	'filename' => 'allow_query_string.php',
		 	'filepath' => 'hooks'
		 );
 
hooks里添加以下文件 : allow_query_string.php
内容如下:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
	Function: allow_query_string
	Overrides CIs default behaviour of destroying $_GET

	Install:
		Put this function in hooks/allow_query_string.php

		Enable hooks in config/config.php
		> $config['enable_hooks'] = TRUE;

		Define the hook in config/hooks.php
		> $hook['post_controller_constructor'] = array(
		> 	'function' => 'allow_query_string',
		> 	'filename' => 'allow_query_string.php',
		> 	'filepath' => 'hooks'
		> );
*/
function allow_query_string() {
    parse_str($_SERVER['QUERY_STRING'], $_GET);
    $_CI =& get_instance();
    foreach ($_GET as $key=>$val) {
		$_GET[$key] = $_CI->input->xss_clean($val);
	}
}

其它这里有说了。。哈。

大概是这样。。

下图是用CodeIgniter,Json结合Flex弄的统计的界面。。

image