日志列表

RSS

php 实现全排列

[ 2010年04月20日 19:00 | by Veidy.lei  ] 评论(2) » | 阅读次数:1359
< ?php
header('Content-Type: text/html; charset=UTF-8');
class quanpailie
{
	var $startTime = 0;
	var $stopTime = 0;
 
    function get_microtime()
    {
        list($usec, $sec) = explode(' ', microtime());
        return ((float)$usec + (float)$sec);
    }
	function start()
    {
        $this->startTime = $this->get_microtime();
    }
	function stop()
	{
		$this->stopTime = $this->get_microtime();
	}
	function spent()
	{
		return round(($this->stopTime - $this->startTime) * 1000, 3);
	}
	function type($list, $k, $m)
	{
		if($k==$m)
		{
			for($i=0; $i< =$m; $i++)
			{
				echo  $list[$i];
			}
			echo "\n";
		}
		else
		{
			for($i=$k; $i<=$m; $i++)
			{
				$this->swap($list[$k], $list[$i]);
				$this->type($list, $k+1, $m);
				$this->swap($list[$k], $list[$i]);
			}
		}
	}
	public function swap(&$a, &$b)
	{
		$temp=$a;
		$a=$b;
		$b=$temp;
	}
}
$aa=new quanpailie();
$aa->start();
$str = 'abcde';
$aa->type(str_split($str), 0, 2);
$aa->stop();
echo "<br />页面执行时间: ". $aa->spent() . " 毫秒";
?>
Filed under: php | Tags : ,

Ubuntu 学习日志 ubuntu9.04 安装apache php mysql

[ 2009年06月23日 10:08 | by Veidy.lei  ] 评论(2) » | 阅读次数:1180

1、在终端中执行命令sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server,期间会提示你设置Mysql root用户的密码。
2、安装完成后,在浏览器中输入http://127.0.0.1.如果出现it’s works 表示apache已经安装成功。在/var/www下建立phpinfo.php文件,并在文件中添加如下内容并保存:

< ?php
phpinfo();
?>

在浏览器中输入http://你的ip/phpinfo.php测试你的PHP是否安装成功。
如果不能没有显示phpinfo页面,以管理员身份打开文件/etc/apache2/httpd.conf,在文件中添加一下两行内容并保存:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
目的是使Apache支持PHP文件,然后在终端中执行命令/etc/init.d/apache2 restart 重启Apache,就OK了。
附MySQL修改用户密码命令:
set password for root@localhost=password(‘123456′);

Filed under: Linux, mysql, php | Tags : , ,

php 查询 mysql时区分大小写

[ 2009年03月31日 19:44 | by Veidy.lei  ] 没有评论 » | 阅读次数:1177

如果想让php在查询时能区分大小写可以在字段前面加一个字段 ‘binary’
如:我想模糊查询标题中包含 ’pHp‘ 这个单词的记录可以这样来写
select title from table where binary title like ‘%pHp%’
如果写成
select title from table where binary title like ‘%php%’
将查询不到
这样在查询的时候就会按照大小写来进行搜索
当然你也可以修改数据库字段的类型为 ‘binary’
这样你所有的查询都将以区分大小写来查询

Filed under: mysql, php | Tags : , , ,

php中utf-8编码和GB2312编码 正则表达式汉字匹配

[ 2009年03月29日 14:18 | by Veidy.lei  ] 没有评论 » | 阅读次数:1181

在javascript中,要判断字符串是中文是很简单的。比如:

var str = 'php编程';
if (/^[u4e00-u9fa5]+$/.test(str))
{
    alert('该字符串全部是中文');
} 
else
{
    alert('该字符串不全部是中文');
}

想当然的,在php中来判断字符串是否为中文,就会沿袭这个思路:
不过,很快就会发现,php并不支持这样的表达,报错:
Warning: preg_match() [
function.preg-match
]: Compilation failed: PCRE does not support L, l, N, U, or u at offset 3 in test.php on line 3
,想从php正则表达式对于十六进制数据的表达方式上进行突破,发现在php中,是用x表示十六进制数据的。于是,变换成如下的代码:

