龚哥哥 爱生活、做自己!
PHP生成分页类 前端基于amazeui
发表于 2015-8-31 | 浏览(2201) | PHP
<?php

/**
 * 分页驱动
 * @author  Devil
 * @version v_1.0.0
 */
class PageLibrary
{
	private $page;
	private $total;
	private $number;
	private $bt_number;
	private $where;
	private $page_total;
	private $url;
	private $html;

	/**
	 * [__construct description]
	 * @param [int]    $param['total'] 		[数据总数]
	 * @param [int]    $param['number'] 	[每页数据条数]
	 * @param [int]    $param['bt_number'] 	[分页显示按钮个数]
	 * @param [array]  $param['where'] 		[额外条件(键值对)]
	 * @param [string] $param['url'] 		[url地址]
	 */
	public function __construct($param = array())
	{
		$this->page = max(1, isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1);
		$this->total = max(1, isset($param['total']) ? intval($param['total']) : 1);
		$this->number = max(1, isset($param['number']) ? intval($param['number']) : 1);
		$this->bt_number = isset($param['bt_number']) ? intval($param['bt_number']) : 2;
		$this->where = (isset($param['where']) && is_array($param['where'])) ? $param['where'] : '';
		$this->url = isset($param['url']) ? $param['url'] : '';
		$this->page_total = 1;
		$this->html = '';

		/* 参数设置 */
		$this->SetParem();
	}

	/**
	 * [SetParem 参数设置]
	 */
	private function SetParem()
	{
		/* 防止超出最大页码数 */
		$this->page_total = ceil($this->total/$this->number);
		if($this->page > $this->page_total) $this->page = $this->page_total;

		/* 额外条件url设置 */
		if(!empty($this->where) && is_array($this->where))
		{
			foreach($this->where as $k=>$v) $this->url .= '&'.$k.'='.$v;
		}
		
	}

	/**
	 * [GetPageHtml 获取生成好的分页代码]
	 */
	public function GetPageHtml()
	{
		$this->html .= '<ul class="am-pagination am-pagination-centered"><li';
		$this->html .= ($this->page > 1) ? '' : ' class="am-disabled"';
		$this->html .= '><a href="'.$this->url.'&page='.($this->page-1).'" class="meila-radius">&laquo;</a></li>';
		$this->html .= $this->GetButtonNumberHtml();
		$this->html .= '<li';
		$this->html .= ($this->page > 0 && $this->page < $this->page_total) ? '' : ' class="am-disabled"';
		$this->html .= '><a href="'.$this->url.'&page='.($this->page+1).'" class="meila-radius">&raquo;</a></li></ul>';

		return $this->html;
	}

	/**
	 * [GetButtonNumberHtml 获取button显示个数的html]
	 * @return [string] [按钮个数html代码]
	 */
	private function GetButtonNumberHtml()
	{
		$html_before = '';
		$html_after = '';
		$html_page = '<li class="am-active"><a class="meila-radius">'.$this->page.'</a></li>';
		if($this->bt_number > 0)
		{
			/* 前按钮 */
			if($this->page > 1)
			{
				$total = ($this->page-$this->bt_number < 1) ? 1 : $this->page-$this->bt_number;
				for($i=$this->page-1; $i>=$total; $i--)
				{
					$html_before = '<li><a href="'.$this->url.'&page='.$i.'" class="meila-radius">'.$i.'</a></li>'.$html_before;
				}
			}

			/* 后按钮 */
			if($this->page_total > $this->page)
			{
				$total = ($this->page+$this->bt_number > $this->page_total) ? $this->page_total : $this->page+$this->bt_number;
				for($i=$this->page+1; $i<=$total; $i++)
				{
					$html_after .= '<li><a href="'.$this->url.'&page='.$i.'" class="meila-radius">'.$i.'</a></li>';
				}
			}
		}
		return $html_before.$html_page.$html_after;
	}

	/**
	 * [GetPageStarNumber 获取分页起始值]
	 */
	public function GetPageStarNumber()
	{
		return intval(($this->page-1)*$this->number);
	}
}
?>

阅读全文

CentOS安装PHP的Redis扩展
发表于 2015-8-31 | 浏览(1502) | 服务器

1;下载扩展包

wget https://github.com/nicolasff/phpredis/archive/master.zip

360云盘下载地址:

http://yunpan.cn/ccXwiNS2B5Hpi  访问密码 2970

2;解压扩展压缩包

unzip master.zip

3;进入安装包

cd phpredis-master

4;安装php-devel,fastcgi模式,phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块

yum install php-devel

5;编译安装

