Home  >  Article  >  Backend Development  >  PHP and Mysql implement data paging display example

PHP and Mysql implement data paging display example

小云云
小云云Original
2018-03-26 14:16:021228browse

This article mainly shares with you examples of data paging display using php and Mysql, hoping to help everyone.

  1. To obtain the number of records in the result set, you can use the COUNT() function in the SELECT statement to obtain the number of records in the result set.

  2. Set the display per page The number of records assumes that the variable $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.

  3. To obtain the total number of pages, you can calculate the total number of pages through the two data $RecordCount and $PageSize$PageCount,

  4. 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

  5. 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 lang="en"><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>


PHP and Mysql implement data paging display example

  1. 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.

  2. Set the number of records displayed on each page. It is assumed that the variable $PageSize is used. To save the number of records displayed on each page, its value can be set by the user according to needs, which can be achieved directly through assignment statements.

  3. To obtain the total number of pages, you can calculate the total number of pages through the two data $RecordCount and $PageSize$PageCount,

  4. 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

  5. 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 lang="en"><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>

PHP and Mysql implement data paging display example

The above is the detailed content of PHP and Mysql implement data paging display example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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