Home>

We all know that using php + mysql to display all database information on web pages is very simple and interesting.The page display is satisfactory when there is little database information.But when there are a lot of database data,The display of the page will become very bad,Here's how to implement the display of the current page data and how to implement the dynamic flip function.

will be introduced hereRealization of two page turning display functions:

First introduce the database syntax used in page turning:

mysql_query ("select * from table order by id desc");

This database statement is too familiar,Is used to search records and display them in reverse order,But it does not work in the page turning function.The following extended syntax is the core function of page turning:

mysql_query ("select * from table order by id desc limit $start, $limit");

Here $start is the starting line of the database search,$limit is the search from the start line to the end of $limit records,Okay, with this core function,We can start the page turning function.

The first page turning function:The function introduced here is the simplest of the page turning functions.Only page forward and page backward can be realized.This page's very news and download center page turning function is such.

First introduce theof page turning functionIdea:

First determine the number of data records displayed on the current page.Assuming 20 records, set the value of $limit to 20:$limit=20; When displaying database records,Must be displayed from the first one,So here the initial value of $start is set to 0:$start=0; The implementation of the page turning function depends on the dynamic change of $start,$Start regularly adds $limit:$start + $limit when page backwards, and $start:$start- $limit is regularly subtracted when pages forward

With the above ideas,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 start record
if (! emptyempty ($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 (! emptyempty ($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 backward
$next=$start + $limit;
if ($next<$num_max) {
echo "<a href=page.php?start=$next&next</a>";
}
echo "</td></tr></table>";
?>

A forward turn,The procedure of the backward function is completed,But when this function is used to process more data,Will be cumbersome.The following will continue to introduce more powerful functions,More complicated page turning function-cycle page turning (I always call it like 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,more complicated,The very forums and articles on this site use this page turning function.

The page turning is realized by turning forward and back together with numbers.The specific expression is:

Page:prve<<1 2 3 4 ....... 20>next

The numbers inside represent the current pages,Turning forward prve and turning next is not just flipping the current page forward or backward,Instead, more complex digital controls flip back and forth.

As always,Before programming,First sort out the ideas,I suggest that after reading how to implement the function of page turning,Be able to do it yourself,Because some of the methods and ideas studied here may be more abstract.

First we boldly assume that there are more than 1000 records in the database.We want to display 25 records currently, and the digital flip control is 20, so we have the following display results:

Page:0 1 2 3 ......... 19>>next

Display results after turning back:

Page:prve<<20 27 28 ....... 49>nextOkay, let's take a look at the laws.A fixed display number 25, a fixed number to control the doubling 20. We can use these two numbers to implement the cycle page turning function;

First set the variables to be fixed:

$limit=20;

Setting of database initial variables:

$start=0;

The total number of database records is:

$num;

Page number variable:$page;

The procedure for cycling through a page 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
}
}
?>

In addition to displaying this number,None of the other functions have been implemented.Because there are more digitally controlled flips,So there must be several variables to mark and identify these controls,Here is marked with $s, this variable is used to control the number of page rotation control,Now take a look at the complete code page.php that implements page rotation:

<?
$limit=25;
if (! emptyempty ($start)) $start=0;
if (! emptyempty ($s)) $s=0;
mysql_connect ("localhost", "", "");
mysql_select_db (database);
//Statistic database records total
$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 (! emptyempty ($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>";
//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;
//Note the initial value of the loop,Think carefully why it's 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 display function of digital pages,Control displays 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<>
echo "start=$start&s=$s>next</a></td>";
}
//Note the control out of the loop
break;
}
}
echo "</tr></table>";
?>

Another page turning function is to submit page turning.Add data submission to the submission form,Then the program jumps to the corresponding page,This function is relatively simple to implement,It is left to the reader to do it themselves.

The above program can already complete a powerful page turning function.You can study it in detail,Really apply what you have learned.

php
  • Previous JS achieve slide text box zoom stretch effect code
  • Next Double colon :: Use case in PHP