日期:2014-05-17  浏览次数:20623 次

php简单翻页代码
php简单翻页代码
<style type="text/css">
*{
    margin:0;
    padding:0;
}
#lay{
    margin:100px 100px 100px 100px;
}
a{
    padding:5px;
    font-size:13px;
    text-decoration:none;
}
span{
    font-size:13px;
}
table{
    border:1px solid red;
    border-collapse:collapse;
}
</style>
<div id="lay">
<table border="1" align="center" cellpadding="0" cellpadding="0">
<tr align="center" bgcolor="#ffcc">
    <td width="200px;">id</td>
    <td width="200px;">值</td>
  </tr>
<?php
    $link = mysql_connect("localhost","root","admin"); //连接数据库
    mysql_select_db("pagelist");//选择哪个数据库
    mysql_query("set names gbk"); //设置数据库字符编码为中文
    $sql = mysql_query("select *from v_char");
    $pagesize = 10; //
    $sum = mysql_num_rows($sql); //一共有多少条数据
    $count = ceil($sum/$pagesize);//求出一共有多少页
    $pages = $count; //显示最后一页
    $init = 1;
    $page_len = 7;
    $max_p = $count;    
    if(empty($_GET["page"]) || $_GET["page"]<0){
        $page = 1;
    }else{
        $page = $_GET["page"];
    }
    $off = ($page-1)*$pagesize; //求出数据库查询的第一个数据
    $he = mysql_query("select *from v_char limit $off,$pagesize");//按照off开始,到pagesize规律显示
    while($row = mysql_fetch_array($he)){
?>    
<tr align="center">
<td><?php echo $row["id"];?></td>
<td><?php echo $row["name"];?></td>
</tr>
<?php 
    }
    $page_len = ($page_len%2)?$page_len:$page_len+1;//如果余为1则为真,为0则为假
    $pageoffset = ($page_len-1)/2;//页码偏移量
    
    if($page!=1){
        $key.="<span><a href=\"".$_SERVER['PHP_SELF']."?page=1"."\">第一页&nbsp;</a></span>";
        $key.="<span><a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\">上一页&nbsp;</a></span>";
    }else{
        $key.="<span>第一页&nbsp;</span>";
        $key.="<span>上一页&nbsp;</span>";
    }
    if($pages>$page_len){
        if($page<=$pageoffset){
            $init=1;
            $max_p = $page_len;
        }else{
            if($page+$pageoffset>=$pages+1){
                $init = $pages - $page_len+1;
            }else{
                $init = $page-$pageoffset;
                $max_p = $page + $pageoffset;
            }
        }
    }
    for($i=$init;$i<$max_p;$i++){
        if($i==$page){
            $key.="[&nbsp;".$i."&nbsp;]";
            
        }else{
            $key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."\">$i</a>";
        }
    }
    if($i!=$page){
        $key.="<span><a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\">下一页</a></span>";
        $key.="<span><a href=\"".$_SERVER['PHP_SELF']."?page=".$pages."\">最后一页</a></span>";
    }else{
        $key.="<span>下一页</span>";
        $key.="<span>最后一页</span>";
    }
?>
<tr align="center">
<td colspan="2"><?php echo $key;?></td>
</tr>
</table>
</div>