Home > Backend Development > PHP Problem > What is the idea of ​​​​implementing record paging in php?

What is the idea of ​​​​implementing record paging in php?

藏色散人
Release: 2023-03-14 12:12:02
Original
2039 people have browsed it

php ideas for implementing record paging: 1. Get the number of records in the result set; 2. Set the number of records displayed on each page; 3. Get the total number of pages; 4. Control the number of records displayed on each page; 5 , just notify the script program of the page number displayed by passing parameters.

What is the idea of ​​​​implementing record paging in php?

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

What is the idea of ​​​​implementing record paging in php?

PHP Mysql realizes data paging display:

  • ##To get the number of records in the result set, you can use the COUNT() function in the SELECT statement to get the number of records in the result set

  • Set the number of records displayed on each page assuming the use of variables

    $PageSize is used to save the number of records displayed on each page. Its value is set by the user according to needs, and can be implemented directly through an assignment statement.

  • To obtain the total number of pages, you can calculate the total number of pages through the two data

    $RecordCount and $PageSize$PageCount,

  • How to display the records in page n Although the PageSize property can be used to control the number of records displayed on each page, but which records should be displayed? You can use the LIMIT clause in the SELECT statement to specify the range of query records. The usage method is as follows: SELECT * FROM table name LIMIT starting position, display the number of records. For example, to obtain the records in page

    $Page , you can use the following statement: SELECT * FROM table name LIMIT ($Page- 1) * $ PageSize, $ PageSize

  • How to notify the script of the page number to be displayed You can tell the script the page number to display by passing parameters. Assume that the script for paging records is viewPage. php, and the link to pass parameters is as follows:

    http:// localhost/ viewPage. php? page= 2 The page parameter is used to specify the current page number. In viewPage.php, use the following statement to read the parameters:

  • <!doctype html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>Document</title>
    </head>
    <body>
        <?php 
        header("content-type:text/html;charset=utf-8");
            //获取当前页码
            $page=$_GET[&#39;page&#39;];
            if($page==0){
                $page=1;
            }
            //设置每页最大能显示的数量
            $pagesize=3;
    
            //连接数据库
            $conn=mysql_connect("localhost","root","root");
            mysql_select_db("test");
            mysql_query("set names utf-8");
            if(!$conn){
                die("mysql_connect_failed".mysql_connect_error());
            }
            else 
                echo("connected succeed"."<br />");
            //获取结果集的记录数
            $row=mysql_fetch_row(mysql_query("select count(1) from clerk"));
            $recordcount=$row[0]; 
    
    
    
    
            //计算总页数
            if($recordcount==0)
                $pagecount=0;
            else if($recordcount<$pagesize ||$recordcount==$pagesize){
                    $pagecount=1;
                    //如果 记录 总数 量小 于 每页 显示 的 记录 数量, 则 只有 一页
                }
            else if($recordcount%$pagesize==0){
                    $pagecount=$recordcount/$pagesize;
                    //如果 没有 余数, 则 页数 等于 总 记录 数量 除以 每页 显示 记录 的 数量
                }
            else 
                    $pagecount=(int)($recordcount/$pagesize)+1;
                    //取 记录 总数 量 不能 整除 每页 显示 记录 的 数量,
                    // 则 页数 等于 总 记录 数量 除以 每页 显示 记录 数量 的 结果 取整 再加 1
    
            echo("当前页码:".$page."/".$pagecount."<br />");
    
        ?>
    
        <table width="449" border="1">
            <tr>
                <td>员工姓名</td>
                <td>职务</td>
                <td>薪水</td>
            </tr>
        <?php 
        //循环显示当前页面的记录
        header("content-type:text/html;charset=utf-8");
        echo $page;
        //$sql="select * from clerk limit" .($page-1)*$pagesize.",".$pagesize;  //$page为当前页码
        $sql=($page-1)*$pagesize;
        $result=mysql_query("select * from clerk limit {$sql},{$pagesize}");
        while($row=mysql_fetch_row($result))
        {   
    
            echo("<tr />");
            echo("<td>$row[0]</td>");
            echo("<td>$row[2]</td>");
            echo("<td>$row[3]</td>");
            echo("<tr />");
        }
        mysql_close($conn);
    
        //显示分页链接
        if($page==1){
            echo("第一页");
        }
        else
            echo("<a href=viewpage.php?page=1>第一页</a>");
            //设置上一页连接
    
        if($page==1){
             echo("上一页");
        }
        else 
            echo("<a href=viewpage.php?page=".($page-1).">上一页</a>");
    
            //设置下一页链接
        if($page==$pagecount){
            echo("下一页");
        }
        else 
            echo("<a href=viewpage.php?page=".($page+1).">下一页</a>");
        //设置最后一页
        if($page==$pagecount){
            echo("最后一页");
        }
        else 
            echo("<a href=viewpage.php?page=".$pagecount.">最后一页</a>");
        ?>
        </table>
    </body>
    </html>
    Copy after login

What is the idea of ​​​​implementing record paging in php?

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What is the idea of ​​​​implementing record paging in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template