亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP development article publishing system paging program

Paging program

The main principles of paging are shown in the figure below:

分頁(yè)程序邏輯.png

Note: Define the number of data items to be displayed on each page in advance (assuming three pieces of data on one page), and calculate the total number of pages based on the total number of data items in the database.

The output page uses the limit keyword of the database query statement. The first page is the 3 pieces of data starting from the 0th piece of data, the second page is the 3 pieces of data starting from the 3rd piece of data, and the nth page is the 3 pieces of data starting from the (n- 1) *3 pieces of data starting from 3 pieces of data

Now that the principle has been explained, let’s take a look at the actual program. The comments in the program are very clear. You can observe and understand line by line

<?php
//分頁(yè)功能
//連接數(shù)據(jù)庫(kù)
require_once("connect.php");
$page = isset($_GET['page'])?intval($_GET['page']):1;//設(shè)置當(dāng)前頁(yè)數(shù),沒(méi)有則設(shè)置為1
$num=3;//每頁(yè)顯示3條數(shù)據(jù)
/*
首先我們要獲取數(shù)據(jù)庫(kù)中到底有多少數(shù)據(jù),才能判斷具體要分多少頁(yè),總頁(yè)數(shù) 具體的公式就是
總數(shù)據(jù)數(shù) 除以 每頁(yè)顯示的條數(shù),有余進(jìn)一 。
也就是說(shuō)10/3=3.3333=4 有余數(shù)就要進(jìn)一。
*/
$sql="select * from article";
$result=mysqli_query($conn,$sql);
$total=mysqli_num_rows($result);//查詢(xún)數(shù)據(jù)的總條數(shù)
$pagenum=ceil($total/$num);//獲得總頁(yè)數(shù)
//假如傳入的頁(yè)數(shù)參數(shù)page 大于總頁(yè)數(shù) pagenum,則顯示錯(cuò)誤信息
if($page>$pagenum || $page == 0){
       echo "<script>alert('沒(méi)有內(nèi)容了');history.go(-1);</script>";
       exit;
}
 $offset=($page-1)*$num;        
/* 獲取limit的第一個(gè)參數(shù)的值 offset ,假如第一頁(yè)則為(1-1)*10=0,第二頁(yè)為(2-1)*10=10。             (傳入的頁(yè)數(shù)-1) * 每頁(yè)的數(shù)據(jù) 得到limit第一個(gè)參數(shù)的值*/
$sql="select * from article limit $offset,$num ";
$info=mysqli_query($conn,$sql);   //獲取相應(yīng)頁(yè)數(shù)所需要顯示的數(shù)據(jù)
//獲取最新添加的前六條數(shù)據(jù)
$sql_new="select  id,title from article order by dateline desc limit 0,6 ";
$info_title=mysqli_query($conn,$sql_new);
?>


Continuing Learning
||
<?php //分頁(yè)功能 //連接數(shù)據(jù)庫(kù) require_once("connect.php"); $page = isset($_GET['page'])?intval($_GET['page']):1;//設(shè)置當(dāng)前頁(yè)數(shù),沒(méi)有則設(shè)置為1 $num=3;//每頁(yè)顯示3條數(shù)據(jù) /* 首先我們要獲取數(shù)據(jù)庫(kù)中到底有多少數(shù)據(jù),才能判斷具體要分多少頁(yè),總頁(yè)數(shù) 具體的公式就是 總數(shù)據(jù)數(shù) 除以 每頁(yè)顯示的條數(shù),有余進(jìn)一 。 也就是說(shuō)10/3=3.3333=4 有余數(shù)就要進(jìn)一。 */ $sql="select * from article"; $result=mysqli_query($conn,$sql); $total=mysqli_num_rows($result);//查詢(xún)數(shù)據(jù)的總條數(shù) $pagenum=ceil($total/$num);//獲得總頁(yè)數(shù) //假如傳入的頁(yè)數(shù)參數(shù)page 大于總頁(yè)數(shù) pagenum,則顯示錯(cuò)誤信息 if($page>$pagenum || $page == 0){ echo "<script>alert('沒(méi)有內(nèi)容了');history.go(-1);</script>"; exit; } $offset=($page-1)*$num; /* 獲取limit的第一個(gè)參數(shù)的值 offset ,假如第一頁(yè)則為(1-1)*10=0,第二頁(yè)為(2-1)*10=10。 (傳入的頁(yè)數(shù)-1) * 每頁(yè)的數(shù)據(jù) 得到limit第一個(gè)參數(shù)的值*/ $sql="select * from article limit $offset,$num "; $info=mysqli_query($conn,$sql); //獲取相應(yīng)頁(yè)數(shù)所需要顯示的數(shù)據(jù) //獲取最新添加的前六條數(shù)據(jù) $sql_new="select id,title from article order by dateline desc limit 0,6 "; $info_title=mysqli_query($conn,$sql_new); ?>
submitReset Code