在这里让我们一起学习,一起提高!
缓存文件,大家用的最多的估计就是 return array();
实验证明,这种方法是较慢的,试考查以下的3个文件.
其一. $arr.php 直接返回 数组
<?php
return Array
(
"db_host1" => 123,
"db_host2" => 123,
"db_host3" => 123,
"db_host4" => 123,
"db_host5" => 123,
"db_host6" => 123,
"db_host7" => 123,
"db_host8" => 123,
"db_host9" => 123,
"db_host10" => 123,
"db_host11" => 123,
"db_host12" => 123,
"db_host13" => 123,
"db_host14" => 123,
"db_host15" => 123,
"db_host16" => 123
);
?>
其二, ini.php 为INI文件
<?php exit;?>
db_host1 =123
db_host2 =123
db_host3 =123
db_host4 =123
db_host5 =123
db_host6 =123
db_host7 =123
db_host8 =123
db_host9 =123
db_host10 =123
db_host11 =123
db_host12 =123
db_host13 =123
db_host14 =123
db_host15 =123
db_host16 =123
其三,str.php 为序例化后的数组
<?php
return
'a:16:{s:8:"db_host1";s:3:"123";s:8:"db_host2";s:3:"123";s:8:"db_host3";s:3:"123";s:8:"db_host4";s:3:"123";s:8:"db_host5";s:3:"123";s:8:"db_host6";s:3:"123";s:8:"db_host7";s:3:"123";s:8:"db_host8";s:3:"123";s:8:"db_host9";s:3:"123";s:9:"db_host10";s:3:"123";s:9:"db_host11";s:3:"123";s:9:"db_host12";s:3:"123";s:9:"db_host13";s:3:"123";s:9:"db_host14";s:3:"123";s:9:"db_host15";s:3:"123";s:9:"db_host16";s:3:"123";}';
?>
分别对3个文件 进行10000次包含返回数组测试
$t1 = microtime(true);
$file1 = './arr.php';
$file2 = './ini.php';
$file3 = './str.php';
for($i=0; $i<10000; $i++){
//$arr = require $file1;
//$arr = parse_ini_file($file2);
$arr = unserialize(require $file3);
}
$t2 = microtime(true);
echo $t2-$t1;
得出结果:
arr.php 5.7820551395416
ini.php 5.3364160060883
str.php 5.5691919326782
其中,排得最快的居然是 INI文件.
其次是序列化, 最慢的是直接 return 数组
结论:
ini 文件不错,方便书写(可以考虑)
保存数组, 用序列化!
实验证明,这种方法是较慢的,试考查以下的3个文件.
其一. $arr.php 直接返回 数组
<?php
return Array
(
"db_host1" => 123,
"db_host2" => 123,
"db_host3" => 123,
"db_host4" => 123,
"db_host5" => 123,
"db_host6" => 123,
"db_host7" => 123,
"db_host8" => 123,
"db_host9" => 123,
"db_host10" => 123,
"db_host11" => 123,
"db_host12" => 123,
"db_host13" => 123,
"db_host14" => 123,
"db_host15" => 123,
"db_host16" => 123
);
?>
其二, ini.php 为INI文件
<?php exit;?>
db_host1 =123
db_host2 =123
db_host3 =123
db_host4 =123
db_host5 =123
db_host6 =123
db_host7 =123
db_host8 =123
db_host9 =123
db_host10 =123
db_host11 =123
db_host12 =123
db_host13 =123
db_host14 =123
db_host15 =123
db_host16 =123
其三,str.php 为序例化后的数组
<?php
return
'a:16:{s:8:"db_host1";s:3:"123";s:8:"db_host2";s:3:"123";s:8:"db_host3";s:3:"123";s:8:"db_host4";s:3:"123";s:8:"db_host5";s:3:"123";s:8:"db_host6";s:3:"123";s:8:"db_host7";s:3:"123";s:8:"db_host8";s:3:"123";s:8:"db_host9";s:3:"123";s:9:"db_host10";s:3:"123";s:9:"db_host11";s:3:"123";s:9:"db_host12";s:3:"123";s:9:"db_host13";s:3:"123";s:9:"db_host14";s:3:"123";s:9:"db_host15";s:3:"123";s:9:"db_host16";s:3:"123";}';
?>
分别对3个文件 进行10000次包含返回数组测试
$t1 = microtime(true);
$file1 = './arr.php';
$file2 = './ini.php';
$file3 = './str.php';
for($i=0; $i<10000; $i++){
//$arr = require $file1;
//$arr = parse_ini_file($file2);
$arr = unserialize(require $file3);
}
$t2 = microtime(true);
echo $t2-$t1;
得出结果:
arr.php 5.7820551395416
ini.php 5.3364160060883
str.php 5.5691919326782
其中,排得最快的居然是 INI文件.
其次是序列化, 最慢的是直接 return 数组
结论:
ini 文件不错,方便书写(可以考虑)
保存数组, 用序列化!
1、支持PHP4和PHP5
2、增加了对ajax分页的功能
3、优化了整体结构等等。。。
代码如下:
<?
/**
* filename: ext_page.class.php
* @package:phpbean
* @author :feifengxlq<feifengxlq#gmail.com><http://www.phpobject.net/>
* @copyright :Copyright 2006 feifengxlq
* @license:version 2.0
* @create:2006-5-31
* @modify:2006-6-1
* @modify:feifengxlq 2006-11-4
* description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。
* 2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5,
* to see detail,please visit http://www.phpobject.net/blog/read.php?
* example:
* 模式四种分页模式:
require_once('../libs/classes/page.class.php');
$page=new page(array('total'=>1000,'perpage'=>20));
echo 'mode:1<br>'.$page->show();
echo '<hr>mode:2<br>'.$page->show(2);
echo '<hr>mode:3<br>'.$page->show(3);
echo '<hr>mode:4<br>'.$page->show(4);
开启AJAX:
$ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
echo 'mode:1<br>'.$ajaxpage->show();
采用继承自定义分页显示模式:
demo:http://www.phpobject.net/blog
*/
class page
{
/**
* config ,public
*/
var $page_name="PB_page";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
var $next_page='>';//下一页
var $pre_page='<';//上一页
var $first_page='First';//首页
var $last_page='Last';//尾页
var $pre_bar='<<';//上一分页条
var $next_bar='>>';//下一分页条
var $format_left='[';
var $format_right=']';
var $is_ajax=false;//是否支持AJAX分页模式
/**
* private
*
*/
var $pagebarnum=10;//控制记录条的个数。
var $totalpage=0;//总页数
var $ajax_action_name='';//AJAX动作名
var $nowindex=1;//当前页
var $url="";//url地址头
var $offset=0;
/**
* constructor构造函数
*
* @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
*/
function page($array)
{
if(is_array($array)){
if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
$total=intval($array['total']);
$perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
$nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
$url=(array_key_exists('url',$array))?$array['url']:'';
}else{
$total=$array;
$perpage=10;
$nowindex='';
$url='';
}
if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
$this->_set_nowindex($nowindex);//设置当前页
$this->_set_url($url);//设置链接地址
$this->totalpage=ceil($total/$perpage);
$this->offset=($this->nowindex-1)*$this->perpage;
if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
}
/**
* 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
*
* @param string $var
* @param string $value
*/
function set($var,$value)
{
if(in_array($var,get_object_vars($this)))
$this->$var=$value;
else {
$this->error(__FUNCTION__,$var." does not belong to PB_Page!");
}
}
/**
* 打开倒AJAX模式
*
* @param string $action 默认ajax触发的动作。
*/
function open_ajax($action)
{
$this->is_ajax=true;
$this->ajax_action_name=$action;
}
/**
* 获取显示"下一页"的代码
*
* @param string $style
* @return string
*/
function next_page($style='')
{
if($this->nowindex<$this->totalpage){
return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
}
return '<span class="'.$style.'">'.$this->next_page.'</span>';
}
/**
* 获取显示“上一页”的代码
*
* @param string $style
* @return string
*/
function pre_page($style='')
{
if($this->nowindex>1){
return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
}
return '<span class="'.$style.'">'.$this->pre_page.'</span>';
}
/**
* 获取显示“首页”的代码
*
* @return string
*/
function first_page($style='')
{
if($this->nowindex==1){
return '<span class="'.$style.'">'.$this->first_page.'</span>';
}
return $this->_get_link($this->_get_url(1),$this->first_page,$style);
}
/**
* 获取显示“尾页”的代码
*
* @return string
*/
function last_page($style='')
{
if($this->nowindex==$this->totalpage){
return '<span class="'.$style.'">'.$this->last_page.'</span>';
}
return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
}
function nowbar($style='',$nowindex_style='')
{
$plus=ceil($this->pagebarnum/2);
if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
$begin=$this->nowindex-$plus+1;
$begin=($begin>=1)?$begin:1;
$return='';
for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
{
if($i<=$this->totalpage){
if($i!=$this->nowindex)
$return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
else
$return.=$this->_get_text('<span class="'.$nowindex_style.'">'.$i.'</span>');
}else{
break;
}
$return.="\n";
}
unset($begin);
return $return;
}
/**
* 获取显示跳转按钮的代码
*
* @return string
*/
function select()
{
$return='<select name="PB_Page_Select" onchange="window.location.href=\''.$this->url.'\'+this.options[this.selectedIndex].value">';
for($i=1;$i<=$this->totalpage;$i++)
{
if($i==$this->nowindex){
$return.='<option value="'.$i.'" selected>'.$i.'</option>';
}else{
$return.='<option value="'.$i.'">'.$i.'</option>';
}
}
unset($i);
$return.='</select>';
return $return;
}
/**
* 获取mysql 语句中limit需要的值
*
* @return string
*/
function offset()
{
return $this->offset;
}
/**
* 控制分页显示风格(你可以增加相应的风格)
*
* @param int $mode
* @return string
*/
function show($mode=1)
{
switch ($mode)
{
case '1':
$this->next_page='下一页';
$this->pre_page='上一页';
return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
break;
case '2':
$this->next_page='下一页';
$this->pre_page='上一页';
$this->first_page='首页';
$this->last_page='尾页';
return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
break;
case '3':
$this->next_page='下一页';
$this->pre_page='上一页';
$this->first_page='首页';
$this->last_page='尾页';
return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
break;
case '4':
$this->next_page='下一页';
$this->pre_page='上一页';
return $this->pre_page().$this->nowbar().$this->next_page();
break;
case '5':
return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
break;
}
}
/*----------------private function (私有方法)-----------------------------------------------------------*/
/**
* 设置url头地址
* @param: String $url
* @return boolean
*/
function _set_url($url="")
{
if(!empty($url)){
//手动设置
$this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
}else{
//自动获取
if(empty($_SERVER['QUERY_STRING'])){
//不存在QUERY_STRING时
$this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
}else{
//
if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
//地址存在页面参数
$this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
$last=$this->url[strlen($this->url)-1];
if($last=='?'||$last=='&'){
$this->url.=$this->page_name."=";
}else{
$this->url.='&'.$this->page_name."=";
}
}else{
//
$this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
}//end if
}//end if
}//end if
}
/**
* 设置当前页面
*
*/
function _set_nowindex($nowindex)
{
if(empty($nowindex)){
//系统获取
if(isset($_GET[$this->page_name])){
$this->nowindex=intval($_GET[$this->page_name]);
}
}else{
//手动设置
$this->nowindex=intval($nowindex);
}
}
/**
* 为指定的页面返回地址值
*
* @param int $pageno
* @return string $url
*/
function _get_url($pageno=1)
{
return $this->url.$pageno;
}
/**
* 获取分页显示文字,比如说默认情况下_get_text('<a href="">1</a>')将返回[<a href="">1</a>]
*
* @param String $str
* @return string $url
*/
function _get_text($str)
{
return $this->format_left.$str.$this->format_right;
}
/**
* 获取链接地址
*/
function _get_link($url,$text,$style=''){
$style=(empty($style))?'':'class="'.$style.'"';
if($this->is_ajax){
//如果是使用AJAX模式
return '<a '.$style.' href="javascript:'.$this->ajax_action_name.'(\''.$url.'\')">'.$text.'</a>';
}else{
return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
}
}
/**
* 出错处理方式
*/
function error($function,$errormsg)
{
die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg);
}
}
?>
提供一个简单的演示demo
PHP代码如下:
<?
require_once('../libs/classes/page.class.php');
$page=new page(array('total'=>1000,'perpage'=>20));
echo 'mode:1<br>'.$page->show();
echo '<hr>mode:2<br>'.$page->show(2);
echo '<hr>mode:3<br>'.$page->show(3);
echo '<hr>mode:4<br>'.$page->show(4);
echo '<hr>开始AJAX模式:';
$ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
echo 'mode:1<br>'.$ajaxpage->show();
?>
效果图见附件!
提供自定义风格的演示demo一个:
PHP代码如下:
<style type="text/css">
<!--
.pagenavi { text-align:center; font: 11px Arial, tahoma, sans-serif; padding-top: 20px; padding-bottom: 10px; margin: 0px; }
.pagenavi a {border: 1px solid #E2F1AF; background: #FFFFFF; text-decoration: none; color:#C16012; display:inline-block; padding-left:6px; padding-right:6px; padding-top:2px; padding-bottom:2px}
.pagenavi a:visited {border: 1px solid #E2F1AF; background: #FFFFFF; text-decoration: none; padding-left:6px; padding-right:6px; padding-top:2px; padding-bottom:2px}
.pagenavi .break {border: medium none; text-decoration: none; color:#C16012; background:;; padding-left:6px; padding-right:6px; padding-top:2px; padding-bottom:2px}
.pagenavi .num {color:#C16012; font-size:12pt; padding-left:3px; padding-right:3px; padding-top:0; padding-bottom:0}
.pagenavi .curr {padding: 2px 6px; border-color: #999; font-weight: bold; font-size:12pt; background:transparent;}
.pagenavi a:hover {color: #C16012; background: #E2F1AF; text-decoration: none}
-->
</style>
<?
require_once('../libs/classes/page.class.php');
class mypage extends page
{
function mypage($array)
{
parent::page($array);
$this->first_page=1;
$this->last_page=$this->totalpage;
$this->set('format_left','');
$this->set('format_right','');
}
function show()
{
$pagestr='<div class="pagenavi" id="lopage">页:';
$pagestr.=$this->first_page().' ';
$pagestr.=$this->nowbar('','curr');
$pagestr.='<span class="break">...</span>';
$pagestr.=$this->last_page();
$pagestr.=' (总计<span class="num">'.$this->totalpage.'</span>页) </div>';
$pagestr.='</div>';
return $pagestr;
}
}
$page=new mypage(array('total'=>1000,'perpage'=>20));
echo $page->show();
?>
2、增加了对ajax分页的功能
3、优化了整体结构等等。。。
代码如下:
<?
/**
* filename: ext_page.class.php
* @package:phpbean
* @author :feifengxlq<feifengxlq#gmail.com><http://www.phpobject.net/>
* @copyright :Copyright 2006 feifengxlq
* @license:version 2.0
* @create:2006-5-31
* @modify:2006-6-1
* @modify:feifengxlq 2006-11-4
* description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。
* 2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5,
* to see detail,please visit http://www.phpobject.net/blog/read.php?
* example:
* 模式四种分页模式:
require_once('../libs/classes/page.class.php');
$page=new page(array('total'=>1000,'perpage'=>20));
echo 'mode:1<br>'.$page->show();
echo '<hr>mode:2<br>'.$page->show(2);
echo '<hr>mode:3<br>'.$page->show(3);
echo '<hr>mode:4<br>'.$page->show(4);
开启AJAX:
$ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
echo 'mode:1<br>'.$ajaxpage->show();
采用继承自定义分页显示模式:
demo:http://www.phpobject.net/blog
*/
class page
{
/**
* config ,public
*/
var $page_name="PB_page";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
var $next_page='>';//下一页
var $pre_page='<';//上一页
var $first_page='First';//首页
var $last_page='Last';//尾页
var $pre_bar='<<';//上一分页条
var $next_bar='>>';//下一分页条
var $format_left='[';
var $format_right=']';
var $is_ajax=false;//是否支持AJAX分页模式
/**
* private
*
*/
var $pagebarnum=10;//控制记录条的个数。
var $totalpage=0;//总页数
var $ajax_action_name='';//AJAX动作名
var $nowindex=1;//当前页
var $url="";//url地址头
var $offset=0;
/**
* constructor构造函数
*
* @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
*/
function page($array)
{
if(is_array($array)){
if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
$total=intval($array['total']);
$perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
$nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
$url=(array_key_exists('url',$array))?$array['url']:'';
}else{
$total=$array;
$perpage=10;
$nowindex='';
$url='';
}
if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
$this->_set_nowindex($nowindex);//设置当前页
$this->_set_url($url);//设置链接地址
$this->totalpage=ceil($total/$perpage);
$this->offset=($this->nowindex-1)*$this->perpage;
if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
}
/**
* 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
*
* @param string $var
* @param string $value
*/
function set($var,$value)
{
if(in_array($var,get_object_vars($this)))
$this->$var=$value;
else {
$this->error(__FUNCTION__,$var." does not belong to PB_Page!");
}
}
/**
* 打开倒AJAX模式
*
* @param string $action 默认ajax触发的动作。
*/
function open_ajax($action)
{
$this->is_ajax=true;
$this->ajax_action_name=$action;
}
/**
* 获取显示"下一页"的代码
*
* @param string $style
* @return string
*/
function next_page($style='')
{
if($this->nowindex<$this->totalpage){
return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
}
return '<span class="'.$style.'">'.$this->next_page.'</span>';
}
/**
* 获取显示“上一页”的代码
*
* @param string $style
* @return string
*/
function pre_page($style='')
{
if($this->nowindex>1){
return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
}
return '<span class="'.$style.'">'.$this->pre_page.'</span>';
}
/**
* 获取显示“首页”的代码
*
* @return string
*/
function first_page($style='')
{
if($this->nowindex==1){
return '<span class="'.$style.'">'.$this->first_page.'</span>';
}
return $this->_get_link($this->_get_url(1),$this->first_page,$style);
}
/**
* 获取显示“尾页”的代码
*
* @return string
*/
function last_page($style='')
{
if($this->nowindex==$this->totalpage){
return '<span class="'.$style.'">'.$this->last_page.'</span>';
}
return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
}
function nowbar($style='',$nowindex_style='')
{
$plus=ceil($this->pagebarnum/2);
if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
$begin=$this->nowindex-$plus+1;
$begin=($begin>=1)?$begin:1;
$return='';
for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
{
if($i<=$this->totalpage){
if($i!=$this->nowindex)
$return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
else
$return.=$this->_get_text('<span class="'.$nowindex_style.'">'.$i.'</span>');
}else{
break;
}
$return.="\n";
}
unset($begin);
return $return;
}
/**
* 获取显示跳转按钮的代码
*
* @return string
*/
function select()
{
$return='<select name="PB_Page_Select" onchange="window.location.href=\''.$this->url.'\'+this.options[this.selectedIndex].value">';
for($i=1;$i<=$this->totalpage;$i++)
{
if($i==$this->nowindex){
$return.='<option value="'.$i.'" selected>'.$i.'</option>';
}else{
$return.='<option value="'.$i.'">'.$i.'</option>';
}
}
unset($i);
$return.='</select>';
return $return;
}
/**
* 获取mysql 语句中limit需要的值
*
* @return string
*/
function offset()
{
return $this->offset;
}
/**
* 控制分页显示风格(你可以增加相应的风格)
*
* @param int $mode
* @return string
*/
function show($mode=1)
{
switch ($mode)
{
case '1':
$this->next_page='下一页';
$this->pre_page='上一页';
return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
break;
case '2':
$this->next_page='下一页';
$this->pre_page='上一页';
$this->first_page='首页';
$this->last_page='尾页';
return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
break;
case '3':
$this->next_page='下一页';
$this->pre_page='上一页';
$this->first_page='首页';
$this->last_page='尾页';
return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
break;
case '4':
$this->next_page='下一页';
$this->pre_page='上一页';
return $this->pre_page().$this->nowbar().$this->next_page();
break;
case '5':
return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
break;
}
}
/*----------------private function (私有方法)-----------------------------------------------------------*/
/**
* 设置url头地址
* @param: String $url
* @return boolean
*/
function _set_url($url="")
{
if(!empty($url)){
//手动设置
$this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
}else{
//自动获取
if(empty($_SERVER['QUERY_STRING'])){
//不存在QUERY_STRING时
$this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
}else{
//
if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
//地址存在页面参数
$this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
$last=$this->url[strlen($this->url)-1];
if($last=='?'||$last=='&'){
$this->url.=$this->page_name."=";
}else{
$this->url.='&'.$this->page_name."=";
}
}else{
//
$this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
}//end if
}//end if
}//end if
}
/**
* 设置当前页面
*
*/
function _set_nowindex($nowindex)
{
if(empty($nowindex)){
//系统获取
if(isset($_GET[$this->page_name])){
$this->nowindex=intval($_GET[$this->page_name]);
}
}else{
//手动设置
$this->nowindex=intval($nowindex);
}
}
/**
* 为指定的页面返回地址值
*
* @param int $pageno
* @return string $url
*/
function _get_url($pageno=1)
{
return $this->url.$pageno;
}
/**
* 获取分页显示文字,比如说默认情况下_get_text('<a href="">1</a>')将返回[<a href="">1</a>]
*
* @param String $str
* @return string $url
*/
function _get_text($str)
{
return $this->format_left.$str.$this->format_right;
}
/**
* 获取链接地址
*/
function _get_link($url,$text,$style=''){
$style=(empty($style))?'':'class="'.$style.'"';
if($this->is_ajax){
//如果是使用AJAX模式
return '<a '.$style.' href="javascript:'.$this->ajax_action_name.'(\''.$url.'\')">'.$text.'</a>';
}else{
return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
}
}
/**
* 出错处理方式
*/
function error($function,$errormsg)
{
die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg);
}
}
?>
提供一个简单的演示demo
PHP代码如下:
<?
require_once('../libs/classes/page.class.php');
$page=new page(array('total'=>1000,'perpage'=>20));
echo 'mode:1<br>'.$page->show();
echo '<hr>mode:2<br>'.$page->show(2);
echo '<hr>mode:3<br>'.$page->show(3);
echo '<hr>mode:4<br>'.$page->show(4);
echo '<hr>开始AJAX模式:';
$ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
echo 'mode:1<br>'.$ajaxpage->show();
?>
效果图见附件!
提供自定义风格的演示demo一个:
PHP代码如下:
<style type="text/css">
<!--
.pagenavi { text-align:center; font: 11px Arial, tahoma, sans-serif; padding-top: 20px; padding-bottom: 10px; margin: 0px; }
.pagenavi a {border: 1px solid #E2F1AF; background: #FFFFFF; text-decoration: none; color:#C16012; display:inline-block; padding-left:6px; padding-right:6px; padding-top:2px; padding-bottom:2px}
.pagenavi a:visited {border: 1px solid #E2F1AF; background: #FFFFFF; text-decoration: none; padding-left:6px; padding-right:6px; padding-top:2px; padding-bottom:2px}
.pagenavi .break {border: medium none; text-decoration: none; color:#C16012; background:;; padding-left:6px; padding-right:6px; padding-top:2px; padding-bottom:2px}
.pagenavi .num {color:#C16012; font-size:12pt; padding-left:3px; padding-right:3px; padding-top:0; padding-bottom:0}
.pagenavi .curr {padding: 2px 6px; border-color: #999; font-weight: bold; font-size:12pt; background:transparent;}
.pagenavi a:hover {color: #C16012; background: #E2F1AF; text-decoration: none}
-->
</style>
<?
require_once('../libs/classes/page.class.php');
class mypage extends page
{
function mypage($array)
{
parent::page($array);
$this->first_page=1;
$this->last_page=$this->totalpage;
$this->set('format_left','');
$this->set('format_right','');
}
function show()
{
$pagestr='<div class="pagenavi" id="lopage">页:';
$pagestr.=$this->first_page().' ';
$pagestr.=$this->nowbar('','curr');
$pagestr.='<span class="break">...</span>';
$pagestr.=$this->last_page();
$pagestr.=' (总计<span class="num">'.$this->totalpage.'</span>页) </div>';
$pagestr.='</div>';
return $pagestr;
}
}
$page=new mypage(array('total'=>1000,'perpage'=>20));
echo $page->show();
?>
做程序的时候,无论Php还是Asp,缓存的问题很是让人讨厌,虽说缓存是个好东西,但在写程序,调试程序的时候,缓存就显得多余了,尤其是在那无敌的Ie下...
我想大部分页面都会有一个或几个相同的头文件被包含,我们可以在头文件里加入下面的代码解决缓存问题:
Php:header("Cache-Control: no-cache, must-revalidate");
ASP:response.addHeader("Cache-Control","no-cache");
当然,如果只有一个或为数不多的几个页面需要禁止缓存,我们同样可以把上面的代码单独加入到那些需要的页面里面。
我想大部分页面都会有一个或几个相同的头文件被包含,我们可以在头文件里加入下面的代码解决缓存问题:
Php:header("Cache-Control: no-cache, must-revalidate");
ASP:response.addHeader("Cache-Control","no-cache");
当然,如果只有一个或为数不多的几个页面需要禁止缓存,我们同样可以把上面的代码单独加入到那些需要的页面里面。
今天整理商贸程序,其中得需要计算一个字符串中某个字符出现的次数,就上网搜,好不容易找到一个函数substr_count(),
<?php
$text = 'This is a test' ;
echo strlen ( $text ); // 14
echo substr_count ( $text , 'is' ); // 2
// the string is reduced to 's is a test', so it prints 1
echo substr_count ( $text , 'is' , 3 );
// the text is reduced to 's i', so it prints 0
echo substr_count ( $text , 'is' , 3 , 3 );
// generates a warning because 5+10 > 14
echo substr_count ( $text , 'is' , 5 , 10 );
// prints only 1, because it doesn't count overlapped subtrings
$text2 = 'gcdgcdgcd' ;
echo substr_count ( $text2 , 'gcdgcd' );
?>
<?php
$text = 'This is a test' ;
echo strlen ( $text ); // 14
echo substr_count ( $text , 'is' ); // 2
// the string is reduced to 's is a test', so it prints 1
echo substr_count ( $text , 'is' , 3 );
// the text is reduced to 's i', so it prints 0
echo substr_count ( $text , 'is' , 3 , 3 );
// generates a warning because 5+10 > 14
echo substr_count ( $text , 'is' , 5 , 10 );
// prints only 1, because it doesn't count overlapped subtrings
$text2 = 'gcdgcdgcd' ;
echo substr_count ( $text2 , 'gcdgcd' );
?>
终于让服务器每天早上备份一次 MySQL 数据库并自动打包,同时删除 5 天前的备份文件. 分享如下.
1. 环境: windows server 2003 + Apache 2.0 + PHP5 + MySQL 4.0.26 .
2. 假设 PHP 安装目录为 D:/php ,MySQL 安装目录为 D:/mysql.
3. 在 D:/php 下建立目录 WinRAR, 把你 winrar 安装目录下的 WinRAR.exe 和 RARReg.KEY 复制到 D:/php/WinRAR .
4. D:/php 下建立文件 mysql_backup.php:
1. 环境: windows server 2003 + Apache 2.0 + PHP5 + MySQL 4.0.26 .
2. 假设 PHP 安装目录为 D:/php ,MySQL 安装目录为 D:/mysql.
3. 在 D:/php 下建立目录 WinRAR, 把你 winrar 安装目录下的 WinRAR.exe 和 RARReg.KEY 复制到 D:/php/WinRAR .
4. D:/php 下建立文件 mysql_backup.php:
操作系统:RedHat9
软件版本:apache_1.3.28.tar.gz,php-4.3.3.tar.gz,mysql-4.0.15.tar.gz
所有操作都假定在root下执行
1、mysql
1.1创建mysql组和mysql用户
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
1.2安装mysql
tar -xzvf mysql-4.0.15.tar.gz
cd mysql4.0.15
./configure --prefix=/usr/local/mysql -with-charset=gbk -with-extra-charsets=all
make
make install
#cp support-files/my-medium.cnf /etc/my.cnf或者cp /opt/mysql/share/mysql/my-medium.cnf /etc/my.cnf
cd scripts
./mysql_install_db
chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/var
chown -R mysql /usr/local/mysql/var/
chown -R mysql /usr/local/mysql/var/.
chown -R mysql /usr/local/mysql/var/mysql
chown -R mysql /usr/local/mysql/var/mysql/
chgrp -R mysql /usr/local/mysql
向/etc/ld.so.conf中加入两行:
/usr/local/mysql/lib/mysql
/usr/local/lib
载入库,执行
软件版本:apache_1.3.28.tar.gz,php-4.3.3.tar.gz,mysql-4.0.15.tar.gz
所有操作都假定在root下执行
1、mysql
1.1创建mysql组和mysql用户
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
1.2安装mysql
tar -xzvf mysql-4.0.15.tar.gz
cd mysql4.0.15
./configure --prefix=/usr/local/mysql -with-charset=gbk -with-extra-charsets=all
make
make install
#cp support-files/my-medium.cnf /etc/my.cnf或者cp /opt/mysql/share/mysql/my-medium.cnf /etc/my.cnf
cd scripts
./mysql_install_db
chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/var
chown -R mysql /usr/local/mysql/var/
chown -R mysql /usr/local/mysql/var/.
chown -R mysql /usr/local/mysql/var/mysql
chown -R mysql /usr/local/mysql/var/mysql/
chgrp -R mysql /usr/local/mysql
向/etc/ld.so.conf中加入两行:
/usr/local/mysql/lib/mysql
/usr/local/lib
载入库,执行
每一个电脑爱好者都希望有自己的网站,其实自己拥有一台电脑和一个IP就可以解决了。现在许多人都听说过Linux,却很少有人用过它。我刚刚接触Linux,想把自己的安装过程与大家共同交流一下。
我安装系统的是Linux readhat8.0,配置是Php-4.0.4+Mysql-3.23.46+Apache-1.3.27。
1、 插入Linux 8.0的光盘,出现欢迎安装的界面,然后会让你选择安装过程的语言,一般默认为English(English)就行。对于初次安装最好选择汉语,这样容易安装。但是对于使用linux的用户来说,最好习惯用英语安装,因为linux有的版本不支持汉语并且好多情况下不能用汉语。
2、 之后会让你选择键盘语言、鼠标语言,都选择默认,直接点击“Next”就行了。
我安装系统的是Linux readhat8.0,配置是Php-4.0.4+Mysql-3.23.46+Apache-1.3.27。
(一) Linux系统的安装
1、 插入Linux 8.0的光盘,出现欢迎安装的界面,然后会让你选择安装过程的语言,一般默认为English(English)就行。对于初次安装最好选择汉语,这样容易安装。但是对于使用linux的用户来说,最好习惯用英语安装,因为linux有的版本不支持汉语并且好多情况下不能用汉语。
2、 之后会让你选择键盘语言、鼠标语言,都选择默认,直接点击“Next”就行了。
经过实验发现,PHP4的session只对当前打开的浏览器窗口有效(至少在W2K下的IE5和NS6中是这样)。
实验步骤:
首先我们来建立两个文件。
test1.php
session_start();
session_register("count");
echo $count=0;
?>
实验步骤:
首先我们来建立两个文件。
test1.php
session_start();
session_register("count");
echo $count=0;
?>
在一般的应用中,PHP的程序员会比较少的注意到php程序的细微算法对性能影响的问题,但在处理一些比较占用资源的东西的时候,适当调整算法,会让程序的性能整倍的提高。
这几天,我在研究分词的算法,如果不预先载入词典的话,那种运算速度简直惨不忍睹,因此我考虑了预先载入词典在一个数组中的做法,我手上有一个十多万词汇的词典,开始的时候,用这样的方式载入:
这几天,我在研究分词的算法,如果不预先载入词典的话,那种运算速度简直惨不忍睹,因此我考虑了预先载入词典在一个数组中的做法,我手上有一个十多万词汇的词典,开始的时候,用这样的方式载入:
在php中,下载http资源比较常用的一种做法就是用 file_get_contents 这个函数,但这函数无法获得http头的信息,会给一些应用带来不方便,因此我写了一个http下载类来解决这个问题,测试感觉速度和file_get_contents相差无几。