分类目录归档:codeigniter

【转】给Eclipse PDT加入CodeIgniter框架的自动完成支持——支持CodeIgniter 2.1.0

给Eclipse PDT加入codeigniter框架的自动完成支持
http://blog.k-res.net/%E8%BD%AF%E4%BB%B6%E4%BD%BF%E7%94%A8/%E7%BB%99eclipse-pdt%E5%8A%A0%E5%85%A5codeigniter%E6%A1%86%E6%9E%B6%E7%9A%84%E8%87%AA%E5%8A%A8%E5%AE%8C%E6%88%90%E6%94%AF%E6%8C%81.html

1.给你的WEB项目加入:右键项目->Configure->Add PHP Support…

2.给CI_Model基类和CI_Controller基类加入以下代码,这两个文件的位置在2.1.0版本里是在system/core下,注意,注释部分不可无视!

/**
  * @var CI_Config
  */
 var $config;
 /**
  * @var CI_DB_active_record
  */
 var $db;
 /**
  * @var CI_Email
  */
 var $email;
 /**
  * @var CI_Form_validation
  */
 var $form_validation;
 /**
  * @var CI_Input
  */
 var $input;
 /**
  * @var CI_Loader
  */
 var $load;
 /**
  * @var CI_Router
  */
 var $router;
 /**
  * @var CI_Session
  */
 var $session;
 /**
  * @var CI_Table
  */
 var $table;
 /**
  * @var CI_Unit_test
  */
 var $unit;
 /**
  * @var CI_URI
  */
 var $uri;
 /**
  * @var CI_Pagination
  */
 var $pagination;

3.一切OK,试一下->应该有反应了。

codeigniter 2.0 添加自定义的类库以支持memcache

codeigniter 2.0 添加自定义的类库以支持memcache
1、将下面的代码文件放置到application/system/libraries/目录下

 array(
			'host'	=> '192.168.117.100',
			'port'	=> 11211,
		),
		'u'	=> array(
			'host'	=> '192.168.117.100',
			'port'	=> 11211,
		),
	);
	
	private $mem = NULL;
	
	function __construct()
	{
		$this->mem = new Memcache();
	}
	
	/**获取数据
	 * @param	key		键值(string,array('key1', 'key2')
	 * @param	type	业务类别(r,u)
	 * @return	false or string or array(
	 * 		'key1'	=> data1,
	 * 		'key2'	=> data2,
	 * )
	 * 
	 */
	function get($key, $type)
	{
		if(empty($key) || empty($type))
			return false;
			
		$this->mem->connect($this->server_list[$type]['host'], $this->server_list[$type]['port']);
		
		$ret_data = $this->mem->get($key);
		$this->mem->close();
		return $ret_data;
	}
	
	/** 设置cache
	 * @param	key		键(如果为数组,则表示设置多条cache,要求格式为array('key1' => data1, 'key2' => data2...),此时value不用设置)
	 * @param	type	业务类型(r/u/...)
	 * @param	value	值
	 * @param	expire	过期时间(秒)
	 * @return	true/false
	 */
	function  set($key, $type, $value  = NULL, $expire = 60)
	{
		if(empty($key) || empty($type) || (! is_array($key) && empty($value)))
			return false;
		
		$this->mem->connect($this->server_list[$type]['host'], $this->server_list[$type]['port']);
		if(is_array($key))
		{
			foreach ($key as $k => $v)
			{
				$this->mem->set($k, $v, 0, $expire);
			}
		}
		else
		{
			$this->mem->set($key, $value, 0, $expire);
		}
		
		$this->mem->close();
		return true;
	}
	
	/** 删除cache
	 * @param	key		键值(string,array('key1', 'key2'))
	 * @param	type	业务类型
	 * @param	timeout	超时时间
	 * @return	true/false
	 */
	function  del($key, $type, $timeout = 0)
	{
		if(empty($key) || empty($type))
			return false;
			
		$this->mem->connect($this->server_list[$type]['host'], $this->server_list[$type]['port']);
		if(is_array($key))
		{
			foreach ($key as $v)
			{
				$this->mem->delete($key, $timeout);
			}
		}
		else
			$this->mem->delete($key, $timeout);
			
		$this->mem->close();
		return true;
	}
	
}

