


How to implement PHP page turning and jumping function, PHP page turning and jumping implementation_PHP tutorial
How to implement page turning and jumping function in PHP, implementation of page turning and jumping in PHP
We all know that it is very simple and interesting to use php mysql to display all database information on a web page , the page display is still satisfactory when there is very little database information, but when there is a lot of database information, the page display will become very bad. Let's introduce how to display the current page data information. And how to implement dynamic flip function.
Here we will introduce the implementation of two page turning display functions:
First, let’s 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 does not play a role in the page turning function. The following expanded syntax is the core function of realizing page turning:
mysql_query("select * from table order by id desc limit $start,$limit");
Here $start is the starting row of the database search, $limit is the search starting from the starting row and ending with $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 page turning backward. This is the page turning function of the special news and download center of this site.
First introduce the idea of realizing the page turning function:
- First determine the number of data records fixedly displayed on the current page. Assume it is 20 records. Set the value of $limit to 20: $limit=20;
- When displaying database records, they must start from the first one, so the initial value of $start is set here to 0: $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; while when turning the page forward, $start regularly subtracts $limit. :$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 for turning forward and turning backward has been completed, but 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 the page (I always call 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 loop page turning function.
Cyclic page turning is achieved by turning forward and backward together with numbers. The specific expression is:
page: prve > next
The numbers inside represent each current page. prve and next are not just forward and backward flips of the current page, but more complex digital control of forward and backward flips.
As always, before proceeding with program design, 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 and ideas studied here may be more complicated. abstract.
First, 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 display result is as follows:
Page: 0 1 2 3 ......... 19 >> next
Display result after flipping:
page: prve > next
Okay, 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 the fixed display variable:
$limit=20;
Settings of database initial variables:
$start=0;
The total number of database records is:
$num;
Page number variable:$page;
The program for displaying a page number cycle 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; //退出循环 } } ?>
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 It is used to control digital page turning. Now you can take a look at the complete code page.php that implements 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'page.php?"; echo "start=$start&s=$s>next</a></td>"; } //注意跳出循环的控制 break; } } echo "</tr></table>"; ?>
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 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.

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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.

WebStorm Mac version
Useful JavaScript development tools
