search
HomeBackend DevelopmentPHP TutorialPHP page jump function implementation method example code

This article mainly introduces how to implement the page flip function in PHP. Next, we will introduce how to display the number of data on the current page and how to implement the dynamic flip function. Friends who need it can refer to it

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 display situation will become very bad. Let's introduce how to display the amount of data on the current page and how to implement the dynamic flip function.
Here we will introduce the implementation 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 very familiar. It is used to search records and display them in reverse order. However, it does not work in the page turning function. The following expanded syntax is the implementation. The core function of page turning:

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

The $start here is the starting row of the database search, and the $limit is the search starting from the starting row and ending with $limit records. Okay, after having 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 page turning backward. This is the page turning function of the unusual news and download center of this site.
First introduce the idea of ​​​​implementing the page turning function:

  • First determine the number of data records fixedly displayed on the current page, assuming it is 20 records. Set the value of $limit to 20: $limit=20;

  • When displaying database records, they must be displayed from the first one, so the initial value of $start is set to 0 here. :$start=0;

  • 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; When turning the page forward, $start will subtract $limit regularly: $start-$limit;

After having the above ideas, you can start designing the program

page.php:

<? 
//设置当前页显示的数量(这个数量可任意设置) 
$limit=20; 
//初始化数据库搜索起始记录 
if (!emptyempty($start)) $start=0; 
mysql_connect("localhost","",""); 
mysql_select_db(database); 
//设置数据库记录总数 
$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>翻页功能</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>"; 
//设置向前翻页的跳转 
$prve=$start-$limit; 
if ($prve>=0) { 
echo "<a href=page.php?start=$prve>prve</a>"; 
} 
//设置向后翻页的跳转 
$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, but this function will be very cumbersome when processing more data to display. The following is We will continue to introduce a more powerful and complex page turning function - loop page turning (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 special forums and articles on this site use this circular page turning function.
The circular page turning is achieved by turning forward and backward together with numbers. The specific The expression is:
page: prve > next
The numbers inside For each current page, prve and next are not just forward and backward flips of the current page, but more complex digitally controlled front and back flips.

As always, before proceeding with programming, let’s clarify our ideas first. I suggest readers to practice it themselves after reading how to implement the page turning function, because some of the methods studied here are The ideas may be relatively abstract.
First of all, we boldly assume that there are more than 1,000 records in the database. We want to currently display 25 records, and the number flip control is 20, so the following display results will be shown:
Page : 0 1 2 3 ......... 19 >> next
The displayed result after flipping:
page: prve > next
Okay, let’s take a look at the rules. A fixed number displays the number 25, and a fixed number controls the times. Turn 20. We can use these two numbers to realize the loop page turning function;
First set the fixed display variables:
$limit=20;

Settings of the initial variables of the database:
$start=0;

The total number of database records is:

$num;
Page number variable:$page;
A page number is displayed in a loop The program 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; //退出循环 
} 
} 
?>

In addition to 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. $ is used here. s to mark, this variable is used to control the digital loop page turning control. Now you can look at the complete code page.php that implements the loop page turning:

<? 
$limit=25; 
if (!emptyempty($start)) $start=0; 
if (!emptyempty($s)) $s=0; 
mysql_connect("localhost","",""); 
mysql_select_db(database); 
//统计数据库记录总数 
$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>"; 
//数字循环翻页的控制 
echo "<table>"; 
echo "<tr><td>页:</td>"; 
//前翻控制 
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>"; 
//设置当前页对应页数无链接功能 
$star=$start; 
//注意循环的初始附值,仔细想想为什么不是 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>"; 
//控制数字页面限制显示功能,控制只显示 20 页 
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&#39;page.php?"; 
echo "start=$start&s=$s>next</a></td>"; 
} 
//注意跳出循环的控制 
break; 
} 
} 
echo "</tr></table>"; 
?

There is also a page turning function that is to submit page turning. That is, add data submission to the submission form, and then the program jumps to the corresponding page. This function is relatively simple to implement and is left to the reader to complete.

The above program can already complete the powerful page turning function. You can study it carefully and truly apply what you have learned.

The above is the detailed content of PHP page jump function implementation method example code. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.