$str = 'php编程';
if (preg_match("/^[x4e00-x9fa5]+$/", $str)) 
{
    echo '该字符串全部是中文';
}
else 
{
    echo '该字符串不全部是中文';
}

貌似不报错了,判断的结果也正确,不过把$str换成“编程”两字,结果却还是显示“该字符串不全部是中文”,看来这样的判断还是不够准确。

模式修正符: u
u (PCRE_UTF8)
此修正符启用了一个 PCRE 中与 Perl 不兼容的额外功能。模式字符串被当成 UTF-8。本修正符在 Unix 下自 PHP 4.1.0 起可用,在 win32 下自 PHP 4.2.3 起可用。
例子:
preg_match(‘/[x{2460}-x{2468}]/u’, $str); 匹配 内码汉字
按照他提供的方式进行测试,代码如下:

$str = 'php编程';
if (preg_match("/^[x{2460}-x{2468}]+$/u", $str)) 
{
    echo '该字符串全部是中文';
} 
else 
{
    echo '该字符串不全部是中文';
}

发现这次依然对是否为中文判断失常。不过,既然x表示的十六进制数据,为什么和js里边提供的范围x4e00-x9fa5不一样呢?于是我就换成了下边的代码:

$str = 'php编程';
if (preg_match("/^[x4e00-x9fa5]+$/u", $str)) 
{
    echo '该字符串全部是中文';
} 
else 
{
    echo '该字符串不全部是中文';
}

本来以为铁定成功了的事情,没想到,warning又一次产生了:
Warning: preg_match() [
function.preg-match
]: Compilation failed: invalid UTF-8 string at offset 6 in test.php on line 3
看来又有错误的表达方式了,于是对照了一下那篇文章的表达方式,给“4e00”和“9fa5”两边分别用”{“和“}”包起来,跑了一遍,发现真的准确了:

$str = 'php编程';
if (preg_match("/^[x{4e00}-x{9fa5}]+$/u", $str)) 
{
    echo '该字符串全部是中文';
} 
else 
{
    echo '该字符串不全部是中文';
}
Filed under: javascript, php | Tags : , , ,

关于php的引用

[ 2009年03月27日 08:52 | by Veidy.lei  ] 没有评论 » | 阅读次数:1168

我们先来看一段代码

$str = 'aaaaa';
$a = &$str;
$str = 'zzz';
unset($str);
echo $a;

这个代码一看觉得$a的值为null,但实际值为zzz
仔细看了手册后发现
当 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了
看到这段后就不难理解了,这段代码unset掉的只是断开了str与其值间的绑定而已。

Filed under: php | Tags : ,

php 不使用第三方变量和数组交换两个变量的值

[ 2009年03月11日 08:22 | by Veidy.lei  ] 评论(2) » | 阅读次数:1298

今天去面试有一道题,怎样不用第三方变量和数组交换二个变量的值,在那里想了半天也没想出么样实现-_-!!,回来后查了下资料,可以用位运算中的异或运算。
下面是试例代码

<?php
$a = 'str1';
$b = 'str2';
 
echo "交换前:\$a = {$a}; \$b = {$b};";
$b = $b ^ $a;
$a = $b ^ $a;
$b = $b ^ $a;
echo "交换后:\$a = {$a}; \$b = {$b};";
?>

此算法能够实现是由异或运算的特点决定的,通过异或运算能够使数据中的某些位翻转,其他位不变。这就意味着任意一个数与任意一个给定的值连续异或两次,值不变。

Filed under: php | Tags : , ,

PHP使用技巧

[ 2009年03月04日 18:12 | by Veidy.lei  ] 没有评论 » | 阅读次数:1161

