Paging is basically used by every project, so encapsulate it into a tool class and call it directly in the future. (Although the TP framework is very powerful, it is not a bad idea to seal one yourself.) , which saves time and effort and earns 'work points'.
The paging tool class I sealed is relatively complete. It has the homepage, previous page, next page, last page and an optional number of page numbers (that is, when there are many pages, only the parameters passed in are displayed. (the number of entries), you can also choose whether to use the drop-down page jump function according to your needs.
<?php /** * 分頁工具類 */ class page{ /** * 返回分頁所需字符串 * @param $pageNum int 顯示的頁碼 * @param $pageCount int 總的頁碼數(shù) * @param $url string 當(dāng)前頁碼路徑 * @param $Count string 頁碼列表里顯示的個(gè)數(shù) * @param $selected bool 是否開啟下拉框選擇功能。默認(rèn)不開啟 * return $strpage string 分頁字符串 */ function showPage($pageNum,$pageCount,$url,$Count = 5,$selected = false){ $pageMaxVal = $Count - 1;// 頁碼到達(dá)最大時(shí),頁碼列表里第一個(gè)頁碼與最大頁碼的差 $pageMiddleVal = ceil($Count / 2);// 依據(jù)需要顯示頁碼個(gè)數(shù)得到的中間值 $pageMiddleCha = floor($Count / 2);// 依據(jù)需要顯示頁碼個(gè)數(shù)得到的當(dāng)前頁碼與第一個(gè)頁碼的差 // 判斷url是否已經(jīng)存在? if(!strpos($url, '?')){ // 未存在? $url .= '?'; }else{ $url .= '&'; } // 首頁 $strpage = "<a href='{$url}num=1'>首頁</a>"; // 上一頁 $preNum = $pageNum > 1 ? $pageNum - 1 : 1; // 當(dāng)頁碼為第一頁時(shí)取消上一頁按鈕的功能 $strpage .= $pageNum == 1 ? "<a class='unshow'>上一頁</a>" : "<a href='{$url}num={$preNum}'>上一頁</a>"; // 顯示的頁碼列表中的第一個(gè)頁碼 $startPage = $pageNum > $pageMiddleVal ? $pageNum - $pageMiddleCha : 1; // 起始頁碼最大值 = 總頁數(shù) - 4 if($pageCount - $pageMaxVal > 0 && $startPage > $pageCount - $pageMaxVal){ $startPage = $pageCount - $pageMaxVal; } // 顯示的頁碼列表中的最后一個(gè)頁碼 $endPage = $startPage<($pageCount - $pageMaxVal) ? $startPage + $pageMaxVal : $pageCount; // 中間數(shù)字頁碼 for($i=$startPage;$i<=$endPage;$i++){ if($i == $pageNum){ $strpage .= "<span>$i</span>"; }else{ $strpage .= "<a href='{$url}num=$i'>$i</a>"; } } // 下一頁 $nextNum = $pageNum < $pageCount ? $pageNum + 1 : $pageCount; // 當(dāng)頁碼為最后一頁時(shí)取消下一頁按鈕的功能 $strpage .= $pageNum == $pageCount ? "<a class='unshow'>下一頁</a>" : "<a href='{$url}num={$nextNum}'>下一頁</a>"; // 末頁 $strpage .= "<a href='{$url}num={$pageCount}'>末頁</a>"; // 總頁數(shù) $strpage .= "總頁數(shù):{$pageCount} "; // 判斷是否是否選擇頁碼功能 if($selected){ // 選擇了 $strpage .= "當(dāng)前頁碼:<select onchange=\"location.href='{$url}num=' + this.value \">"; for($i = 1; $i <= $pageCount;$i++){ if($i == $pageNum){ $strpage .= "<option value='$i' selected='selected'>$i</option>"; }else{ $strpage .= "<option value='$i'>$i</option>"; } } $strpage .= "</select>"; } return $strpage; } }
The rest of the MySQL knowledge should be posted next. I hope everyone will continue to pay attention.
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more articles related to commonly used PHP packaging paging tools, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre
