Introduction to...LOGIN

Introduction to the simple paging function of developing a simple news release system with PHP

Paging display is a very common method of browsing and displaying large amounts of data, and is one of the most commonly processed events in web programming.

The so-called paging display means that the result set in the database is artificially divided into sections for display. Two initial parameters are required here:

The number of news displayed on each page: $limitNews

Get the current number of pages: $page

Because the computer language defaults to starting from 0 to obtain records

If the number displayed per page is set to 3, $limitNews = 3,

Then the first page $page = 1 will display three records of 0, 1, 2

The second page $page = 2 will display three records of 3, 4, and 5

The third page $page = 3 will display three records of 6, 7, and 8

and so on. . . . . . . . .

Set a parameter $limitFrom from which piece of data to start reading

You can get it from the above rules:

$limitFrom = ($page - 1 ) * $limitNews;

will obtain data starting from the 0th, 3rd, and 6th items respectively.


Next we need to connect to the database and read the data

We still use the database name test and table name new:# created earlier ##

<?php
$link = mysqli_connect('localhost','uesrname','password','test');

$sql = "select * from new";  //  select * from表示获取全部字段的值

$sqlCount = "select count(*) from new";    //count(*)统计的是结果集的总条数
?>


Get the

total number of news in the database table through query statements$countNews

<?php
$retQuery = mysqli_query($link, $sqlCount);  //查询数量sql语句

$retCount = mysqli_fetch_array($retQuery);   //获取数量

$count = $retCount[0]?$retCount[0]:0;   //判断获取的新闻数量

$countNews = $count;
?>


Another parameter needs to be given here

$countPage How many pages are displayed in total

Now another problem arises. If there are 10 news records and 3 records are displayed on each page, then What to do with the remaining one?

We need to use % remainder to make the judgment:

<?php
$countPage = $countNews%$limitNews;   //求余数获取分页数量能否被除尽

if(($countPage) > 0) {  //获取的页数有余
  $countPage = ceil($countNews/$limitNews);  
  
// ceil() 函数向上舍入为最接近的整数,除不尽则取整数+1页, 10个新闻每个页面显示3个,成3个页面,剩余1个单独成1个页面,这样总共有4个页面
} else {
  $countPage = $countNews/$limitNews;  //如果是9个新闻每个页面显示3个,成3个页面
}
?>


Other parameters

Previous page$prev, next page$next

In the paging function, there are often clicks on "previous page" and "next page" to jump

Let me first talk about the idea of ​​​​implementing the PHP code function:

The previous page $prev is the current page $page -1. Jump to the previous page step by step. When the current page $page is the first page, jump forward to page 0. This will obviously cause bugs.

Here we need to give it a setting. When the current page $page is the first page, click "Previous Page" to set it to be displayed as the first page. If not Jumped forward.

$prev = ($page - 1 <= 0 )?1:$page-1;

The next page$next is the current Page $page -1 jumps to the next page step by step. The problem is that when $page is the last page, click "Next Page" to jump back.

is similar to the previous page. We set Set it to display the last page.

$next = ($page + 1 > $countPage)?$countPage:$page+1;

//The current page number must be greater than When the total number of pages is displayed, the current page is displayed.

The page code is shown below:

<!DOCTYPE html>
<html>
<head>  
<meta charset=utf8">  
<title>分表页</title>
</head>
<body>
   <div>
         <a href="?page=<?php echo $prev;?>">|上一页</a>         
         <?php for($i=1; $i<=$countPage; $i++):?>         
         <a href="?page=<?php echo $i;?>"><?php echo $i;?></a>         
         <?php endfor;?>         
         <a href="?page=<?php echo $next;?>">|下一页</a>
   </div>
</body>
</html>

Note: The above code uses a for loop to implement 1,2,3. . . . page effect.

Next Section

<?php $link = mysqli_connect('localhost','usermane','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } $page = isset($_GET['page'])?$_GET['page']:1;//获取当前分页数 $limitNews = 3; //每页显示新闻数量 $countNews = 0; //总共有多少条新闻 $countPage = 0; //一共有多少页数 $limitFrom = ($page - 1) * $limitNews;//从第几条数据开始读记录 //每页显示3个 //page = l limit 0 //page = 2 limit 3 //page = 3 limit 6 $sql = "select * from new"; $sqlCount = "select count(*) from new"; $retQuery = mysqli_query($link, $sqlCount); //查询数量sql语句 $retCount = mysqli_fetch_array($retQuery); //获取数量 $count = $retCount[0]?$retCount[0]:0; //判断获取的新闻数量 $countNews = $count; $countPage = $countNews%$limitNews; //求余数获取分页数量能否被除尽 if(($countPage) > 0) { //获取的页数有余 $countPage = ceil($countNews/$limitNews); // ceil() 函数向上舍入为最接近的整数,除不尽则取整数+1页, 10个新闻每个页面显示3个,成3个页面,剩余1个成1个页面 } else { $countPage = $countNews/$limitNews; } $prev = ($page - 1 <= 0 )?1:$page-1; //上一页 $next = ($page + 1 > $countPage)?$countPage:$page+1; //下一页 $result = mysqli_query($link, $sql); ?> <!DOCTYPE html> <html> <head> <meta charset=utf8"> <title>分页</title> </head> <body> <div> <a href="?page=<?php echo $prev;?>">|上一页</a> <?php for($i=1; $i<=$countPage; $i++):?> <a href="?page=<?php echo $i;?>"><?php echo $i;?></a> <?php endfor;?> <a href="?page=<?php echo $next;?>">|下一页</a> </div> </body> </html>
submitReset Code
ChapterCourseware