English Sentence Loading...
英语句子加载中...
用php实现一个双向队列
作者:蘑菇 日期:2010-07-23 00:54
代码实现:
这道题从难度上讲其实不是很难,它主要考察了phper以下几个方面的技能:
1. 当然是双向队列的定义,这个就不多做解释了。
2. 考察对函数是否熟悉。
3. 考察OOP编程。
4. 考察程序员的代码规范和编程习惯。
下载: deque.php
- <?php
- class deque
- {
- public $queue = array();
- public $length = 0;
- public function frontAdd($node){
- array_unshift($this->queue,$node);
- $this->countqueue();
- }
- public function frontRemove(){
- $node = array_shift($this->queue);
- $this->countqueue();
- return $node;
- }
- public function rearAdd($node){
- array_push($this->queue,$node);
- $this->countqueue();
- }
- public function rearRemove(){
- $node = array_pop($this->queue);
- $this->countqueue();
- return $node;
- }
- public function countqueue(){
- $this->length = count($this->queue);
- }
- }
- ?>
这道题从难度上讲其实不是很难,它主要考察了phper以下几个方面的技能:
1. 当然是双向队列的定义,这个就不多做解释了。
2. 考察对函数是否熟悉。
3. 考察OOP编程。
4. 考察程序员的代码规范和编程习惯。
php 分页类
作者:蘑菇 日期:2010-06-01 23:22
这个分页类融合了数据库查询的功能,用起来还比较方便。下面这个是用了ADODB类来操作数据库,关于ADODB的操作,在这里就不多说了。
使用举例:
下载: dbclass.php
- <?php
- /**
- * FileName : dbclass.php
- * Description: Simple db class
- * Author : Liubing(liubing@eduicc.com)
- */
- class dbclass
- {
- var $dblink;
- var $_sql;
- var $primary_key;
- function dbclass($primary_key='id')
- {
- $this->connectDB();
- $this->primary_key = $primary_key;
- }
- function connectDB()
- {
- global $db_config;
- if(is_object($this->dblink)) return;
- $this->dblink = &ADONewConnection('mysql');
- $this->dblink->PConnect($db_config["db_host"],$db_config["db_user"],$db_config["db_password"],$db_config["db_name"]);
- }
- function E($sql='')
- {
- $sql != '' && $this->_sql = $sql;
- $this->dblink->Execute("SET NAMES UTF8");
- return $this->dblink->Execute($this->_sql);
- }
- function Store($fields,$tb,$key='')
- {
- if(count($fields) == 0) return false;
- foreach($fields as $field => $value){
- $params[] = "`{$field}`='".$this->safestring($value)."'";
- }
- $key = intval($key);
- if($key > 0){ //modify
- if($this->primary_key == '' || $key < 1) return false;
- $sql = "UPDATE `{$tb}` SET ".implode(",",$params)." WHERE `{$this->primary_key}`='{$key}'";
- }else{ //add new record
- $sql = "INSERT INTO `{$tb}` SET ".implode(",",$params);
- }
- return $this->E($sql);
- }
- function getResults($params,$tb,$orderfield='')
- {
- $sql = "SELECT * FROM `{$tb}` ";
- if(is_array($params) && count($params) > 0){
- $sql.= " WHERE ".implode(" AND ",$params);
- }
- if($orderfield != '') $sql.= " ORDER BY `{$orderfield}`";
- $rs = $this->E($sql);
- $offset = 0;
- while(!$rs->EOF){
- $rs->fields["offset"] = ++$offset;
- $results[] = $rs->fields;
- $rs->MoveNext();
- }
- if(count($results) == 0) return false;
- elseif(count($results) == 1) return $results[0];
- else return $results;
- }
- function getRow($key,$tb)
- {
- if($this->primary_key == '' || $key < 1) return false;
- $params[] = "`{$this->primary_key}`='{$key}'";
- $results = $this->getResults($params,$tb);
- return $results;
- }
- function delRow($key,$tb)
- {
- if($this->primary_key == '' || $key < 1) return false;
- $sql = "DELETE FROM `{$tb}` WHERE `".$this->primary_key."`={$key}";
- $this->E($sql);
- }
- function getNum($tb)
- {
- $sql = "SELECT COUNT(*) AS `t` FROM `{$tb}`";
- $rs = $this->E($sql);
- return $rs->fields['t'];
- }
- function getMax($field,$table,$params=array())
- {
- $sql = "SELECT MAX(`{$field}`) AS `{$field}` FROM `{$table}` WHERE 1 ";
- foreach($params as $f => $val){
- $sql.= " AND `{$f}`='{$val}'";
- }
- $rs = $this->E($sql);
- return $rs->fields[$field];
- }
- //html,mysql
- function safestring($string,$mode="3")
- {
- switch($mode)
- {
- case "0":
- break;
- case "1":
- $string = mysql_escape_string($string);
- break;
- case "2":
- $string = strip_tags($string);
- break;
- case "3":
- $string = strip_tags(mysql_escape_string($string));
- break;
- default:
- break;
- }
- return $string;
- }
- }
下载: pageclass.php
- <?php
- /**
- * FileName : pageclass.php
- * Description: pagination class
- * Author : Liubing(liubing@eduicc.com)
- */
- require_once dirname(__FILE__).'/dbclass.php';
- class pageclass extends dbclass
- {
- var $page;
- var $pagesize;
- var $pagesql;
- var $results;
- var $offset;
- var $params;
- var $totalpage;
- var $totalnum;
- var $pagelink;
- var $pagerange;
- function pageclass($page=1,$params=array(),$pagesize=10)
- {
- dbclass::dbclass();
- $page = intval($page);
- $pagesize = intval($pagesize);
- $this->page = $page < 1 ? 1 : $page;
- $this->pagesize = $pagesize < 10 ? 10 : $pagesize;
- $this->offset = ($this->page - 1) * $this->pagesize;
- $this->params = $params;
- $this->pagerange = 10;
- }
- function loadpage($sql)
- {
- $sql = preg_replace("/select/i","select",$sql);
- $sqlitems = explode("select",$sql);
- unset($sqlitems[0]);
- $newsql = implode("SELECT",$sqlitems);
- $newsql = "SELECT SQL_CALC_FOUND_ROWS ".$newsql." LIMIT {$this->offset},{$this->pagesize}";
- $rs = $this->E($newsql);
- $pagesql = "SELECT FOUND_ROWS() AS `allnum`";
- $pagers = $this->E($pagesql);
- $pageresult = $pagers->fields;
- $this->totalnum = $pageresult["allnum"];
- $this->setpage();
- $offset = $this->offset;
- while(!$rs->EOF){
- $offset++;
- $rs->fields['offset'] = $offset;
- $this->results[] = $rs->fields;
- $rs->MoveNext();
- }
- }
- function setpage()
- {
- if(count($this->params) > 0){
- foreach($this->params as $key => $value){
- $params.= "{$key}={$value}&";
- }
- }
- $this->totalpage = ceil($this->totalnum/$this->pagesize);
- $this->pagelink = "<span class='nolink'>".$this->page."/".$this->totalpage."</span>";
- //FIRST PREV
- if($this->page > 1){
- $this->pagelink.= " <a href='?{$params}page=1'>FIRST</a> ";
- $this->pagelink.= " <a href='?{$params}page=".($this->page - 1)."'>PREV</a> ";
- }else{
- $this->pagelink.= " <span class='nolink'>FIRST</span> ";
- $this->pagelink.= " <span class='nolink'>PREV</span> ";
- }
- for($i = $this->page - $this->pagerange; $i <= $this->page + $this->pagerange; $i++){
- if($i < 1 || $i > $this->totalpage) continue;
- $this->pagelink.= $i == $this->page ? " <span class='currentpage'>[{$i}]</span> " : "<a href='?{$params}page={$i}'>[{$i}]</a> ";
- }
- //NEXT LAST
- if($this->page < $this->totalpage){
- $this->pagelink.= " <a href='?{$params}page=".($this->page + 1)."'>NEXT</a> ";
- $this->pagelink.= " <a href='?{$params}page={$this->totalpage}'>LAST</a> ";
- }else{
- $this->pagelink.= " <span class='nolink'>NEXT</span> ";
- $this->pagelink.= " <span class='nolink'>LAST</span> ";
- }
- }
- }
使用举例:
下载: list.php
- <?php
- //引入ADODB
- require_once dirname(__FILE__)."/../lib/adodb/adodb.inc.php";
- //DB configuration
- $db_config = array("db_host"=>"localhost","db_user"=>"root","db_password"=>"123456","db_name"=>"flow");
- $page = intval($_REQUEST['page']);
- $sql = "SELECT * FROM `flow_bulletins` WHERE 1=1 ";
- $keyword != "" && $sql.= " AND (`bulletin_title_en` LIKE '%{$keyword}%' OR `bulletin_title_cn` LIKE '%{$keyword}%' OR `bulletin_title_fr` LIKE '%{$keyword}%') ";
- $status != "" && $sql.= " AND `enabled`='{$status}' ";
- $sql .= " ORDER BY `ordering`";
- $pageparams = compact("keyword","status");
- $pageobj = new pageclass($page, $pageparams);
- $pageobj->loadpage($sql);
- var_dump($pageobj);
- ?>
php 生成 xml 添加 BOM
作者:蘑菇 日期:2010-05-27 21:20
php 生成缩略图
作者:蘑菇 日期:2010-05-06 11:56
下载: thumbclass.php
- <?php
- /*
- * Create Thumbnail picture
- * Author: Liubing
- * Last Modify: 2010-05-06 12:09
- *
- * How to use it:
- *
- * $source_pic = dirname(__FILE__).'/1920VectorLove008.png';
- * $thumb = new Thumbnail($source_pic,250,100);
- * $thumb->create();
- */
- class Thumbnail
- {
- /* source picture */
- var $source_pic;
- /* source picture width */
- var $source_w;
- /* source picture height */
- var $source_h;
- /* source picture mimetype */
- var $source_mime;
- /* source picture is valid or not */
- var $source_valid;
- /* thumb picture directory */
- var $dest_dir;
- /* thumb picture name */
- var $dest_pic;
- /* thumb picture width */
- var $dest_w;
- /* thumb picture height */
- var $dest_h;
- /* assign mimetype to function name */
- var $mime2func;
- function Thumbnail($source_pic,$width="400",$height="300",$dest_dir="")
- {
- $this->source_pic = $source_pic;
- $this->source_valid = false;
- $this->dest_dir = $dest_dir == "" ? dirname(__FILE__) : $dest_dir;
- $this->dest_w = intval($width);
- $this->dest_h = intval($height);
- $this->mime2func = array("image/jpeg" => "jpeg", "image/gif" => "gif", "image/png" => "png");
- $this->readSource();
- }
- /* Read Source Picture */
- function readSource()
- {
- if(file_exists($this->source_pic)){
- $picinfo = getimagesize($this->source_pic);
- if(is_array($picinfo)){
- $this->source_w = $picinfo[0];
- $this->source_h = $picinfo[1];
- $this->source_mime = $picinfo['mime'];
- $this->source_valid = true;
- $basename = basename($this->source_pic);
- $this->dest_pic = substr(md5(rand()),0,10)."_".$this->dest_w."_".$this->dest_h."_".$basename;
- $this->setRealSize();
- }
- }
- }
- //Set Thumb Size (keep proportion)
- function setRealSize()
- {
- if(!$this->source_valid) return;
- if($this->dest_w < 1 || $this->dest_w >= $this->source_w) $this->dest_w = 0;
- if($this->dest_h < 1 || $this->dest_h >= $this->source_h) $this->dest_h = 0;
- if($this->dest_w == 0 && $this->dest_h == 0) return;
- if($this->dest_w > 0 && $this->dest_h > 0){
- if(($this->source_w / $this->dest_w) > ($this->source_h / $this->dest_h)){
- $this->dest_h = ceil(($this->source_h * $this->dest_w) / $this->source_w);
- }else{
- $this->dest_w = ceil(($this->source_w * $this->dest_h) / $this->source_h);
- }
- }elseif($this->dest_w > 0){
- $this->dest_h = ceil(($this->source_h * $this->dest_w) / $this->source_w);
- }else{
- $this->dest_w = ceil(($this->source_w * $this->dest_h) / $this->source_h);
- }
- }
- function create()
- {
- if(!$this->source_valid || $this->mime2func[$this->source_mime] == '') return;
- //create folder
- if(substr($this->dest_dir,-1,1) != "/") $this->dest_dir .= "/";
- $this->dest_dir .= "thumb/".date("Y-m");
- if(!is_dir($this->dest_dir)) mkdir($this->dest_dir);
- //copy source to thumb
- if($this->dest_w == 0 || $this->dest_h == 0){
- copy($this->source_pic,$this->dest_dir."/".$this->dest_pic);
- return;
- }
- //create thumb
- $createfunc = "imagecreatefrom".$this->mime2func[$this->source_mime];
- $outputfunc = "image".$this->mime2func[$this->source_mime];
- $d_im = imagecreatetruecolor($this->dest_w,$this->dest_h);
- $s_im = $createfunc($this->source_pic);
- imagecopyresized($d_im, $s_im, 0, 0, 0, 0, $this->dest_w, $this->dest_h, $this->source_w, $this->source_h);
- //save
- $outputfunc($d_im,$this->dest_dir."/".$this->dest_pic);
- //destroy
- imagedestroy($d_im);
- imagedestroy($s_im);
- }
- }
- ?>
Ubuntu/CentOS 安装 Memcache
作者:蘑菇 日期:2010-04-22 12:00
平台: Ubuntu 9.10 + Nginx 0.7.65 + Mysql 5.1.45 + php 5.2.10 FastCGI
1. 下载memcache: http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
2. 解压安装
$ tar -zxvf memcached-1.4.5.tar.gz
$ cd memcached-1.4.5
$ ./configure
$ make
$ sudo make install
3. 启动
$ sudo /usr/local/bin/memcached -d -c 128 -u root -P /tmp/memcached.pid
4. 安装php-memcache扩展
下载:http://pecl.php.net/get/memcache-2.2.4.tgz
#解压安装
$ tar -zxvf memcache-2.2.4.tgz
$ cd memcache-2.2.4
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
$ make
$ sudo mkdir /usr/local/php/extensions/
$ sudo cp modules/memcache.so /usr/local/php/extensions/
#在php.ini中加入扩展
[vi /usr/local/php/etc/php.ini]
extension_dir = "/usr/local/php/extensions/"
extension = "memcache.so"
重新启动php-fpm
$ sudo /usr/local/php/sbin/php-fpm restart
5. 测试
1. 下载memcache: http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
2. 解压安装
$ tar -zxvf memcached-1.4.5.tar.gz
$ cd memcached-1.4.5
$ ./configure
$ make
$ sudo make install
3. 启动
$ sudo /usr/local/bin/memcached -d -c 128 -u root -P /tmp/memcached.pid
4. 安装php-memcache扩展
下载:http://pecl.php.net/get/memcache-2.2.4.tgz
#解压安装
$ tar -zxvf memcache-2.2.4.tgz
$ cd memcache-2.2.4
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
$ make
$ sudo mkdir /usr/local/php/extensions/
$ sudo cp modules/memcache.so /usr/local/php/extensions/
#在php.ini中加入扩展
[vi /usr/local/php/etc/php.ini]
extension_dir = "/usr/local/php/extensions/"
extension = "memcache.so"
重新启动php-fpm
$ sudo /usr/local/php/sbin/php-fpm restart
5. 测试
下载: memcache.php
- <?php
- $mc = new Memcache;
- $mc->connect("127.0.0.1",11211);
- $item = $mc->get('item');
- if(!is_array($item)){
- echo "Add item to memcache";
- $mc->add('item',array('item'));
- }
- $item = $mc->get('item');
- var_dump($item);
- ?>
多刷新几次浏览器,我们会发现,只有第一次出现了Add item to memcache。
文档请参阅:http://php.net/manual/en/book.memcache.php
CentOS下按此步骤安装测试无误。