phpize
./configure
make
make install

6;修改php.ini

vim /etc/php.ini 添加 extension=redis.so

阅读全文

PHP PDO操作API
发表于 2015-8-31 | 浏览(1856) | PHP
<?php

/**
 *	DB驱动
 *	@author  Devil
 *	@version v_0.0.1
 */
class DBLibrary
{
	private $obj;

	/**
	 * [__construct 构造方法]
	 * @param [string]  $host    [连接地址]
	 * @param [string]  $name    [数据库名]
	 * @param [string]  $user    [用户名]
	 * @param [string]  $pwd     [用户密码]
	 * @param [boolean] $connect [是否长连接]
	 */
	private function __construct($host, $name, $user, $pwd, $connect)
	{
		if(empty($host) || empty($name) || empty($user) || empty($pwd)) exit('Param Error');
		try
		{
			$charset = C('charset');
			$charset = empty($charset) ? 'utf8' : str_replace('-', '', $charset);
			@$this->obj = new PDO("mysql:dbname={$name};host={$host};charset={$charset}", $user, $pwd, array(PDO::ATTR_PERSISTENT=>$connect));

			/* 属性设置 */
			$this->obj->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
		}
		catch(PDOException $e)
		{
			exit('DB Connect Fails');
		}
	}

	/**
	 * [SetInstance 静态方法]
	 * @param string $type [请求类型]
	 */
	public static function SetInstance($host, $name, $user, $pwd, $connect = false)
	{
		static $object = null;
		if(!is_object($object)) $object = new self($host, $name, $user, $pwd, $connect);
		return $object;
	}

	/**
	 * [Query 查询数据对象]
	 * @param  [string] $sql [sql查询语句]
	 * @return [obj] [对象]
	 */
	private function Query($sql)
	{
		if(empty($sql)) return '';
		try
		{
			$rs = $this->obj->query($sql);
			if(!is_object($rs)) exit('DB Non Object Error');
			$rs->setFetchMode(PDO::FETCH_ASSOC);
			return $rs;
		}
		catch(PDOException $e)
		{
			exit('DB Resources Error');
		}
	}

	/**
	 * [GetAll 获取所有数据]
	 * @param  [string] $sql [sql查询语句]
	 * @return [array|空字符串] [二维数组或空字符串]
	 */
	public function GetAll($sql)
	{
		$data = $this->Query($sql)->fetchAll();
		return empty($data) ? '' : $data;
	}

	/**
	 * [GetRow 获取一行的数据]
	 * @param  [string] $sql [sql查询语句]
	 * @return [array|空字符串] [一维数组或空字符串]
	 */
	public function GetRow($sql)
	{
		$data = $this->Query($sql)->fetch();
		return empty($data) ? '' : $data;
	}

	/**
	 * [GetOne 获取一个字段的数据]
	 * @param  [string] $sql [sql查询语句]
	 * @return [string|空字符串] [字符串或空字符串]
	 */
	public function GetOne($sql)
	{
		$data = $this->Query($sql)->fetchColumn();
		return empty($data) ? '' : $data;
	}

	/**
	 * [GetCol 获取所有数据的一列数据]
	 * @param  [string] $sql [sql查询语句]
	 * @return [array|空字符串] [一维数组或空字符串]
	 */
	public function GetCol($sql)
	{
		if(empty($sql)) return '';
		$data = array();
		$rs = $this->obj->query($sql);
		$rs->setFetchMode(PDO::FETCH_NUM);
		while($row = $rs->fetch())
		{
			$data[] = $row[0];
		}
		return empty($data) ? '' : $data;
	}

	/**
	 * [Exec 数据更新 删除]
	 * @param  [string] $sql [sql操作语句]
	 * @return [boolean] [成功true, 失败false]
	 */
	public function Exec($sql)
	{
		if($this->obj->exec($sql)) return true;
		return false;
	}

	/**
	 * [Insert 数据插入]
	 * @param  [string] $sql [sql插入语句]
	 * @return [int] [成功返回自增id(表无自增id则0), 失败-1]
	 */
	public function Insert($sql)
	{
		if($this->obj->exec($sql)) return $this->obj->lastInsertId();
		return -1;
	}

	/**
	 * [StartTrans 开启事物]
	 */
	public function StartTrans()
	{
		$this->obj->beginTransaction();
	}

	/**
	 * [Commit 提交事物]
	 */
	public function Commit()
	{
		$this->obj->commit();
	}

	/**
	 * [RollBack 回滚事物]
	 */
	public function RollBack()
	{
		$this->obj->rollBack();
	}
}

?>

阅读全文

TOP