静态调用的成员一定要定义成 static (PHP5 ONLY)
贴士:PHP 5 引入了静态成员的概念,作用和 PHP 4 的函数内部静态变量一致,但前者是作为类的成员来使用。静态变量和 Ruby 的类变量(class variable)差不多,所有类的实例共享同一个静态变量。

<?php
class foo 
{
     function bar() 
     {
          echo 'foobar';
     }
}
 
$foo = new foo;
 
// instance way
$foo->bar();
 
// static way
foo::bar();
?>

静态地调用非 static 成员,效率会比静态地调用 static 成员慢 50-60%。主要是因为前者会产生 E_STRICT 警告,内部也需要做转换。

使用类常量 (PHP5 ONLY)

贴士:PHP 5 新功能,类似于 C++ 的 const。

使用类常量的好处是:

- 编译时解析,没有额外开销
- 杂凑表更小,所以内部查找更快
- 类常量仅存在于特定「命名空间」,所以杂凑名更短
- 代码更干净,使除错更方便

(暂时)不要使用 require/include_once

require/include_once 每次被调用的时候都会打开目标文件!

- 如果用绝对路径的话,PHP 5.2/6.0 不存在这个问题
- 新版的 APC 缓存系统已经解决这个问题

文件 I/O 增加 => 效率降低

如果需要,可以自行检查文件是否已被 require/include。

不要调用毫无意义的函数

有对应的常量的时候,不要使用函数。

<?php
php_uname('s') == PHP_OS;
php_version() == PHP_VERSION;
php_sapi_name() == PHP_SAPI;
?>

虽然使用不多,但是效率提升大概在 3500% 左右。

最快的 Win32 检查

<?php
$is_win = DIRECTORY_SEPARATOR == '\\';
?>

- 不用函数
- Win98/NT/2000/XP/Vista/Longhorn/Shorthorn/Whistler…通用
- 一直可用

时间问题 (PHP>5.1.0 ONLY)

你如何在你的软件中得知现在的时间?简单,「time() time() again, you ask me…」。

不过总归会调用函数,慢。

现在好了,用 $_SERVER['REQUEST_TIME'],不用调用函数,又省了。

加速 PCRE

- 对于不用保存的结果,不用 (),一律用 (?:)

这样 PHP 不用为符合的内容分配内存,省。效率提升 15% 左右。

- 能不用正则,就不用正则,在分析的时候仔细阅读手册「字符串函数」部分。有没有你漏掉的好用的函数?

例如:

strpbrk()
strncasecmp()
strpos()/strrpos()/stripos()/strripos()

加速 strtr

如果需要转换的全是单个字符的时候,用字符串而不是数组来做 strtr:

<?php
$addr = strtr($addr, "abcd", "efgh"); // good
$addr = strtr($addr, array('a' => 'e',
                           // ...
                           )); // bad
?>

效率提升:10 倍。

不要做无谓的替换

即使没有替换,str_replace 也会为其参数分配内存。很慢!解决办法:

- 用 strpos 先查找(非常快),看是否需要替换,如果需要,再替换

效率:

- 如果需要替换:效率几乎相等,差别在 0.1% 左右。
- 如果不需要替换:用 strpos 快 200%。

邪恶的 @ 操作符

不要滥用 @ 操作符。虽然 @ 看上去很简单,但是实际上后台有很多操作。用 @ 比起不用 @,效率差距:3 倍。
特别不要在循环中使用 @,在 5 次循环的测试中,即使是先用 error_reporting(0) 关掉错误,在循环完成后再打开,都比用 @ 快。

善用 strncmp

当需要对比「前 n 个字符」是否一样的时候,用 strncmp/strncasecmp,而不是 substr/strtolower,更不是 PCRE,更千万别提 ereg。strncmp/strncasecmp 效率最高(虽然高得不多)。

慎用 substr_compare (PHP5 ONLY)

