Message board p...LOGIN

Message board pagination developed in PHP

When a page displays a lot of data, we have to use our paging to display the data in pages. The paging code is as follows

<?php
session_start();
header("content-type:text/html;charset=utf-8");
$page=isset($_GET['page']) ?$_GET['page'] :1 ;//接收页码
$page=!empty($page) ? $page :1;
$conn=mysqli_connect("localhost","root","root","Ressage");
mysqli_set_charset($conn,'utf8'); //设定字符集
$table_name="ressage_user";//查取表名设置
$perpage=5;//每页显示的数据个数
//最大页数和总记录数
$total_sql="select count(*) from $table_name";
$total_result =mysqli_query($conn,$total_sql);
$total_row=mysqli_fetch_row($total_result);
$total = $total_row[0];//获取最大页码数
$total_page = ceil($total/$perpage);//向上整数
//临界点
$page=$page>$total_page ? $total_page:$page;//当下一页数大于最大页数时的情况
//分页设置初始化
$start=($page-1)*$perpage;

The paging code is divided into It is divided into two parts, one is to query data on the PHP page, and the other is to display the queried data on the HTML page. The html code is as follows

<div id="baner" style="margin-top: 20px">
    <a href="<?php
    echo "$_SERVER[PHP_SELF]?page=1"
    ?>">首页</a>
    &nbsp;&nbsp;<a href="<?php
    echo "$_SERVER[PHP_SELF]?page=".($page-1)
    ?>">上一页</a>
    <!--        显示123456等页码按钮-->
    <?php
    for($i=1;$i<=$total_page;$i++){
        if($i==$page){//当前页为显示页时加背景颜色
            echo "<a  style='padding: 5px 5px;background: #000;color: #FFF' href='$_SERVER[PHP_SELF]?page=$i'>$i</a>";
        }else{
            echo "<a  style='padding: 5px 5px' href='$_SERVER[PHP_SELF]?page=$i'>$i</a>";
        }
    }
    ?>
    &nbsp;&nbsp;<a href="<?php
    echo "$_SERVER[PHP_SELF]?page=".($page+1)
    ?>">下一页</a>
    &nbsp;&nbsp;<a href="<?php
    echo "$_SERVER[PHP_SELF]?page={$total_page}"
    ?>">末页</a>
    &nbsp;&nbsp;<span>共<?php echo $total?>条</span>
</div>

You only need to slightly change the above PHP code and change the database and data table. Add the local information and put the html code where you want it to be displayed on the page to achieve paging




##Next Section

<!doctype html> <html> <head> <meta charset="utf-8"> <title>PHP中文网</title> </head> <body> <h2>PHP分页代码</h2> </body> </html>
submitReset Code
ChapterCourseware