In the above article http://www.BkJia.com/kf/201204/128413.html I told you about the operating mechanism and use of recursive functions. I don’t know if you understand it. If you don’t understand, you can ask questions below. , I will help you answer it at any time.
In the past two days, many students have added questions to the paging when asking questions. So today I will tell you in detail the application of paging technology:
We often see beautiful pagination in some web applications,
Clicking the next page will display the content of the next page, which makes the page more beautiful.
Okay, after reading the explanation in this article, I believe that students can also write beautiful pagination
Now I have a data table named mytable with the following data:
First, make a paging link
echo "Previous page
";
echo "Next page
";
echo "Total items";
?>
Let’s think about it, click on the previous page, the current page number will be reduced by one, and the data of the previous page will be displayed
Suppose we use a variable $page to control the current page number, then should the previous page be $page-1
The page number of the next page is $page+1
So our link above should be changed to:
echo "Previous page
";
echo "Next page
";
?>
This way everyone should understand clearly how to pass the page numbers of the previous and next pages
But in actual development, it is impossible for a web software to allow users to pass in page numbers. When users visit the page, they will only access index.php
So we will handle this situation accordingly:
$page = !empty($_GET['page'])?$_GET['page']:1
?>
If our $page is not given at this time, that is, $_GET['page'] is empty, we directly assign the value 1 to $page
Next step, let’s display the data separately, that is to say, the first page only displays the data of the first page, and the second page only displays the data of the second page.....Only the Nth page is displayed. Data on page N
In this case, we need to operate the sql statement. If we want to display the data in pages, we use the limit clause of the sql statement
Okay, then we think, if each page of data only displays 5 records, what should the SQL statement look like?
Then ours
First page:
Should the displayed data from the first to fifth items be "limit 0,5";
Second page:
The displayed data from the sixth to the tenth item should be "limit 5,5";
Page 3:
The displayed data from the 11th to 15th items should be “limit 10,5”;
............
Let’s look at a pattern,
Look carefully, should our nth page be
limit (n-1)*Number of items displayed on each page, number of items displayed on each page
In other words, the data we need include the current page number and the number of items displayed on each page
Okay, as we just said, we can get the current page number through $_GET['page'], then we can use a variable to define the number of items displayed on each page. Skilled students can define a constant and put it Go to the configuration file,
Here we use variables to define and set the page size to 5
$page = $_GET['page'];
$pagesize = 5;
The limit clause of the assembled sql statement should be "limit ($page-1)*$pagesize,$pagesize"
At this point, we have been able to paginate the data,
But if we want to get the total number of pages, we also need to count the total number of records from the database
$sql = "select count(*) from mytable";
$result = mysql_query($sql);
We can take out the execution result using the mysql_result() function.
$count = mysql_result($result,0);
Okay, then our current total number of records is $count,
Now our total number of data items is 9, and each page displays 5 items. Think about it, should our total number of pages be 2, that is, ceil(9/5)=2
The formula for calculating the total number of pages is ceil($count/$pagesize),
Okay, all the data we need has been obtained, finally assembled into a sql statement and displayed, it should be:
//Set how many records to display on each page
$pagesize = 5;
//Get the current page number
$page = !empty($_GET['page'])?$_GET['page']:1;
//Total number of records
$sql = "select count(*) from mytable";
$result = mysql_query($sql);
$count = mysql_result($result,0);
//How many pages are there in total
$page_count = ceil($count/$pagesize);
//Determine whether the current given page number is out of bounds
if($page<1){
$page=1;
}elseif($page>$page_count){
$page=$page_count;
}
//Start assembling sql statements
$sql = "select id,name,age,sex from mytable limit ".($page-1)*$pagesize.','.$pagesize;
//Send sql statement
$result = mysql_query($sql);
//Output form
echo "
Username | Age | Gender | ";
---|---|---|
".$row['name']." | ";".$row['age']." | ";".$sex[$row['sex']]." | ";
Let me summarize the specific steps of paging:
Step one: Set the page size (how many items to display on each page)
Step 2: Get the current page number
Step 3: Get the total number of records
Step 4: Get the total number of pages
Step 5: Determine whether the current given page number is out of bounds
Step 6: Assemble sql statement
Step 7: Send sql statement to mysql server
Step 8: Traverse the result set and output
Step 9: Add pagination links
As long as you remember the above martial arts secrets of Jie Ge’s nine-yang magic skill, I believe that students will be able to leisurely ride through the martial arts killing fields of PHP
Author zdrjlamp