PHP、MYSQLI實現分頁(初學者)

來源:互聯網
上載者:User

標籤:php   mysqli   

//求出總條數  假如13條

/*需求:每頁只顯示5條   即分三頁
* ceil(總條數/每頁顯示數)   ceil是向上取整,就算剩一條也要單獨佔一頁
*/


select * from bbs_user limit 0 , 5 這是第一頁   1

select * from bbs_user limit 5 , 5 這是第二頁   2

select * from bbs_user limit 10 , 5 這是第三頁  3

select * from bbs_user limit 15 , 5 這是第四頁  4

推出來的公式
    ($page - 1) * $sum (5) //$page是當前頁數



先寫好了查詢頁面,如下:

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>系統首頁面</title></head><body><?php     $conn = mysqli_connect("localhost","root","");    if(!$conn){        echo "失敗";    }        mysqli_select_db($conn,"bbs");    $sql = "select * from bbs_user";    $obj = mysqli_query($conn,$sql);    echo "<center>";    echo "<table border = 1 cellspacing = '0' cellpadding = '10'>";    echo "<th>編號</th><th>姓名</th><th>密碼</th><th>地址</th><th>性別</th><th>年齡</th><th>操作</th>";    while($row = mysqli_fetch_assoc($obj)){        echo "<tr>";            echo '<td>'.$row['id'].'</td>';            echo '<td>'.$row['username'].'</td>';            echo '<td>'.$row['password'].'</td>';            echo '<td>'.$row['address'].'</td>';            echo '<td>'.$row['sex'].'</td>';            echo '<td>'.$row['age'].'</td>';            echo '<td><a href = "del.php?id='.$row['id'].'">刪除</a>/<a href = "update.php?id='.$row['id'].'">修改</a></td>';        echo "</tr>";    }        echo "</table>";    echo "<a href = 'add.php'>添加</a>";    echo "<center>";    mysqli_close($conn);?></body></html>


在查詢介面上做了如下改變:

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>系統首頁面</title></head><body><?php         //5.設定$page的預設值    #$page = 1;        //8.修改$page的值    $page = empty($_GET['page'])?1 : $_GET['page'];    $conn = mysqli_connect("localhost","root","");    if(!$conn){        echo "失敗";    }    mysqli_select_db($conn,"bbs");            //------------分頁開始-------------------    //1.求出總條數    $sql = "select count(*) as count from bbs_user";    $result = mysqli_query($conn,$sql);    $pageRes = mysqli_fetch_assoc($result);    #var_dump($pageRes);   //13    $count = $pageRes['count'];        //2.每頁顯示數(5)    $num = 5;        //3.根據每頁顯示數求出總頁數    $pageCount = ceil($count / $num);  //向上取整    #var_dump($pageCount);  //3        //4.根據總頁數求出位移量    $offset = ($page - 1) * $num;  //$page預設為 1, 下一步設定    //------------分頁結束-------------------        //6.修改sql語句    $sql = "select * from bbs_user limit " . $offset . ',' . $num;        #$sql = "select * from bbs_user";    $obj = mysqli_query($conn,$sql);    echo "<center>";    echo "<table border = 1 cellspacing = '0' cellpadding = '10'>";    echo "<th>編號</th><th>姓名</th><th>密碼</th><th>地址</th><th>性別</th><th>年齡</th><th>操作</th>";    while($row = mysqli_fetch_assoc($obj)){        echo "<tr>";            echo '<td>'.$row['id'].'</td>';            echo '<td>'.$row['username'].'</td>';            echo '<td>'.$row['password'].'</td>';            echo '<td>'.$row['address'].'</td>';            echo '<td>'.$row['sex'].'</td>';            echo '<td>'.$row['age'].'</td>';            echo '<td><a href = "del.php?id='.$row['id'].'">刪除</a>/<a href = "update.php?id='.$row['id'].'">修改</a></td>';        echo "</tr>";    }        echo "</table>";    #echo "<a href = 'add.php'>添加</a>";    echo "<center>";        //10.設定上一頁下一頁的$prev和$next    $prev = $page - 1;    $next = $page + 1;        //11.設定頁數限制    if($prev<1){        $prev = 1;    }    if($next>$pageCount){        $next = $pageCount;    }        //關閉串連    mysqli_close($conn);?>    <!--7.添加首頁、上一頁、下一頁、尾頁(href沒有連結)-->    <!--9.給定連結,首頁和尾頁寫死,首頁就是page=1,尾頁是總頁數,上一頁先用$prev表示,下一步設定,下一頁同上一頁-->    <a href="testFenye.php?page=1">首頁</a>&nbsp;&nbsp;&nbsp;      <a href="testFenye.php?page=<?php echo $prev;?>">上一頁</a>&nbsp;&nbsp;&nbsp;    <!--混編簡寫-->    <a href="testFenye.php?page=<?=$next;?>">下一頁</a>&nbsp;&nbsp;&nbsp;    <a href="testFenye.php?page=<?=$pageCount;?>">尾頁</a>    </body></html>

PHP、MYSQLI實現分頁(初學者)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.