本文章收集了在php中一些常常用到的正则表达式规则总结,有需要的朋友可参考。
匹配特定数字:
代码如下 |
复制代码 |
^[1-9]d*$ //匹配正整数
^-[1-9]d*$ //匹配负整数
^-?[1-9]d*$ //匹配整数
^[1-9]d*|0$ //匹配非负整数(正整数 + 0)
^-[1-9]d*|0$ //匹配非正整数(负整数 + 0)
^[1-9]d*.d*|0.d*[1-9]d*$ //匹配正浮点数
^-([1-9]d*.d*|0.d*[1-9]d*)$ //匹配负浮点数
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ //匹配浮点数
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$ //匹配非正浮点数(负浮点数 + 0)
|
评注:处理大量数据时有用,具体应用时注意修正
匹配特定字符串:
代码如下 |
复制代码 |
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式
|
匹配中文字符的正则表达式: [u4e00-u9fa5]
例
代码如下 |
复制代码 |
$str = "php编程";
if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
|
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
匹配双字节字符(包括汉字在内):[^x00-xff]
例
代码如下 |
复制代码 |
$str = "singlepoint单点日志";
if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
$alias_len = mb_strlen($value['alias'], "utf-8");
$temp_array = array();
for($i = 0;$i<$alias_len; $i++)
{
$temp_array[$i] = mb_substr($value['alias'],$i,1,"utf-8");
if(ord(substr($temp_array[$i],0,1))>'0xe0' && strlen($temp_array[$i])<3)
$temp_array[$i] = '';
}
$value['alias'] = implode('',$temp_array);
|
评注:编码表 双字节字符编码范围 1. GBK (GB2312/GB18030) x00-xff GBK双字节编码范围 x20-x7f ASCII
xa1-xff 中文 gb2312 x80-xff 中文 gbk 2. UTF-8 (Unicode) u4e00-u9fa5 (中文) x3130-x318F (韩文
xAC00-xD7A3 (韩文) u0800-u4e00 (日文)
匹配空白行的正则表达式:ns*r
代码如下 |
复制代码 |
$str = "123 456";
$patten = "/s+/";
$result = split($patten,$str);
echo join("<br>",$result);
|
评注:可以用来删除空白行
匹配HTML标记的正则表达式:<(S*?)[^>]*>.*?</1>|<.*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
匹配首尾空白字符的正则表达式:^s*|s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
评注:表单验证时很实用
匹配网址URL的正则表达式:[a-zA-z]+://[^s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用
匹配国内电话号码:d{3}-d{8}|d{4}-d{7}
评注:匹配形式如 0511-4405222 或 021-87888822
匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始
匹配中国邮政编码:[1-9]d{5}(?!d)
评注:中国邮政编码为6位数字
匹配身份证:d{15}|d{18}
评注:中国的身份证为15位或18位
匹配ip地址:d+.d+.d+.d+
评注:提取ip地址时有用
可能对你有用的与正则表达式有关的内容
一,a-z A-Z_0-9 //最常见的字符
二,(bfw)(sda) //用圆括号包含起来的单元符号,一个括号表示一个整体
三,[sdwe][^mjnb] //用方括号包含起来的原子表,原子表中^代表排除或相反内容
四,转义字符
d 包含所有的数字[0-9]
D 除所有数字外[^0-9]
w 包含所有英文字符[a-zA-Z_0-9]
W 除所有英文字符外[^a-zA-Z_0-9] -----匹配特殊字符
s 包含空白区域 如回车,换行,分页等[fnr]
4,正则表达式元字符
* 匹配前一个内容的0次或多次
. 匹配内容的0次或多次,但不包含回车换行
+ 匹配前一个内容的1次或多次
? 匹配前一个内容的0次或1次
| 选择匹配,类似php中||的用法
^ 匹配字符串首部的内容
$ 匹配字符串尾部内容
b 匹配单词边界,边界可以是空格或者特殊符号
B 匹配除带单词边界的意外内容
{m} 匹配前一个内容的重复次数为m次
{m,} 匹配前一个内容的重复次数大于等于m次
{m,n} 匹配前一个内容的重复次数m次到n次
() 合并整体匹配,并放入内存,可使用\1\2依次获取调用
在php中我们会经常碰到一些错误要去处理,下面我来总结php中的异常处理、错误报告、日志一些内容总结与介绍。
错误处理:
1.语法错误
2.运行时错误
3.逻辑错误
错误报告:
错误:E_ERROT 程序会中断,发生错误
警告:E_WARNING 程序不会中断,但可能部分功能没有实现
注意:E_NOTICE 不会影响程序,完全可以屏蔽
开发时输出所有错误报告,运行时禁用所有错误报告
将错误写入到日志中:
1.开启日志(php.ini中error_log = On),且关闭错误报告,错误(如果发生,但没有允许直接输出)日志将会被记录
2.如不指定日志路径,将默认写入web服务器日志中
设置错误报告:
error_reporting(E_ALL) //输出所有报告
修改php.ini配置文件:
代码如下 |
复制代码 |
ini_set(“display_errors”,off) //修改为不显示错误报告
ini_get(“upload_max_filesize”) //读取配置文件中上传文件大小限制
|
异常处理:
意外,是在程序运行中发生的意料之外的事,使用异常改变脚本正常流程
PHP 5 提供了一种新的面向对象的错误处理方法。
异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程。这种情况称为异常。
当异常被触发时,通常会发生:
•当前代码状态被保存
•代码执行被切换到预定义的异常处理器函数
•根据情况,处理器也许会从保存的代码状态重新开始执行代码,终止脚本执行,或从代码中另外的位置继续执行脚本
我们将展示不同的错误处理方法:
•异常的基本使用
•创建自定义的异常处理器
•多个异常
•重新抛出异常
•设置顶层异常处理器
语法:
代码如下 |
复制代码 |
try{
可能出错的代码
throw new Exception(“异常信息”)
}catch(Exception $e[异常对象]){
后面的正常代码
}
|
例
代码如下 |
复制代码 |
function runtimeErrorHandler($level,$string)
{
//自定义错误处理时,手动抛出一个异常实例
//为了把错误级别代码也显示出来,这里拼接了错误代码和错误信息作为新的错误信息来传递。
throw new Exception($level.'|'.$string);
}
//设置自定义错误处理函数
set_error_handler( "runtimeErrorHandler");
try
{
$a=2/0;
//这里制造一个以前无法截获的除0错误
}
catch(Exception $e)
{
echo '错误信息:', $e->getMessage();
//显示错误,这里就可以看到错误级别和错误信息了“2|Division by zero”
}
|
1.如果try中代码没有异常,则正常执行。
2.如果try中代码有异常,则抛出一个异常对象,在catch()中捕捉$e就指向异常对象。再继续向下执行
3.$e->getMessage()获取异常信息
自定义异常类:
作用:写一些方法解决特定异常(内置的类没有处理方法)
1.自定义异常类,必须是Exception(内置类)的子类
2.Exception类中只有构造方法和toString()可以重写
3.定义需要的方法
异常的规则
•需要进行异常处理的代码应该放入 try 代码块内,以便捕获潜在的异常。
•每个 try 或 throw 代码块必须至少拥有一个对应的 catch 代码块。
•使用多个 catch 代码块可以捕获不同种类的异常。
•可以在 try 代码块内的 catch 代码块中再次抛出(re-thrown)异常。
在php中截取字符串最简单的办法就是利用substr()函数来实现,但是substr函数只能截取英文,如果是中文不会是乱码哦,那么有朋友说可使用mb_substr()来截取,这个方法又不能截取中文英混合的字符。
此函数用于截取gb2312编码的中文字符串:
代码如下 |
复制代码 |
<?php
// 说明:截取中文字符串
function mysubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
?>
|
Utf-8、gb2312都支持的汉字截取函数
截取utf-8字符串函数
为了支持多语言,数据库里的字符串可能保存为UTF-8编码,在网站开发中可能需要用php截取字符串的一部分。为了避免出现乱码现象,编写如下的UTF-8字符串截取函数
关于utf-8的原理请看 UTF-8 FAQ
UTF-8编码的字符可能由1~3个字节组成, 具体数目可以由第一个字节判断出来。(理论上可能更长,但这里假设不超过3个字节)
第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)。
代码如下 |
复制代码 |
<?php
// 说明:Utf-8、gb2312都支持的汉字截取函数
/*
Utf-8、gb2312都支持的汉字截取函数
cut_str(字符串, 截取长度, 开始长度, 编码);
编码默认为 utf-8
开始长度默认为 0
*/
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
if($code == 'UTF-8')
{
$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i<$strlen; $i++)
{
if($i>=$start && $i<($start+$sublen))
{
if(ord(substr($string, $i, 1))>129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
$str = "abcd需要截取的字符串";
echo cut_str($str, 8, 0, 'gb2312');
?>
|
注意明:
代码如下 |
复制代码 |
function utf8Substr($str, $from, $len)
{
return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'.
'((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s',
'$1',$str);
}
|
可单独截取uft8字符串哦。
程序说明:
1. len 参数以中文字符为标准,1len等于2个英文字符,为了形式上好看些
2. 如果将magic参数设为false,则中文和英文同等看待,取绝对的字符数
3. 特别适用于用htmlspecialchars()进行过编码的字符串
4. 能正确处理GB2312中实体字符模式()
程序代码:
代码如下 |
复制代码 |
function FSubstr($title,$start,$len="",$magic=true)
{
/**
* powered by Smartpig
* mailto:d.einstein@263.net
*/
$length = 0;
if($len == "") $len = strlen($title);
//判断起始为不正确位置
if($start > 0)
{
$cnum = 0;
for($i=0;$i<$start;$i++)
{
if(ord(substr($title,$i,1)) >= 128) $cnum ++;
}
if($cnum%2 != 0) $start--;
unset($cnum);
}
if(strlen($title)<=$len) return substr($title,$start,$len);
$alen = 0;
$blen = 0;
$realnum = 0;
for($i=$start;$i<strlen($title);$i++)
{
$ctype = 0;
$cstep = 0;
$cur = substr($title,$i,1);
if($cur == "&")
{
if(substr($title,$i,4) == "<")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,4) == ">")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,5) == "&")
{
$cstep = 5;
$length += 5;
$i += 4;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == """)
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == "'")
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(preg_match("/&#(d+);/i",substr($title,$i,8),$match))
{
$cstep = strlen($match[0]);
$length += strlen($match[0]);
$i += strlen($match[0])-1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}
}else{
if(ord($cur)>=128)
{
$cstep = 2;
$length += 2;
$i += 1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}else{
$cstep = 1;
$length +=1;
$realnum ++;
if($magic)
{
$alen++;
}
}
}
if($magic)
{
if(($blen*2+$alen) == ($len*2)) break;
if(($blen*2+$alen) == ($len*2+1))
{
if($ctype == 1)
{
$length -= $cstep;
break;
}else{
break;
}
}
}else{
if($realnum == $len) break;
}
}
unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);
return substr($title,$start,$length);
}
|
无限级分类是所有程序开发中会碰到的一个问题,下面我来介绍php+mysql实现的一个无限级分类程序,有需要的朋友可参考参考。
下面给大家看看我的数据库结构吧:数据库的名字为:fa_category
Field |
Type |
Comment |
cid |
int(11) |
分类id |
catename |
varchar(255) |
分类名字 |
catetype |
int(1) |
分类类型,1为单页面,2为普通分类 |
catdir |
varchar(255) |
英文目录 |
display |
int(1) |
1为显示,2为不显示 |
keywords |
varchar(255) |
栏目关键字 |
description |
text |
栏目描述 |
ctime |
int(11) |
创建时间 |
parentid |
int(11) |
父节点id,最高节点父节点为0 |
我们使用一个parentid字段来记录父节点的id,如果parentid为0,则为root。不多说,我们看看代码怎么写吧。我们要实现的功能就是如图片所示:
怎么样把它这样显示呢?这个问题我想了很久,先看看这段SQL语句吧,
代码如下 |
复制代码 |
SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade ,c.sort_order, COUNT( s.cat_id ) AS has_children
FROM ecs_category AS c
LEFT JOIN ecs_category AS s ON s.parent_id = c.cat_id
GROUP BY c.cat_id
ORDER BY c.parent_id, c.sort_order ASC
|
用左连接连接一个表,返回一个字段 has_children,这个字段是记录有多少子节点。
看看代码吧,
代码如下 |
复制代码 |
public function getCategory($catid=0,$re_type = true,$selected=0)
{
$db = new Public_DataBase();
$sql = 'select c.cid,c.catename,c.catetype,c.ctime,c.parentid,count(s.cid) as has_children from '.
__MYSQL_PRE.'category as c left join '.
__MYSQL_PRE.'category as s on s.parentid=c.cid group by c.cid order by c.parentid asc';
$res = $db->selectTable($sql);
$cateInfo = self::getChildTree($catid,$res);
if($re_type==true)
{
$select = '';
foreach($cateInfo as $val)
{
$select .= '<option value="' . $val['cid'] . '" ';
$select .= ($selected == $val['cid']) ? "selected='ture'" : '';
$select .= '>';
if($val['level']>0)
{
$select .= str_repeat(' ', $val['level'] * 4);
}
$select .= htmlspecialchars(addslashes($val['catename']), ENT_QUOTES) . '</option>';
}
return $select;
}
else
{
foreach($cateInfo as $key=>$val)
{
if($val['level']>0)
{
$cateInfo[$key]['catename'] = "|".str_repeat(' ', $val['level'] * 8)."└─".$val['catename'];
}
}
return $cateInfo;
}
}
/**
* 通过父ID递归得到所有子节点树
* @param int $catid 上级分类
* @param array $arr 含有所有分类的数组
* @return array
*/
public function getChildTree($catid,$arr)
{
$level = $last_cat_id = 0;
while (!empty($arr))
{
foreach($arr as $key=>$value)
{
$cid = $value['cid'];
if ($level == 0 && $last_cat_id == 0)
{
if ($value['parentid'] > 0)
{
break;
}
$options[$cid] = $value;
$options[$cid]['level'] = $level;
$options[$cid]['id'] = $cid;
$options[$cid]['name'] = $value['catename'];
unset($arr[$key]);
if ($value['has_children'] == 0)
{
continue;
}
$last_cat_id = $cid;
$cat_id_array = array($cid);
$level_array[$last_cat_id] = ++$level;
continue;
}
if ($value['parentid'] == $last_cat_id)
{
$options[$cid] = $value;
$options[$cid]['level'] = $level;
$options[$cid]['id'] = $cid;
$options[$cid]['name'] = $value['catename'];
unset($arr[$key]);
if ($value['has_children'] > 0)
{
if (end($cat_id_array) != $last_cat_id)
{
$cat_id_array[] = $last_cat_id;
}
$last_cat_id = $cid;
$cat_id_array[] = $cid;
$level_array[$last_cat_id] = ++$level;
}
}
elseif ($value['parentid'] > $last_cat_id)
{
break;
}
}
$count = count($cat_id_array);
if ($count > 1)
{
$last_cat_id = array_pop($cat_id_array);
}
elseif ($count == 1)
{
if ($last_cat_id != end($cat_id_array))
{
$last_cat_id = end($cat_id_array);
}
else
{
$level = 0;
$last_cat_id = 0;
$cat_id_array = array();
continue;
}
}
if ($last_cat_id && isset($level_array[$last_cat_id]))
{
$level = $level_array[$last_cat_id];
}
else
{
$level = 0;
}
}
return $options;
|
}用smarty的一个循环就可以把它显示出来 效果和上面图片的一样!大家有什么问题可以给我留言。
一个简单php和mysql数据分页程序有需要的朋友可参考一下。
代码如下 |
复制代码 |
<?php
// Adam's Custom PHP MySQL Pagination Tutorial and Script
// You have to put your mysql connection data and alter the SQL queries(both queries)
// This script is in tutorial form and is accompanied by the following video:
mysql_connect("DB_Host_Here","DB_Username_Here","DB_Password_Here") or die (mysql_error());
mysql_select_db("DB_Name_Here") or die (mysql_error());
////////////// QUERY THE MEMBER DATA INITIALLY LIKE YOU NORMALLY WOULD
$sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC");
//////////////////////////////////// Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////
$nr = mysql_num_rows($sql); // Get total of Num rows from the database query
if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
//$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
} else { // If the pn URL variable is not present force it to be value of page number 1
$pn = 1;
}
//This is where we set how many database items to show on each page
$itemsPerPage = 10;
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
$pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
$pn = $lastPage; // force it to be $lastpage's value
}
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
} else if ($pn == $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> ';
} else if ($pn > 1 && $pn < $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
$sql2 = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC $limit");
//////////////////////////////// END Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. ' ';
// If we are not on page 1 we can place the Back button
if ($pn != 1) {
$previous = $pn - 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
}
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($pn != $lastPage) {
$nextPage = $pn + 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
}
}
///////////////////////////////////// END Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////////////
// Build the Output Section Here
$outputList = '';
while($row = mysql_fetch_array($sql2)){
$id = $row["id"];
$firstname = $row["firstname"];
$country = $row["country"];
$outputList .= '<h1>' . $firstname . '</h1><h2>' . $country . ' </h2><hr />';
} // close while loop
?>
<html>
<head>
<title>Adam's Pagination</title>
<style type="text/css">
<!--
.pagNumActive {
color: #000;
border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:link {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:visited {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:hover {
color: #000;
text-decoration: none;
border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:active {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
-->
</style>
</head>
<body>
<div style="margin-left:64px; margin-right:64px;">
<h2>Total Items: <?php echo $nr; ?></h2>
</div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
<div style="margin-left:64px; margin-right:64px;"><?php print "$outputList"; ?></div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
</body>
</html>
|
效果
Page 6 of 39 Back Next