按照上面的道理,substr_compare 应该比先 substr 再比较快咯。答案是否定的,除非:

- 无视大小写的比较
- 比较较大的字符串

不要用常量代替字符串

为什么:

- 需要查询杂凑表两次
- 需要把常量名转换为小写(进行第二次查询的时候)
- 生成 E_NOTICE 警告
- 会建立临时字符串

效率差别:700%。

不要把 count/strlen/sizeof 放到 for 循环的条件语句中

贴士:我的个人做法

<?php
for ($i = 0, $max = count($array);$i < $max; ++$i);
?>
效率提升相对于:
 
- count 50%
- strlen 75%
 
<strong>短的代码不一定快 </strong>
<pre lang="php">
<?php
// longest
if ($a == $b) {
    $str .= $a;
} else {
    $str .= $b;
}
 
// longer
if ($a == $b) {
    $str .= $a;
}
$str .= $b;
 
// short
$str .= ($a == $b ? $a : $b);
?>

你觉得哪个快?

效率比较:

- longest: 4.27
- longer: 4.43
- short: 4.76

不可思议?再来一个:

<?php
// original
$d = dir('.');
while (($entry = $d->read()) !== false) {
    if ($entry == '.' || $entry == '..') {
        continue;
    }
}
 
// versus
glob('./*');
 
// versus (include . and ..)
scandir('.');
?>

哪个快?

效率比较:

- original: 3.37
- glob: 6.28
- scandir: 3.42
- original without OO: 3.14
- SPL (PHP5): 3.95

画外音:从此也可以看出来 PHP5 的面向对象效率提高了很多,效率已经和纯函数差得不太多了。

提高 PHP 文件访问效率

需要包含其他 PHP 文件的时候,使用完整路径,或者容易转换的相对路径。

<?php
include 'file.php'; // bad approach
incldue './file.php'; // good
include '/path/to/file.php'; // ideal
?>

物尽其用
PHP 有很多扩展和函数可用,在实现一个功能的之前,应该看看 PHP 是否有了这个功能?是否有更简单的实现?

<?php
$filename = "./somepic.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
 
// vs. much simpler
 
file_get_contents('./somepic.gif');
?>

关于引用的技巧

引用可以:

- 简化对复杂结构数据的访问
- 优化内存使用

<?php
$a['b']['c'] = array();
 
// slow 2 extra hash lookups per access
for ($i = 0; $i < 5; ++$i)
    $a['b']['c'][$i] = $i;
 
// much faster reference based approach
$ref =& $a['b']['c'];
for ($i = 0; $i < 5; ++$i)
    $ref[$i] = $i;
?>
 
<?php
$a = 'large string';
 
// memory intensive approach
function a($str)
{
    return $str.'something';
}
 
// more efficient solution
function a(&$str)
{
    $str .= 'something';
}
?>

==============================================
参考资料

http://ilia.ws

Ilia 的个人网站,Blog,他参与的开发以及出版的一些稿物链接等等。

http://ez.no

eZ components 官方网站,eZ comp 是针对 PHP5 的开源通用库,以效率为己任,Ilia 也参与了开发。

http://phparch.com

php|architect,不错的 php 出版商/培训组织。买不起或者买不到的话,网上可以下到很多经典的盗版。

http://talks.php.net

PHP 会议上的演讲合集,现在还不是很丰富,不过内容都是让人一看就容易废寝忘食的好东东,推荐早上睡眼朦胧的时候或者吃完午饭仔细研究,否则你会忘记吃饭和睡觉的!

==============================================

Filed under: php | Tags : ,

php 分页类

[ 2009年02月13日 05:49 | by Veidy.lei  ] 没有评论 » | 阅读次数:1172

一个比较好用的PHP分页类

/**
 * 分页类
 *
 * @package pager
 * @author Veidy.lei<veidy .lei@gmail.com>
 * @version $Id: pager.php,v 1.5 2008/03/26 09:26:39 Veidy.lei Exp $
 * @copyright (c) 2008 Veidy.lei Group
 */
 
