We all know that it is very simple and interesting to use php+mysql to display all database information on a web page. When the database information is very small, the page display is still satisfactory, but when there is a lot of database information, the page display The situation will become very bad. Let's introduce how to display the number of data on the current page and how to implement the dynamic flip function.
Here we will introduce the implementation of two page flip display functions:
-------------------------------------------------- ----------
Let me first introduce the database syntax used in page turning:
mysql_query("select * from table order by id desc");
This database statement is very familiar. It is used to search for records and display them in reverse order, but it cannot It plays a role in the page turning function, and the following expanded syntax is the core function of page turning:
mysql_query("select * from table order by id desc limit $start,$limit");
where $start is the database The starting line of the search, $limit, is to search from the starting line to the end of $limit records. Well, with this core function, we can start the page turning function;
-------------------------------------------------- ----------
The first page turning function:
The function introduced here is the simplest one among the page turning functions. It can only realize page turning forward and backward. The page turning function of the special news and download center of this site is this.
First, we will introduce the implementation of the page turning function. Ideas:
First determine the number of data records that are fixedly displayed on the current page. Assume that there are 20 records. Set the value of $limit to 20: $limit=20;
when displaying database records, they must start from the first one, so set $start here. The initial value of is 0: $start=0;
and the realization of the page turning function relies on the dynamic change of $start. When turning the page backward, $start regularly adds $limit: $start+$limit; and when turning the page forward $start then subtracts $limit regularly: $start-$limit;
with the above ideas, you can start designing the program page.php:
<?
//Set the number displayed on the current page (this number can be set arbitrarily)
$limit=20;
//Initialize database search starting record
if (!empty($start)) $start=0;
mysql_connect("localhost","","");
mysql_select_db(database);
//Set the total number of database records
$result=mysql_query("select * from table");
$num_max=mysql_numrows($result);
$result=mysql_query("select * from table order by id desc limit $start,$limit);
$num=mysql_numrows($result);
echo "<table><tr><td>Page turning function</td></tr>";
if (!empty($num)) {
for ($i=0;$i<$num;$i++) {
$val=mysql_result($result,$i,"val");
$val1=mysql_result($result,$i,"val1");
echo "<tr><td>$val</td><td>$val1</td></tr>";
}
}
echo "<tr><td>";
//Set the page forward jump
$prve=$start-$limit;
if ($prve>=0) {
echo "<a href=page.php?start=$prve>prve</a>";
}
//Set the jump to page backwards
$next=$start+$limit;
if ($next<$num_max) {
echo "<a href=page.php?start=$next>next</a>";
}
echo "</td></tr></table>";
?>
A program with forward and backward flip functions has been completed. However, this function will be very cumbersome when processing more data to display. The following will continue to introduce a more powerful and complex page turning function - loop turning. Page (I have always called it this, because I can’t find a more appropriate name).:)
The simple page turning function was introduced earlier. The page turning function introduced below is more powerful and complex. The very forum and very special forum on this site The article uses this loop page turning function.
Cyclic page turning is realized by turning forward and backward together with numbers. The specific expression is:
page: prve <<1 2 3 4 ....... 20 >>
The numbers in next represent each current page. The forward prve and backward flips are no longer just forward and backward flips of the current page, but more complex digitally controlled front and back flips.
As always, before proceeding with program design, please clarify your ideas first. I recommend that readers read how After realizing the function of turning pages in a loop, you can practice it yourself, because some of the methods and ideas studied here may be relatively abstract.
First, we boldly assume that there are more than 1,000 records in the database. We hope that 25 records are currently displayed, and the number The flip control is 20, so there is the following display result:
page: 0 1 2 3 ......... 19 >> next
display result after flipping:
page: prve <<20 27 28 ... .... 49 >> nextOkay
, let’s take a look at the rules. A fixed display number is 25, and a fixed number controls the doubling of 20. We can use these two numbers to implement the page turning function;
first set Fixed displayed variables:
$limit=20;
Database initial variable settings:
$start=0;
The total number of database records is: $num;
Page number variable: $page;
The program for a page number loop display is as follows:
<?
...
$result=mysql_query("select * from table");
$num=mysql_numrows($result);
for ($page=0;$page<($num/$limit);$page++) {
echo $page;
if ($page>0 && ($page%20)==0) {
break; //Exit the loop
}
}
?>
Except for displaying numbers, this code does not implement any other functions. Because there are more numbers to control flipping, several variables must be used to mark and identify these control quantities. $s is used to mark them here; this variable is used To control the digital page turning control, now you can look at the complete code page.php that implements the page turning cycle:
<?
$limit=25;
if (!empty($start)) $start=0;
if (!empty($s)) $s=0;
mysql_connect("localhost","","");
mysql_select_db(database);
//Statistical total number of database records
$result=mysql_query("select * from table");
$num=mysql_numrows($result);
$result=mysql_query("select * from table order by id limit $start,$limit");
$numb=mysql_numrows($result);
echo "<table>";
if (!empty($numb)) {
for($i=0;$i<$numb;$i++) {
$val=mysql_result($result,$i,"val");
$val1=mysql_result($result,$i,"val1");
echo "<tr><td>$val</td><td>$val1</td></tr>";
}
}
echo "</table>";
//Control of digital page turning
echo "<table>";
echo "<tr><td>Page:</td>";
//Flip forward control
if ($s>20) {
if ($s==21) {
$st=$s-21;
} else {
$st=$s-20;
}
$pstart=$st*$limit;
echo "<td><a href=page.php?";
echo "start=$pstart&s=$st>prve</a></td>";
}
echo "<td> >></td>";
//Set the number of pages corresponding to the current page without link function
$star=$start;
//Pay attention to the initial value of the loop and think carefully about why it is not 0
for ($page=$s;$page<($num/$limit);$page++) {
$start=$page*$limit;
echo "<td>";
if($page!=$star/$limit) {
echo "<a href=page.php?";
echo "start=$start&s=$s>";
}
echo $page;
if($page!=$star/$limit) {
echo "</a>";
}
echo "</td>";
//Control the digital page limit display function, control to display only 20 pages
if ($page>0 && ($page%20)==0) {
if ($s==0) {
$s=$s+21;
} else {
$s=$s+20;
}
$start=$start+$limit;
if ((($num/$limit)-1)>$page) {
echo "<td> <<</td><td><a href'page.php?";
echo "start=$start&s=$s>next</a></td>";
}
//Pay attention to the control of jumping out of the loop
break;
}
}
echo "</tr></table>";
?>
The above program can complete a powerful loop page turning function.
There is also a page turning function that is submission page turning, that is, adding data submission in the submission form, and then the program jumps to the corresponding page. This function is relatively easy to implement. It's simple, I'll leave it to the readers to complete it themselves, and I won't introduce it here...