最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php分頁代碼學習示例分享

 更新時間:2014年02月20日 16:53:22   作者:  
這篇文章主要介紹了一個簡單的php分頁代碼,提供給大家學習一下了,代碼中有注釋,也可以直接使用

復制代碼 代碼如下:

<?php 
    header("content-type:text/html;charset=utf-8");
    //數(shù)據(jù)庫連接
    $conn = mysql_connect("localhost", "root", "111") or die("not connnected : ".mysql_error());
    mysql_select_db("test", $conn);
    mysql_query("set names utf8");

    //查詢共有多少行數(shù)據(jù)
    $sql1 = "select count(*) from user";
    $ret1 = mysql_query($sql1);
    $row1 = mysql_fetch_row($ret1);
    $tot = $row1[0];

    //每頁多少行數(shù)據(jù)
    $length = 5;     
    //總頁數(shù) 
    $totpage = ceil($tot / $length);

    //當前頁數(shù)
    $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>";

    //將查詢出來的數(shù)據(jù)用表格顯示
    $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;
    }

    //跳轉(zhuǎn)
    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和頁數(shù)之間的關(guān)系。

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

 

相關(guān)文章

最新評論

满洲里市| 沾益县| 大方县| 肇庆市| 乐东| 上饶市| 武功县| 抚顺县| 柳江县| 中宁县| 科尔| 鄂伦春自治旗| 屏东县| 内丘县| 吕梁市| 秀山| 左云县| 靖宇县| 屏南县| 南陵县| 新营市| 广东省| 淮安市| 通辽市| 图片| 宁南县| 郸城县| 界首市| 曲沃县| 边坝县| 八宿县| 霍林郭勒市| 五大连池市| 山东| 乡城县| 南城县| 抚松县| 阿坝县| 北票市| 紫阳县| 兰溪市|