国产探花免费观看_亚洲丰满少妇自慰呻吟_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常用源碼下載 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 深州市| 阜阳市| 犍为县| 年辖:市辖区| 平谷区| 汉川市| 金塔县| 当雄县| 叶城县| 葵青区| 黑龙江省| 历史| 五大连池市| 乾安县| 邵武市| 什邡市| 华池县| 九龙县| 龙川县| 云龙县| 湘乡市| 荆州市| 平江县| 丰原市| 安岳县| 长宁区| 凤凰县| 鸡泽县| 龙南县| 宕昌县| 通州区| 原平市| 远安县| 晋江市| 新干县| 大城县| 淅川县| 若尔盖县| 东乌珠穆沁旗| 布拖县| 廊坊市|