class pager
{
	var $page_num_per_group = 3;  //页码每组页码数
	var $total_item = 0;  //总记录数
	var $per_page_num = 10;  //每页记录数
	var $first_page_num = 1;  //首页页码数
	var $pre_page_num = 0;  //上页页码数
	var $next_page_num = 0;  //下页页码数
	var $last_page_num = 0;  //尾页页码数
	var $pre_group_page_num = 0;  //上一组的显示页码数
	var $next_group_page_num = 0;  //下一组的显示页码数
	var $cur_group_page = array(); //当前组的页面页码数列表
	var $cur_page = 1;  //当前页码数
	var $cur_group = 0; //当前组
	var $total_group = 0; //总组数
	var $page_num = array();  //所有页码
	var $total_page = 0;  //总页数
	var $cur_item_num = 0;  //当前页记录条数
 
	function pager($total_item = 0, $per_page_num = 10, $page_num_per_group = 3)
	{
		$this-&gt;cur_page = (int)request_var('page', 1); //得到当前页码数
		$this-&gt;per_page_num = ($per_page_num &gt; 0) ? ceil($per_page_num) : 10; //得到每页记录数
		$this-&gt;page_num_per_group = ($page_num_per_group &gt; 0) ? ceil($page_num_per_group) : 3; //得到页码每组页码数
 
		if ($total_item &lt; 1)
		{
			$this-&gt;page_num = false; //即无分页
		}
		else
		{
			$this-&gt;total_item = $total_item;
 
			$this-&gt;total_page = ceil($this-&gt;total_item / $this-&gt;per_page_num); //得到总页数
			$this-&gt;last_page_num = $this-&gt;total_page; //得到尾页页码数
 
			if ($this-&gt;cur_page &lt; 1)
			{
				$this-&gt;cur_page = 1; //得到当前页码数
			}
 
			if ($this-&gt;cur_page &gt; $this-&gt;total_page)
			{
				$this-&gt;cur_page = $this-&gt;total_page; //得到当前页码数
			}
 
			$this-&gt;pre_page_num = ($this-&gt;cur_page &gt; 1) ? $this-&gt;cur_page - 1 : 1; //得到上页页码数
			$this-&gt;next_page_num = ($this-&gt;cur_page &lt; $this-&gt;total_page) ? $this-&gt;cur_page + 1 : $this-&gt;total_page; //得到下页页码数
 
			$this-&gt;total_group = ceil($this-&gt;total_page / $this-&gt;page_num_per_group); //得到总组数
			$this-&gt;cur_group = ceil($this-&gt;cur_page / $this-&gt;page_num_per_group); //得到当前组数
 
			if ($this-&gt;total_group &gt; 1)
			{
				if ($this-&gt;cur_group &gt; 1) //当前组不是第一组
				{
					$this-&gt;pre_group_page_num = ($this-&gt;cur_group - 1) * $this-&gt;page_num_per_group;  //得到上一组的显示页码数
				}
 
				if ($this-&gt;cur_group &lt; $this-&gt;total_group)  //当前组不是最后一组
				{
					$this-&gt;next_group_page_num = ($this-&gt;cur_group * $this-&gt;page_num_per_group) + 1;  //得到下一组的显示页码数
				}
			}
 
			$this-&gt;cur_group_page[0] = (($this-&gt;cur_group - 1) * $this-&gt;page_num_per_group) + 1; //当前组第一页
			for ($i = 1; $i &lt; $this-&gt;page_num_per_group; $i++)
			{
				if ($this-&gt;cur_group_page[0] &lt;= ($this-&gt;total_page - $i))
				{
					$this-&gt;cur_group_page[$i] = $this-&gt;cur_group_page[0] + $i; //当前组其它页
				}
			}
 
			if ($this-&gt;cur_page == $this-&gt;total_page)
			{
				$this-&gt;cur_item_num = $this-&gt;total_item - (($this-&gt;total_page -1) * $this-&gt;per_page_num);
			}
			else
			{
				$this-&gt;cur_item_num = $this-&gt;per_page_num;
			}
 
			$this-&gt;page_num = array();
			$this-&gt;page_num['total_item'] = $this-&gt;total_item;
			$this-&gt;page_num['page_num_per_group'] = $this-&gt;page_num_per_group;
			$this-&gt;page_num['per_page_num'] = $this-&gt;per_page_num;
			$this-&gt;page_num['first_page_num'] = $this-&gt;first_page_num;
			$this-&gt;page_num['pre_page_num'] = $this-&gt;pre_page_num;
			$this-&gt;page_num['next_page_num'] = $this-&gt;next_page_num;
			$this-&gt;page_num['last_page_num'] = $this-&gt;last_page_num;
			$this-&gt;page_num['pre_group_page_num'] = $this-&gt;pre_group_page_num;
			$this-&gt;page_num['next_group_page_num'] = $this-&gt;next_group_page_num;
			$this-&gt;page_num['cur_group_page'] = $this-&gt;cur_group_page;
			$this-&gt;page_num['cur_page'] = $this-&gt;cur_page;
			$this-&gt;page_num['cur_group'] = $this-&gt;cur_group;
			$this-&gt;page_num['total_group'] = $this-&gt;total_group;
			$this-&gt;page_num['total_page'] = $this-&gt;total_page;
			$this-&gt;page_num['cur_item_num'] = $this-&gt;cur_item_num;
		}
	}
 
