代码一、
<?php
function genpage(&$sql,$page_size=20)
{
global $prepage,$nextpage,$pages,$sums; //out param
$page = $_GET["page"];
$eachpage = $page_size;
$pagesql = strstr($sql," from ");
$pagesql = "select count(*) as ids ".$pagesql;
$result = mysql_query($pagesql) or die(mysql_error());
if($rs = mysql_fetch_array($result)) $sums = $rs[0];
$pages = ceil(($sums-0.5)/$eachpage)-1;
$pages = $pages>=0?$pages:0;
$prepage = ($page>0)?$page-1:0;
$nextpage = ($page<$pages)?$page+1:$pages;
$startpos = $page*$eachpage;
$sql .=" limit $startpos,$eachpage ";
}
function showpage()
{
global $page,$pages,$prepage,$nextpage,$queryString;
$shownum =10/2;
$startpage = ($page>=$shownum)?$page-$shownum:0;
$endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
echo "total".($pages+1)."page: ";
if($page>0)echo "<a href=$PHP_SELF?page=0&$queryString>the first</a>";
if($startpage>0)
echo " ... <b><a href=$PHP_SELF?page=".($page-$shownum*2)."&$queryString>«</a></b>";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " <b>[".($i+1)."]</b> ";
else echo " <a href=$PHP_SELF?page=$i&$queryString>".($i+1)."</a> ";
}
if($endpage<$pages)
echo "<b><a href=$PHP_SELF?page=".($page+$shownum*2)."&$queryString>»</a></b> ... ";
if($page<$pages)
echo "<a href=$PHP_SELF?page=$pages&$queryString>the end</a>";
}
?>
然后进行引用就行了
<?php
$mysql_link=mysql_connect($host,$user,$password);
mysql_select_db($bdb,$mysql_link);
$query = "select * from t_games order by id";
genpage($query);//此句不可少
$result =mysql_qusery($query);
while($rs =mysql_fetch_array($result))
{
?>
<a href ="gamesinfo.php?gamesid=<?echo $rs["id"];?>"><? echo ($rs[gamesname]);?></a><BR>
<?
}
?>
<BR>
<?php
showpage(); //显示bar
?>
代码二、
<?PHP
$conn=mysql_connect("localhost","sa","1" ) or die("Could not connect: " );
mysql_Select_db("njc2",$conn);
$strsql="select * from t_games";
$result = mysql_query($strsql);
$total = mysql_num_rows($result);
$pagesize=10;
if (($total%$pagesize)!=0)
$totalpage=intval($total/$pagesize)+1;
else
$totalpage=intval($total/$pagesize);
if (empty($_GET['pagenum'])){
$_GET['pagenum']=1; }
if($total!=0){
mysql_data_seek($result,(($_GET['pagenum']-1)*$pagesize));
}
$i=1;
?>
<table border="0" >
<?PHP
while($row=mssql_fetch_row($result))
{?>
<tr>
<td><?=$row[0];?></td>
<td><?=$row[1];?></td>
<td><?=$row[2];?></td>
<td><?=$row[3];?></td>
<td><?=$row[4];?></td>
<td><a href="play.php?id=<?=$row[0];?>"><?=$row[3];?></a></td>
</tr>
<? $i++;
if ($i>$pagesize)
break;
else
continue;
}
?>
<b>第<?=$_GET['pagenum'];?>页,共<?=$totalpage;?>页.......共<?=$total;?>条记录
</table><br/>
<?PHP
echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=1>首页</a> ";
if ($_GET['pagenum']>1){
$temp_current=$_GET['pagenum']-1;
echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$temp_current.">上一页</a> "; }
if ($_GET['pagenum']<$totalpage){
$temp_current=$_GET['pagenum']+1;
echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$temp_current.">下一页</a> "; }
echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$totalpage.">尾页</a> ";
?>
其中只需要修改数据库名就行了
代码三、
<?php
/**********************
* 通用分页类
* 作者: 多菜鸟
* 邮箱: kingerq AT msn DOT com
* 时间: 2006-04-17
* 来源:http://blog.csdn.net/kingerq
* 实例:
$p = new Page( 60, 10 );
$p->trunStr = array( "First", "Prev", "Next", "End" );//设置分页显示字符
echo $p->printPage();//输出分页字符串,也可以trunUp(),trunNum(),trunDown()和jumpTo()分开来输出显示
//echo $p->limitStr();//显示MYSQL中LIMIT部分字符串
//echo $p->pageCount();//总页数
**********************
*/
class Page {
var $perPage = 5; //每页显示的页码数
var $paramStr = ""; //分页参数字符串变量,连接地址
var $currentPage = 1; //当前页码
var $paramName = "pageNum"; //翻页参数名称
var $limitStr = ""; //MYSQL中LIMIT参数字符串
var $pageSize = 20; //每页记录数
var $trunStr = array( "首页", "上页", "下页", "末页" ); //翻页字符
/*
* 构造函数
* $recordCount 为记录总数量
* $pageSize 为每页显示记录数
*/
function Page( $recordCount = 0, $pageSize = 0 ) {
if( ! is_numeric( $recordCount ) || ! is_numeric( $pageSize ) ) return;
//记录总数
if( intval( $recordCount ) == 0 )
$recordCount = $this->recordCount;
else
$this->recordCount = $recordCount;
//每页显示记录数
if( intval( $pageSize ) == 0 )
$pageSize = $this->pageSize;
else
$this->pageSize = $pageSize;
$this->currentPage();//当前页码处理
$this->paramStr();//当前页地址参数
//为当前页定界
if( $this->currentPage > $this->pageCount() )
$this->currentPage = $this->pageCount() < 1 ? 1 : $this->pageCount();
}
function printPage() {
$pageStr = $this->trunUp(); //向上
$pageStr .= $this->trunNum(); //数字页码
$pageStr .= $this->trunDown(); //向下
$pageStr .= $this->jumpTo(); //跳转
return $pageStr;
}
//向上翻处理
function trunUp() {
$pageStr = "";
if( $this->currentPage == 1 ) {
$pageStr .= $this->trunStr[0] . " " . $this->trunStr[1] . " ";
}else {
$pageStr .= "<a href=/"".$this->paramStr."1/" title=/"".$this->trunStr[0]."/">".$this->trunStr[0]."</a> ";
$pageStr .= "<a href=/"".$this->paramStr.($this->currentPage - 1)."/" title=/"".$this->trunStr[1]."/">".$this->trunStr[1]."</a> ";
}
return $pageStr;
}
//每页显示的页码连接
function trunNum() {
$pageStr = "";
$num = 1;
while( $this->currentPage - $this->perPage * $num > 0 ) $num++;
$start = ( $num - 1 ) * $this->perPage;
$end = $this->pageCount() < $this->perPage * $num ? $this->pageCount() : $this->perPage * $num;
for( $i = $start + 1; $i <= $end; $i++ ) {
$pageStr .= $i != $this->currentPage ? "<a href=/"".$this->paramStr."$i/">$i</a> " : "<strong>$i</strong> ";
}
return $pageStr;
}
//向下翻处理
function trunDown() {
$pageStr = "";
if( $this->pageCount() == $this->currentPage || $this->pageCount() == 0 ) {
$pageStr .= $this->trunStr[2] . " " . $this->trunStr[3] . " ";
} else {
$pageStr .= "<a href=/"".$this->paramStr.($this->currentPage + 1)."/" title=/"".$this->trunStr[2]."/">".$this->trunStr[2]."</a> ";
$pageStr .= "<a href=/"".$this->paramStr.$this->pageCount()."/" title=/"".$this->trunStr[3]."/">".$this->trunStr[3]."</a> ";
}
return $pageStr;
}
//跳转
function jumpTo() {
$pageStr = "<select οnchange=/"location.href=this.options[this.selectedIndex].value/">/n";
for($i = 1; $i <= $this->pageCount(); $i++){
if($this->currentPage == $i) $selected = " selected";
else $selected = "";
$pageStr .= "<option value='".$this->paramStr.$i."'$selected>$i</option>/n";
}
$pageStr .= "</select>";
return $pageStr;
}
//当前页参数
function paramStr() {
unset( $_GET[$this->paramName] );//去掉翻页参数
if( count( $_GET ) ) {
foreach( $_GET AS $param => $value ) {
$this->paramStr .= $this->paramStr ? "&" : "?";
$this->paramStr .= $param . "=" . $value;
}
$this->paramStr .= "&".$this->paramName."=";
} else {
$this->paramStr = "?$this->paramName=";
}
}
//取得当前页码
function currentPage() {
if( isset( $_GET[$this->paramName] ) && intval( $_GET[$this->paramName] ) > 0 ) {
$this->currentPage = intval( $_GET[$this->paramName] );
}
return $this->currentPage;
}
//总页数
function pageCount() {
return ceil( $this->recordCount / $this->pageSize );//总页数
}
//MYSQL中LIMIT部分字符串
function limitStr() {
return " LIMIT " . ( ( $this->currentPage - 1 ) * $this->pageSize ) . ", " . $this->pageSize;
}
//设置每页显示页码数,页码数至少大于1
function setPerPage($num = 5) {
if( intval($num) > 1 )
$this->perPage = intval($num);
}
}
?>