Archive for the ‘PHP & MySQL’ Category

MySQL存储过程乱码解决方法

我本地的开发环境是windows,服务器为linux,上传到服务器后出现乱码的问题,把存储过程中的变量指定编码后问题解决 :grin:

DECLARE name VARCHAR(100) CHARACTER SET utf8;

用JS来实现PHP函数功能

http://phpjs.org/
很多PHP函数都有相应的Javascript实现

自定义CodeIgniter的404错误页面

http://maestric.com/doc/php/codeigniter_404
google一下,找一个国外朋友提供的一个解决方法。

其他几个解决方法。

http://codeigniter.com/forums/viewthread/76340/

http://codeigniter.com/forums/viewthread/90613/

http://codeigniter.com/forums/viewthread/54807/

把MY_Router类放到你的“./application/libraries/MY_Router.php”

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router {

	var $error_controller = 'error';
	var $error_method_404 = 'error_404';

    function My_Router()
    {
        parent::CI_Router();
    }

	// this is just the same method as in Router.php, with show_404() replaced by $this->error_404();
	function _validate_request($segments)
	{
		// Does the requested controller exist in the root folder?
		if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
		{
			return $segments;
		}

		// Is the controller in a sub-folder?
		if (is_dir(APPPATH.'controllers/'.$segments[0]))
		{
			// Set the directory and remove it from the segment array
			$this->set_directory($segments[0]);
			$segments = array_slice($segments, 1);

			if (count($segments) > 0)
			{
				// Does the requested controller exist in the sub-folder?
				if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
				{
					return $this->error_404();
				}
			}
			else
			{
				$this->set_class($this->default_controller);
				$this->set_method('index');

				// Does the default controller exist in the sub-folder?
				if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
				{
					$this->directory = '';
					return array();
				}
			}

			return $segments;
		}

		// Can't find the requested controller...
		return $this->error_404();
	}

	function error_404()
	{
		$this->directory = "";
		$segments = array();
		$segments[] = $this->error_controller;
		$segments[] = $this->error_method_404;
		return $segments;
	}

	function fetch_class()
	{
		// if method doesn't exist in class, change
		// class to error and method to error_404
		$this->check_method();

		return $this->class;
	}

	function check_method()
	{
		$ignore_remap = true;

		$class = $this->class;
		if (class_exists($class))
		{
			// methods for this class
			$class_methods = array_map('strtolower', get_class_methods($class));

			// ignore controllers using _remap()
			if($ignore_remap && in_array('_remap', $class_methods))
			{
				return;
			}

			if (! in_array(strtolower($this->method), $class_methods))
			{
				$this->directory = "";
				$this->class = $this->error_controller;
				$this->method = $this->error_method_404;
				include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
			}
		}
	}

	function show_404()
	{
		include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
		call_user_func(array($this->error_controller, $this->error_method_404));
	}

}
?>

这个是控制器

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
include_once("base.php");

class Error extends Controller {
	function error_404()
	{
		$this->load->view($this->_getTheme().'error_404_view', $this->data);
	}
}
?>

Image_moo Class

Image_moo是一个基于Codeigniter的图片类,比Codeigniter自带的Image_lib类要方便,且功能也很强。

//可以连续操作添加水印和生成不同尺寸的缩略图
$this->image_moo
	->load(“image.jpg”)
	->make_watermark_text("Text", "FONT.TTF", 25, "#000")
	->resize(600,600)
	->watermark(2)
	->save(“large.jpg”)
	->resize(400,400)
	->save(“medium.jpg”)
	->resize(100,100)
	->save(“small.jpg”);

Codeigniter的Ajax示例

下载后解压缩到自己的CI目录,导入SQL文件就可以直接查看了。示例是用Google Libraries API的Mootools 1.1.1版本,所以无需再自己下载了。
点击下载示例
示例中的Ajax代码