	/**
	 * 获取各相关页码数
	 *
	 * @return bool 或 array ,如果是false表示无记录,如果是数组,返回值如下:
	 * 		$this-&gt;page_num['total_item']--------------------------------总记录条数
			$this-&gt;page_num['page_num_per_group']------------------------每组显示的页码数
			$this-&gt;page_num['per_page_num']------------------------------每页显示的记录条数
			$this-&gt;page_num['first_page_num']----------------------------第一页页码
			$this-&gt;page_num['pre_page_num']------------------------------上一页页码
			$this-&gt;page_num['next_page_num']-----------------------------下一页页码
			$this-&gt;page_num['last_page_num']-----------------------------最后一页码
			$this-&gt;page_num['pre_group_page_num']------------------------上一组显示的页码
			$this-&gt;page_num['next_group_page_num']-----------------------下一组显示的页码
			$this-&gt;page_num['cur_group_page']----------------------------当前组的页码列表,是一个数组
			$this-&gt;page_num['cur_page']----------------------------------当前页码
			$this-&gt;page_num['cur_group']---------------------------------当前组
			$this-&gt;page_num['total_group']-------------------------------总组数
			$this-&gt;page_num['total_page']--------------------------------总页数
			$this-&gt;page_num['cur_item_num']------------------------------当前页的记录条数
	 */
	function get_page_num()
	{
		return $this-&gt;page_num;
	}
 
	function get_cur_page()
	{
		return $this-&gt;cur_page;
	}
}
 
class pager_str extends pager
{
	//设定
	var $total_item_str = "  共%s条 "; //共多少条,如果为空表示不要此项
	var $cur_item_str = "当前页%s条 "; //当前页多少条,如果为空表示不要此项
	var $tj_cur_page_str = "%s/"; //统计段当前页码,如果为空表示不要此项
	var $total_page_str = "%s页 "; //总页数,如果为空表示不要此项
 
