php簡單分頁

公共文件common.php

<?php
header("content-type:text/html;charset=utf-8");
//配置信息
$db_host='localhost';  //主機名
$db_port='3306';       //端口號
$db_user='root';       //用戶名
$db_pass='123456';     //密碼
$db_name='students';   //數(shù)據(jù)庫名稱
$charset='utf8';       //字符集
//php連接mysql服務(wù)器
$link=@mysql_connect($db_host.':'.$db_port,$db_user,$db_pass);
if(!$link)
    die('php數(shù)據(jù)庫連接失敗'.mysql_error());
//選擇當前數(shù)據(jù)庫
if(!mysql_select_db($db_name))
    die('選擇數(shù)據(jù)庫失敗'.mysql_error());
//設(shè)置客戶端字符集
mysql_query("set charset {$charset}");

list.php分頁文件

<?php
    //包含連接數(shù)據(jù)庫的公共文件
    require_once("./conn.php");
    //每頁顯示數(shù)
    $pagesize=5;
    //獲取當前頁碼和計算開始行號
    $page=isset($_GET['page']) ? $_GET['page']:1;
    $startrow=($page-1)*$pagesize;
    //計算總記錄數(shù)和總頁數(shù)
    $sql="select*from person";
    $result=mysql_query($sql);
    $records=mysql_num_rows($result);
    $pages=ceil($records / $pagesize);
    //構(gòu)建分頁的sql語句
    $sql .=" order by id asc limit {$startrow},{$pagesize}";
    //執(zhí)行sql語句,并返回結(jié)果集
    $result=mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>學生信息管理中心</title>
    <style type="text/css">
        .pagelist a,.pagelist span{
            display: inline-block;
            padding:5px 10px;
            border:1px solid #ddd;
            margin:0 3px;
            text-decoration: none;
        }
        .pagelist span{color:red;border-color:red;}
    </style>
</head>
<body>
<div class="content" style="text-align: center;margin-bottom:20px;">
    <h2>學生信息管理中心</h2>
</div>
<table width='600' border='1' bordercolor='#ccc' align='center' cellpadding='5' cellspacing='0'>
    <tr bgcolor="#f0f0f0">
        <th>編號</th>
        <th>姓名</th>
        <th>年齡</th>
        <th>性別</th>
        <th>學歷</th>
        <th>愛好</th>
        <th>工資</th>
        <th>獎金</th>
        <th>籍貫</th>
    </tr>
    <?php
        while($arr=mysql_fetch_assoc($result)){
    ?>
    <tr>
        <td><?php echo $arr['id'] ?></td>
        <td><?php echo $arr['name'] ?></td>
        <td><?php echo $arr['age'] ?></td>
        <td><?php echo $arr['sex'] ?></td>
        <td><?php echo $arr['edu'] ?></td>
        <td><?php echo $arr['hobby'] ?></td>
        <td><?php echo $arr['salary'] ?></td>
        <td><?php echo $arr['bonus'] ?></td>
        <td><?php echo $arr['city'] ?></td>
    </tr>
    <?php } ?>
    <tr>
        <td colspan="9" align="center" class='pagelist'>
            <?php 
                //計算循環(huán)的起點和終止頁
                $start=$page-1;
                $end=$page+1;
                if($page<2){
                    $start=1;
                    $end=3;
                }
                if($page>$pages-1){
                    $start=$pages-2;
                    $end=$pages;
                }
                for ($i=$start; $i <=$end; $i++) { 
                    //如果是當前頁,不加鏈接
                    if($i==$page){
                        echo "<span>$i</span>";
                    }else{
                        echo "<a href='?page=$i'>$i</a>";
                    }   
                }
            ?>
        </td>
    </tr>
</table>
</body>
</html>
分頁信息表1.png
分頁信息表2.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容