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

首頁 > 開發 > PHP > 正文

php分頁代碼學習示例分享

2024-05-04 23:21:30
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


<?php 
    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 >";
    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>";

核心點:

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

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

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玛纳斯县| 嫩江县| 剑阁县| 通渭县| 塔河县| 白银市| 巴马| 平泉县| 卢氏县| 上饶县| 扎赉特旗| 蓝田县| 五原县| 温州市| 斗六市| 岚皋县| 澎湖县| 汾西县| 东兰县| 徐水县| 漳州市| 兴安盟| 吴旗县| 秀山| 师宗县| 榆中县| 十堰市| 枣阳市| 城固县| 华蓥市| 陵川县| 吉安市| 岳阳市| 黄山市| 连山| 滨州市| 雷州市| 潜山县| 玉门市| 两当县| 石阡县|