	var $first_page_str = "<a class="text_link_class" href="%s">首页</a> "; //第一页,如果为空表示不要此项
	var $first_page_str2 = "首页 "; //第一页,没有链接的情况,如果为空表示不要此项
	var $pre_page_str = "<a class="text_link_class" href="%s">上页</a> "; //上一页,如果为空表示不要此项
	var $pre_page_str2 = "上页 "; //上一页,没有链接的情况,如果为空表示不要此项
	var $next_page_str = " <a class="text_link_class" href="%s">下页</a> "; //下一页,如果为空表示不要此项
	var $next_page_str2 = " 下页 "; //下一页,没有链接的情况,如果为空表示不要此项
	var $last_page_str = "<a class="text_link_class" href="%s">尾页</a> "; //最后一页,如果为空表示不要此项
	var $last_page_str2 = "尾页 "; //最后一页,没有链接的情况,如果为空表示不要此项
 
	var $go_text_str = "
<input name="page" size="3" type="text" value="%s" /> "; //跳转框,如果为空表示不要跳转
	var $go_submit_str = "
<input name="go" type="submit" value="GO" />  "; //跳转按钮
 
	var $group_split_str = ' ... '; //分页组的分割符,如果为空表示不用显示页数
	var $page_split_str = ' '; //页码之间的分割符
	var $cur_page_str = "<span class="cur_page_class">%s</span>"; //当前页页码
	var $not_cur_page_str = "<a class="num_link_class" href="%s">%s</a>"; //非当前页页码
 
	var $url_start = ''; //网址头
	var $url_end = ''; //网址尾
 
	var $style_table = 'border="0" cellpadding="0" cellspacing="0" style="border:0px;"'; //表格样式
	var $style_td = 'align="left" style="border-bottom:0px; border-top:0px;"'; //单元格样式
 
	var $page_str = ''; //将要输出的分页内容
 
