Home >Backend Development >PHP Tutorial >PHP paging program implementation code for PHP development_PHP tutorial

PHP paging program implementation code for PHP development_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:55:57911browse

It is relatively simple to implement paging in php+mysql. Just get the page and then For reference.

Project structure:

Operation effect:

Database connection code

The code is as follows Copy code
 代码如下 复制代码

$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误"); mysql_select_db("form", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
//替换空格,回车键
function htmtocode($content)
{
$content = str_replace("n", "
", str_replace(" ", " ", $content));
return $content;
}
?>

$conn = @ mysql_connect("localhost", "root", "") or die("Database link error"); mysql_select_db("form", $conn);
mysql_query("set names 'GBK'"); //Use GBK Chinese encoding;
//Replace spaces and enter key
function htmtocode($content)
{
$content = str_replace("n", "
", str_replace(" ", " ", $content));
return $content;
}
?>

Here is a more important sharing core function

The code is as follows Copy code


Function _PAGEFT($totle, $displaypg = 20, $url = '') {

global $page, $firstcount, $pagenav, $_SERVER;

            $GLOBALS["displaypg"] = $displaypg;

          if (!$page)
$page = 1;
             if (!$url) {
               $url = $_SERVER["REQUEST_URI"];
         }

//URL analysis:
            $parse_url = parse_url($url);
          $url_query = $parse_url["query"]; //Get the query string of the URL separately
             if ($url_query) {
                $url_query = ereg_replace("(^|&)page=$page", "", $url_query);
                $url = str_replace($parse_url["query"], $url_query, $url);
                 if ($url_query)
                           $url .= "&page";
            else
                       $url .= "page";
          } else {
                  $url .= "?page";
         }
​​​​​ $lastpg = ceil($totle / $displaypg); //The last page, also the total number of pages
           $page = min($lastpg, $page);
$prepg = $page -1; //Previous page
          $nextpg = ($page == $lastpg ? 0 : $page +1); //Next page
         $firstcount = ($page -1) * $displaypg;

//Start paging navigation bar code:
$pagenav = "Display number " . ($totle ? ($firstcount +1) : 0) . "-" . min($firstcount + $displaypg, $totle) . " records, $totle records in total";

//If there is only one page, jump out of the function:
If ($lastpg <= 1)
              return false;

               $pagenav .= " Homepage ";
            if ($prepg)
                 $pagenav .= " Previous page ";
        else
                $pagenav .= "Previous page";
            if ($nextpg)
                  $pagenav .= " Next page ";
        else
                 $pagenav .= "Next page";
             $pagenav .= " Last page ";

//Pull down the jump list and list all page numbers in a loop:
             $pagenav .= "Go to page page of $lastpg";
}


include("conn.php");

$result=mysql_query("SELECT * FROM `test`");
$total=mysql_num_rows($result);
//Call pageft() to display 10 pieces of information per page (this parameter can be omitted when using the default 20), and use the URL of this page (default, so omit it).
_PAGEFT($total,5);
echo $pagenav;

$result=mysql_query("SELECT * FROM `test` limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result)){

echo "


".$row[name]." | ".$row[sex];

}
?>

list.php

Database query records and generate sql query statements

The code is as follows
 代码如下 复制代码
include("conn.php");
$pagesize=5; 5 $url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$numq=mysql_query("SELECT * FROM `test`");
$num = mysql_num_rows($numq);
if($_GET[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;
echo "共 $num 条". 21 " 上一页 下一页";
}
$SQL="SELECT * FROM `test` limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){
echo "
".$row[name]." | ".$row[sex];
}
 ?>
Copy code
include("conn.php"); <🎜> $pagesize=5; 5 $url=$_SERVER["REQUEST_URI"]; <🎜> $url=parse_url($url); <🎜> $url=$url[path]; <🎜> $numq=mysql_query("SELECT * FROM `test`"); <🎜> $num = mysql_num_rows($numq); <🎜> if($_GET[page]){ <🎜> $pageval=$_GET[page]; <🎜> $page=($pageval-1)*$pagesize; <🎜> $page.=',';<🎜> } <🎜> if($num > $pagesize){ if($pageval<=1)$pageval=1;<🎜> echo "Total $num items". 21 " Previous page Next page"; } $SQL="SELECT * FROM `test` limit $page $pagesize "; $query=mysql_query($SQL); while($row=mysql_fetch_array($query)){ echo "
".$row[name]." | ".$row[sex]; } ?>

Paging formula: (current page number - 1) * number of items per page, number of items per page

 代码如下 复制代码
sql语句:select * from test_table limit ($page-1)*$pageSize,$pageSize;

Summary:

No matter what program is developed, it is divided into one. The original method is to take N items starting from Take 5 out of 1.

Let’s introduce the core code. Here we get the paging number, and the Xpagesize code is as follows

 代码如下 复制代码
if($_GET[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;


It is relatively easy to divide in mysql+php because of limit

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631629.htmlTechArticleIt is relatively simple to implement paging in php+mysql. Just get the page and then Paging can be perfectly realized by using limit n, M. This example clearly explains the need...
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