国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

php分頁功能實現方法

2024-04-25 20:31:03
字體:
來源:轉載
供稿:網友

文章整理了php實現分頁功能的方法,提供核心思路和實現代碼,希望對您有所幫助!

核心思想:

<1>“$sql2 = "select * from user order by id limit {$offset}, {$length}";”,$offset、$length和頁數之間的關系。

<2>上一頁和下一頁的獲得方式,以及臨界點。

代碼如下:

<?php 

    /**

    * php padding

    */


    header("content-type:text/html;charset=utf-8");

    //數據庫連接

    $conn = mysql_connect("localhost", "root", "111") or die("not connnected : ".mysql_error());

    mysql_select_db("test", $conn);

    mysql_query("set names utf8");


    //查詢共有多少行數據

    $sql1 = "select count(*) from user";

    $ret1 = mysql_query($sql1);

    $row1 = mysql_fetch_row($ret1);

    $tot = $row1[0]; 


    //每頁多少行數據

    $length = 5;      

    //總頁數  

    $totpage = ceil($tot / $length);


    //當前頁數

    $page = @$_GET['p'] ? $_GET['p'] : 1;

    //limit 下限

    $offset = ($page - 1) * $length;


    echo "<center>";

    echo "<h2>php padding</h2>";

    echo "<table width='700px' border='1px' >";

    echo "<tr>";

    echo "<th>ID</th>";

    echo "<th>USER</th>";

    echo "<th>PASS</th>";

    echo "</tr>";


    //將查詢出來的數據用表格顯示

    $sql2 = "select * from user order by id limit {$offset}, {$length}";

    $ret2 = mysql_query($sql2);

    while ($row2 = mysql_fetch_assoc($ret2)) {

        echo "<tr>";

        echo "<td>{$row2['id']}</td><td>{$row2['name']}</td><td>{$row2['pass']}</td>";

        echo "</tr>";

    }


    echo "</table>";


    //上一頁和下一頁

    $prevpage = $page - 1;

    if ($page >= $totpage) {

        $nextpage = $totpage;

    } else {

        $nextpage = $page + 1;

    }


    //跳轉

    echo "<h3><a href='index.php?p={$prevpage}'>上一頁</a>|<a href='index.php?p={$nextpage}'>下一頁</a></h3>";

    echo "</center>";

    

php常用源碼下載 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福海县| 囊谦县| 无棣县| 宁陕县| 营口市| 土默特右旗| 津南区| 克拉玛依市| 通许县| 富阳市| 社会| 屏东市| 铜川市| 鄢陵县| 岫岩| 句容市| 保德县| 大竹县| 阿巴嘎旗| 青海省| 高淳县| 九龙县| 麻阳| 师宗县| 本溪市| 连江县| 石城县| 县级市| 富裕县| 伊吾县| 科尔| 太原市| 洛宁县| 大厂| 南乐县| 东兴市| 凯里市| 凯里市| 罗江县| 南雄市| 治县。|