	function pager_str($options = array())
	{
		global $mod, $do;
 
		foreach ( $options as $k=&gt;$v )
		{
			$this-&gt;$k = $v;
		}
 
		/*
		if ($this-&gt;url_start == '')
		{
		$this-&gt;url_start = '/' . $mod . '/';
		}
 
		if (substr($this-&gt;url_start, -1) != '/') //在最后加上'/'
		{
		$this-&gt;url_start .= '/';
		}
 
		if ($this-&gt;url_end == '')
		{
		$this-&gt;url_end = $do . '.html';
		}
 
		if (substr($this-&gt;url_end, 0, 1) == '/') //去掉开头的'/'
		{
		$this-&gt;url_end = substr($this-&gt;url_end, 0, -1);
		}
		*/
 
		//$this-&gt;url_start = '/' . $mod . '/;
		$this-&gt;url_start = '?';
		$url_arr = array();
		if (isset($_POST) &amp;&amp; sizeof($_POST) &amp;&amp; isset($_GET) &amp;&amp; sizeof($_GET))
		{
			$url_arr = array_merge($_POST, $_GET);
		}
		else if (isset($_POST) &amp;&amp; sizeof($_POST))
		{
			$url_arr = $_POST;
		}
		else if (isset($_GET) &amp;&amp; sizeof($_GET))
		{
			$url_arr = $_GET;
		}
 
		if (sizeof($url_arr))
		{
			foreach ($url_arr as $k =&gt; $v)
			{
				if ($k != 'page' &amp;&amp; $k != 'go' &amp;&amp; $k != 'mod' &amp;&amp; $k != 'do')
				{
					if ($k == 'key') {
						$this-&gt;url_start .= trim($k) . '=' . replace_str_for_url($v) . '&amp;';
					}
					else {
						$this-&gt;url_start .= trim($k) . '=' . $v . '&amp;';
					}
				}
			}
		}
 
		$this-&gt;pager($this-&gt;total_item, $this-&gt;per_page_num, $this-&gt;page_num_per_group);
 
		if ($this-&gt;page_num)
		{
			$this-&gt;page_str = '
<table border="0">style_table . '&gt;';
			$this-&gt;page_str .= ($this-&gt;go_text_str != '') ? '
<form id="page_go" action="' . $this-&gt;url_start . $this-&gt;url_end . '" enctype="application/x-www-form-urlencoded" method="post">' : '';
			$this-&gt;page_str .= '
</form>
<tbody>
<tr>
<td>style_td . '&gt;'; $this-&gt;page_str .= ($this-&gt;total_item_str != '') ? sprintf($this-&gt;total_item_str, $this-&gt;total_item) : ''; $this-&gt;page_str .= ($this-&gt;cur_item_str != '') ? sprintf($this-&gt;cur_item_str, $this-&gt;cur_item_num) : ''; $this-&gt;page_str .= ($this-&gt;tj_cur_page_str != '') ? sprintf($this-&gt;tj_cur_page_str, $this-&gt;cur_page) : ''; $this-&gt;page_str .= ($this-&gt;total_page_str != '') ? sprintf($this-&gt;total_page_str, $this-&gt;total_page) : ''; if ($this-&gt;cur_page == 1) { $this-&gt;page_str .= $this-&gt;first_page_str2 . $this-&gt;pre_page_str2; } else { $this-&gt;page_str .= ($this-&gt;first_page_str != '') ? sprintf($this-&gt;first_page_str, $this-&gt;url_start . 'page=' . $this-&gt;first_page_num) : ''; $this-&gt;page_str .= ($this-&gt;pre_page_str != '') ? sprintf($this-&gt;pre_page_str, $this-&gt;url_start . 'page=' . $this-&gt;pre_page_num) : ''; } if ($this-&gt;group_split_str != '') { if ($this-&gt;pre_group_page_num) { $this-&gt;page_str .= ($this-&gt;not_cur_page_str != '') ? sprintf($this-&gt;not_cur_page_str, $this-&gt;url_start . 'page=' . $this-&gt;pre_group_page_num, $this-&gt;pre_group_page_num) . $this-&gt;group_split_str : ''; } foreach ($this-&gt;cur_group_page as $k =&gt; $v) { if ($this-&gt;cur_page == $v) { $this-&gt;page_str .= ($this-&gt;cur_page_str != '') ? sprintf($this-&gt;cur_page_str, $v) : ''; } else { $this-&gt;page_str .= ($this-&gt;not_cur_page_str != '') ? sprintf($this-&gt;not_cur_page_str, $this-&gt;url_start . 'page=' . $v, $v) : ''; } $this-&gt;page_str .= $this-&gt;page_split_str; } if ($this-&gt;next_group_page_num) { $this-&gt;page_str .= ($this-&gt;not_cur_page_str != '') ? $this-&gt;group_split_str . sprintf($this-&gt;not_cur_page_str, $this-&gt;url_start . 'page=' . $this-&gt;next_group_page_num, $this-&gt;next_group_page_num) : ''; } } if ($this-&gt;cur_page == $this-&gt;total_page) { $this-&gt;page_str .= $this-&gt;next_page_str2 . $this-&gt;last_page_str2; } else { $this-&gt;page_str .= ($this-&gt;next_page_str != '') ? sprintf($this-&gt;next_page_str, $this-&gt;url_start . 'page=' . $this-&gt;next_page_num) : ''; $this-&gt;page_str .= ($this-&gt;last_page_str != '') ? sprintf($this-&gt;last_page_str, $this-&gt;url_start . 'page=' . $this-&gt;last_page_num) : ''; } if ($this-&gt;go_text_str != '') { $this-&gt;page_str .= sprintf($this-&gt;go_text_str, $this-&gt;cur_page) . $this-&gt;go_submit_str; } $this-&gt;page_str .= '</td>
</tr>
';
			$this-&gt;page_str .= ($this-&gt;go_text_str != '') ? '</tbody>
' : '';
			$this-&gt;page_str .= '</table>
';
		}
		else
		{
			$this-&gt;page_str = '';
		}
	}
 
	function get_page_str()
	{
		return $this-&gt;page_str;
	}
}</veidy>
Filed under: php | Tags : ,
分页 1 / 11