PHP Single Row from Zero (14) Principle and Implementation of Data Paging Display_PHP Tutorial

WBOY
Release: 2016-07-13 10:30:34
Original
762 people have browsed it

Paging display is one of the most frequently processed links in WEB programming. The so-called paging display is to display the result set piece by piece through the program. To realize paging display, two initial parameters are required: how many records are displayed on each page and what page is the current page. Coupled with the complete result set, paginated display of data can be achieved. As for other functions, such as previous page, next page, etc., they can all be processed based on the above information.

To get the first 10 records in a table, you can use the following SQL statement:

SELECT * FROM a_table LIMIT 0,10
Copy after login

To find the 11th to 20th records, the SQL statement used is as follows:

SELECT * FROM a_table LIMIT 10,10
Copy after login

To find the 21st to 30th records, the SQL statement used is as follows:

SELECT * FROM a_table LIMIT 20,10
Copy after login

It can be seen from the above SQL statement that fetching 10 records each time is equivalent to displaying 10 pieces of data on each page, and there is such a relationship between the starting position of the record to be obtained each time and the number of pages in the current period: Starting position = (current page number - 1) * number of records to be displayed on each page. If the variable $page_size represents the number of records displayed on each page, and the variable $cur_page represents the current page number, then the above can be summarized using the SQL statement template shown below:

select * from table limit ($cur_page-1)*$page_size,$page_size;
Copy after login

In this way, you get the SQL statement for obtaining data under paging. Among them, $page_size can be set as a fixed value according to the actual situation. In actual development, the current page $cur_page can be passed in by parameters. In addition, the total number of pages for which data is to be displayed can be calculated between the total number of records and the number of records displayed on each page. For example, if there is no remainder after dividing the total number of records by the number of records displayed on each page, then the total number of pages is the quotient of the two.

<?php
$host='localhost';
$user_name='root';
$password='helloworld';
$conn=mysql_connect($host,$user_name,$password);
if(!$conn)
{
	die('FAIL!'.mysql_error());
}
mysql_select_db('test');
if(isset($_GET['page']))
{
	$page=$_GET['page'];
}
else
{
	$page=1;
}
$page_size=2;
$sql='select * from users';
$result=mysql_query($sql);
$total=mysql_num_rows($result);
if($total)
{
	if($total<$page_size)
	$page_count=1;
	if($total%$page_size)
	{
		$page_count=(int)($total/$page_size)+1;
	}
	else
	{
		$page_count=$total/$page_size;
	}
}
else
{
	$page_count=0;
}
$turn_page='';
if($page==1)
{
	$turn_page.='Index | Before |';
}
else
{
	$turn_page.='Index | Before |';
}
if($page==$page_count || $page_count==0)
{
	$turn_page.='Next | Last';
}
else
{
	$turn_page.=' Next  |  Last ';
}
$sql='select id,name,sex,age from users limit '.($page-1)*$page_size.','.$page_size;
$result=mysql_query($sql) OR die ("<br/>ERROR:<b>".mysql_error()."</b><br/>SQL:".$sql);
?>
<html>
<head>
<title>13-8.php</title>
</head>
<body>
<table width="75%" border="0" cellpadding="0" cellspacing="1" bgcolor="#7b7b84">
<tr bgcolor="#8bbcc7">
<td height="33"><div align="center"><strong>ID</strong></div></td>
<td><div align="center"><strong>Name</strong></div></td>
<td><div align="center"><strong>Sex</strong></div></td>
<td><div align="center"><strong>Age</strong></div></td>
</tr>
<?php
if($num=mysql_num_rows($result))
{
	while($row=mysql_fetch_array($result,MYSQL_ASSOC))
	{
		?>
		<tr bgcolor="#FFFFFF">
		<td height="22" align="right"><?php echo $row['id']; ?> </td>
		<td height="22"> <?php echo $row['name']; ?> </td>
		<td height="22"> <?php echo $row['sex']; ?> </td>
		<td height="22"> <?php echo $row['age']; ?> </td>
	</tr>

	<?php

	}
}
echo $turn_page;
mysql_close($conn);
?>
</body>
</html>
Copy after login

************************

POST GET are two ways to submit a form. To pass the value via GET, use $_GET to get it. To submit the form via POST, use $_POST
The difference between post and get is that one displays the parameters in the address bar and the other does not

For example, if you use the get method when logging in, your value will be displayed on the address bar, so there is no security at all
And when you search or have a page number, use post to hide the parameters in the address bar, which is meaningless

And use $_GET to get the value of the parameter on the browser address bar (a string of characters after the question mark), such as www.baidu.com/s?wd=123, then you can use $_GET to get the parameter (you It can be understood as an event, action, or parameter. This value is consistent with the name of the input when passing the form) is the value of wd 123. Multiple parameters are connected with the ampersand. For example, ?an=0&si=5 is understood as the value of the an parameter. 0 and the value of the si parameter is 5.

**********************

For example, you enter an address called www.iron-feet.cn/?page=2
$_GET["page"] is to get the value of the page on the address, that is, get 2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/765065.htmlTechArticlePagination display is one of the most frequently processed links in WEB programming. The so-called paging display is to display the result set piece by piece through the program. To realize paging display, two initial parameters are required...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template