window.addEvent('domready', function(){
	$$('a[title=status]').each(function(el){
		el.addEvent('click', function(e){
			new Event(e).stop(); //停止a标签的跳转
			var uri = this.getProperty('href').split("#"); //把URL分割出等下要提交Ajax请求的地址和参数
			var value = uri[1].split("-"); //把参数分割为数据库中的id值和当前status的值
			new Ajax(uri[0], {
				method: 'post', //以post方式提交
				data: Object.toQueryString({
					//生成querystring格式的请求,示例"id=1&status=1"
					id:value[0],
					status: value[1] == '1' ? '0' : '1'
				}),

				//当ajax请求完成时触发的事件
				onComplete: function(vars){
					var r = Json.evaluate(vars); //把从PHP传递过来的Json字符串转换为Javascript对象
					if(r.result){ //判断刚才发送的Ajax请求是否更新成功
						this.setProperty('href', uri[0] + '#' + r.id + '-' + r.status);//更新a标签的URL与参数
						this.setText( r.status );//设置a标签的新文本为更新后的status值
					}
				}.bind(this)
			}).request();
		});

		el.addEvent('mouseover', function(){
			this.setProperty('title', '');
		});
	});
});

在Codeigniter中为上传文件指定名字

CI的Upload类不能在上传之前给文件指定名字,在官方论坛找到一个朋友提供的MY_Upload类,该类解决了上传文件不能先指定名字的问题。
使用方法如下:

$config['upload_filename'] = $upload_filename;
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '200';
$config['max_height'] = '200';
$this->load->library('upload', $config);

其实就是增加了一个upload_filename字段来做为指定的名字 :razz:

在Codeigniter中生成png透明缩略图

CI中的CI_Image_lib类不能生成背景透明的png或gif缩略图,以下是我的解决办法,重新创建一个MY_Image_lib类,并继承CI_Image_lib类,然后重写一下image_process_gd方法。

先在CI_Image_lib中找到image_process_gd方法,然后把这个方法的全部代码复制到MY_Image_lib类中,然后查找下面这行代码:

$dst_img = $create($this->width, $this->height);

接着在找到的这行代码下面添加如下代码:

$transparent = imagecolortransparent($src_img, imagecolorallocate($src_img, 0, 0, 0));
if ($transparent >= 0) {
	imagecolortransparent($dst_img, $transparent);
	imagealphablending($dst_img, false);
	imagesavealpha($dst_img, true);
}

TurboDbadmin很好用的MySQL工具

tda_ss2.jpg
TurboAjax提供很多不错的Ajax Control,其中有TurboDbadmin这个MySQL管理工具,可以即时更新添加数据,弥补phpmyadmin的烦琐操作。

Codeigniter中跳转回之前访问页面

有时需要进行一些操作后,跳转回之前访问输入的地址,比如会员登陆。
那在Codeigniter中如果实现呢?
原理很简单,在第一次访问时,用$this->uri->uri_string()返回Uri信息的字符串,然后保存在名为currentUri的session,之后每次访问其他页面时,都会将之前访问的Uri重新赋值给名为previousUri的session,在需要跳转回之前的页面时,就用$this->_redirect('', true)

//Controller中的两个方法,$this->_setPreviousUri()必须在__construct()中执行
function _setPreviousUri(){
	if( $this->session->isSess('currentUri') ){
		//之前访问URL
		$this->session->setSess('previousUri', $this->session->getSess('currentUri'));
	}
	//当前访问URL
	$this->session->setSess('currentUri', $this->uri->uri_string());
	$this->currentUri = $this->session->getSess('currentUri');
	$this->previousUri = $this->session->getSess('previousUri');
}

function _redirect($uri='', $prev=false){
	if($prev) redirect($this->previousUri, 'refresh');
	else redirect($url, 'refresh');
}

CI Nested Sets类的一个错误

今天在使用CodeIgniter wiki上面的Nested Sets类时,发现了一个错误。查找Nested_sets_model.php的第560行,代码如下

return $result->level;

应该改为

return $result[0]->level;
Powered by WordPress | Shop Free Cellular Phones at Bestincellphones.com. | Thanks to Best CD Rates, iCellPhoneDeals.com Offers Best Cell Phone Deals. and Incinerador De Grasa