A brief talk on PHP fifth---page nine yang Martial _php tutorial

Source: Internet
Author: User
In the above http://www.BkJia.com/kf/201204/128413.html I told you the recursive function of the operation mechanism and use, do not know whether we understand, if not understand can be in the following thread to ask questions, I will always help you answer.

These two days there are many students in the question of the time on the page in addition to the problem, then today I will give you the details of the application of the paging technology:

We often see beautiful pagination in some Web applications,

Click on the next page will show the contents of the next page, so that the page is not a lot of beautiful.

Well, after reading the explanation of this article I believe that students can also write beautiful pagination to

Now I have a data table with the table named MyTable, with the following data:



ID Name pass Age sex
1 dog eggs wrw7921sqa5eb72c92a549dd5avfr112 20 1
2 Gaofu vdf79218965eb72c92a549dd5a334534 20 0
3 Santiago gbj792saa65eb72c92a549dd5avfd321 22 1
4 Brother Lian 45379218965eb72ccdd549dd5a330342 24 1
5 Lampbrothers cdc79218965eb72c92a549dd5vsfdwsx 24 1
6 Lamp ret79218965eb72c92a549dd5a33bgyt 25 0
7 PHP 123qwe18965eb72c92a549dd5a330875 26 1
8 MySQL 76E79218965EB72C92A549DDVFDD0DSA 28 0
9 Apache 54EBNM18965EB72C92A549DD5A330CXZ 30 1


First, let's make a paging link
echo "Previous page
";
echo "Next page
";
echo "Total clause";
?>

Let's think about it, click on the previous page, the current page number minus one, and will show the previous page of data

Assuming that the current page number we use a variable $page to control, then the previous page is not supposed to be $page-1

Next page number is $page+1

So the above link should read:
echo "Previous page
";
echo "Next page
";
?>


So everyone should understand, how to pass the page and the next page of the page

But in the actual development, a Web software can not let the user to pass the page number, the user accesses the page only then accesses to the index.php

So we will deal with this situation:
$page =!empty ($_get[' page ')? $_get[' Page ']:1
?>

At this time our $page if not given, that is, $_get[' page ' is empty, we directly to the $page assigned to a value of 1

Next, we will show the data separately, that is, the first page only shows the first page of data, the second page only shows the second page of data .... Page n only shows data on page n

In this case, we need to operate on the SQL statement, we use the limit clause of the SQL statement if we want to display the data paginated

OK, so we thought, what should the SQL statement look like if the data for each page only shows 5 records?

Well, our

First page:
The first and fifth data displayed should be "limit 0,5";

Second page:
The data shown in articles sixth through tenth should be "limit 5,5";

The third page:
The data shown in articles 11th through 15th should be "limit 10,5";

..............

We look at a pattern,

Carefully observed that our nth page is not supposed to be

Limit (n-1) * Number of bars displayed per page, number of bars per page

That is, the data we need has the current page number and the number of bars displayed per page

OK, we just said, the current page number we can be obtained by $_get[' page ', then each page shows the number of bars we can use a variable to define, the skilled classmate can define a constant, put it into the configuration file,

Here we use variables to define, set 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 page out the data,

But if you want to get a total of how many 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 use the mysql_result () function to take out the results of the execution.

$count = mysql_result ($result, 0);

Well, the current total number of our records is $count,

Now our total number of data is 9, each page shows 5, you think, our total number of pages should be 2 pages, that is Ceil (9/5) =2

The total page calculation formula is ceil ($count/$pagesize),


Well, all the data we need has been obtained, eventually assembled into SQL statements, and displayed, should be:
Set how many records are displayed per page
$pagesize = 5;
Get 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);
Determines 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 table
echo "















"; Echo" "; echo" "; echo" ";//define the gender array $sex = array (' Male ', ' female ');//traverse the output result set while ($row = Mysql_fetch_assoc ($result)) {echo" "; Echo" "; Echo" "; Echo" "; [Backcolor= #ffffff] [Color= #008ef1]echo] "; [/color] [/backcolor]} echo "
User name age gender
". $row [' name ']."". $row [' age ']."". $sex [$row [' sex ']]."
";
Add paging to show connections
$up = (($page-1) <1)? 1: ($page-1);
$down = (($page + 1) > $page _count) $page _count: ($page + 1);
echo "Prev";
echo "Next page";
echo "Total". $page _count. " Page ";
echo "Current section". $page. " Page ";
echo "Total". $count. " Record ";
?>
OK, so we have to add the paging function, how, is not very simple.

Let me summarize the specific steps of the next page:

First step: Set the page size (how many bars are displayed per page)

Step two: Get the current page number

Step three: Get the total number of record bars

Fourth step: Get the total number of pages

Fifth step: Determine whether the current given page number is out of bounds

Sixth step: Assemble SQL statements

Seventh step: Send SQL statements to MySQL server

Eighth step: Traverse result set output

Nineth Step: Add Paging link

Just remember the above Jie brother nine Yang martial arts cheats, I believe students will be able to leisurely gallop PHP's Wulin killing field



Author Zdrjlamp

http://www.bkjia.com/PHPjc/478277.html www.bkjia.com true http://www.bkjia.com/PHPjc/478277.html techarticle in the above http://www.2cto.com/kf/201204/128413.html I told you the recursive function of the operation mechanism and use, do not know whether we understand, if not understand can be in the following thread to ask questions, I ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.