2、在application/controllers下添加Cachetest类测试Cacheop类库

load->library('cacheop');
		$this->cacheop->set('1-stamhe', 'u', 'stamhe1', 86400);
		$this->cacheop->set('2-stamhe', 'u', 'stamhe2', 86400);
		$this->cacheop->set('3-stamhe', 'u', 'stamhe3', 86400);
		$this->cacheop->set(array('4-stamhe' => 'stamhe4', '5-stamhe' => 'stamhe5'), 'u', NULL, 86400);
	}
	
	function  get()
	{
		$this->load->library('cacheop');
		print_r($this->cacheop->get(array('1-stamhe', '2-stamhe', '3-stamhe', '4-stamhe', '5-stamhe'), 'u'));
	}
}

3、访问url【http://192.168.4.112/?c=cachetest&m=get】测试

codeigniter 2.0.2 中使用jquery ajax 框架

codeigniter 2.0.2 中使用jquery ajax 框架
http://www.ryantetek.com/2009/12/how-to-create-a-simple-ajax-post-in-codeigniter-using-jquery/
1、新增一个Ajax_post控制器,代码如下

load->helper(array('url'));
	}
	
	function index()
	{
		$data['baseurl'] = $this->config->item('base_url');
		$this->load->view('ajax_post', $data);
	}
	
	function post_action()
	{
		$message = "";
		$bg_color = "";
		if(($_POST['username'] == "") || ($_POST['password'] == ''))
		{
			$message = "Please fill up blank fields.";
			$bg_color = "#ffebe8";
		}
		else if($_POST['username'] != 'stamhe' || $_POST['password'] != '123456')
		{
			$message = "Username or Password do not match";
			$bg_color = "#ffebe8";
		}
		else
		{
			$message = "Username and Password matched";
			$bg_color = "FFA";
		}

		$output = array(
			'message'	=> $message,
			'bg_color'	=> $bg_color
		);
		
		echo json_encode($output);
	}
}

2、根据上面的控制器代码,新建名为ajax_post.php的视图文件,代码如下



	
		
		ci ajax demo
		
		
		
	
	
		

This is the demo page ofr the simple AJAX post in CodeIgniter Using jQuery

Username: stamhe | Password: 123456

Try to enter nothing in the fields and click the submit button.
Try to mismatch the username and/or password and click the submit button.

Username/Email:*

Password:*

3、在js目录下,新建名为ajax_post.js的javascript脚本文件,代码如下:

$(document).ready(function(){
	$("#login_submit").click(function(){
		var username = $("#username").val();
		var password = $("#password").val();
		
		$.ajax({
			type: 'post',
			dataType: 'json',
			cache: false,
			url: "http://192.168.4.112/ajax_post/post_action",
			data: 'username=' + username + '&password=' + password,
			success:
				function(data){
					$("#form_message").html(data.message).css({"background-color": data.bg_color}).fadeIn('slow');
				}
		});
		return false;
	});
});

4、访问http://192.168.4.112/?c=ajax_post&m=index进行测试

使codeigniter支持$_GET

使codeigniter支持$_GET
http://codeigniter.com/wiki/QUERY_STRING_GET/

1、打开config.php文件,将【url_protocol】设置为PATH_INFO,将【enable_query_strings】设置为TRUE

vim   application/config/config.php
$config['uri_protocol'] = 'PATH_INFO';
$config['enable_query_strings'] = TRUE;

2、在需要使用$_GET之前,添加上下面的代码即可使用$_GET来获取参数了

parse_str($_SERVER['QUERY_STRING'], $_GET);

示例:
http://192.168.4.112/?c=blog&m=loadget1&username=stamhe&pwd=123456
调用controller为blog的loadget1函数,在其中像下面这样获取username与pwd的分段参数

parse_str($_SERVER['QUERY_STRING'], $_GET);
echo "username: ".$_GET['username']."
"; echo "pwd: ".$_GET['pwd']."
";