


PHP uses the Mysqli class library to achieve perfect paging effect, mysqli class library_PHP tutorial
Jul 12, 2016 am 08:54 AMHow PHP uses the Mysqli class library to achieve perfect paging effects, mysqli class library
The example in this article describes how PHP uses the Mysqli class library to achieve perfect paging effects. Share it with everyone for your reference, the details are as follows:
This article is tailor-made based on my previous article "PHP Database Operation: Database Operation Class Library Based on Mysqli". How to use the FetchAll method in the M class library to create perfect paging.
Pagination is essential in each of our projects, and it appears very frequently. This requires our programmers to implement the paging solution with the fastest speed and the most concise code in the project.
The implementation of paging is mostly based on the parameters passed in the URL (usually page), for example: http://localhost/article.php?page=2 means to get the second page data
Recommendation: When you read this article, please make sure you have studied my last article "PHP Database Operation: Database Operation Class Library Based on Mysqli"
Below we will explain the paging based on the M class library. The code that appears in the blog post is finally accompanied by a download address, including the test database file.
1. Create the configuration file config.inc.php
The code list is as follows
<?php header('Content-Type:text/html;Charset=utf-8'); //設(shè)置header編碼 define('ROOT_PATH', dirname(__FILE__)); //設(shè)置根目錄 define('DB_HOST', 'localhost'); //數(shù)據(jù)庫服務(wù)器地址 define('DB_USER', 'root'); //數(shù)據(jù)庫用戶名 define('DB_PWD', '×××');//數(shù)據(jù)庫密碼,請根據(jù)機器填寫 define('DB_NAME', '×××'); //數(shù)據(jù)庫名稱,請根據(jù)機器填寫 define('DB_PORT', '3306'); //數(shù)據(jù)庫端口,請根據(jù)機器填寫 function __autoload($className) { require_once ROOT_PATH . '/includes/'. ucfirst($className) .'.class.php'; //自動加載類庫文件 } ?>
2. Create information test file article.php
Note: Due to my limited CSS ability, in order to demonstrate the function, I only used simple HTML
The code list and comments are as follows
<?php require 'config.inc.php'; //引入配置文件 $m = new M(); //實例化 M 類 $total = $m->Total('jzy_article'); //資訊文章總數(shù) $page = new Page($total, 20); //實例化分頁類 /* 注意事項: 1、實例分頁 Page 類的時候,需要傳兩個參數(shù):記錄總數(shù);每頁顯示的記錄數(shù)。 2、當(dāng)傳入?yún)?shù)后,Page 類中有個setLimit()方法會自動計算出 SQL 中的 limit 值。比如:URL 參數(shù)中 page 為1的時候,limit 值為“0,20”;為2的時候,limit 值為“20,20”…… 3、計算出來的 $page->limit,必須放在 FetchAll 方法中的最后一位,詳情請查看 FetchAll 方法 */ $data = $m->FetchAll("jzy_article", "title, source, writer, pubdate", "", "id DESC", $page->limit); //根據(jù) M 類庫中的 FetchAll 方法獲取數(shù)據(jù) ?> <style> /* 分頁樣式 */ #page {text-align:right; padding:10px;clear:both;}#page a {border:1px solid #666;padding:2px 5px;margin:0 2px;color:#3b6ea5;text-decoration:none;}#page a:hover,#page span.me {color:#fff;border:1px solid #000;background:#000;text-decoration:none;}#page span.disabled {border:1px solid #ccc;padding:2px 5px;margin:0 2px;color:#ccc;}#page span.me {padding:2px 5px;margin:0 2px;} </style> <table width="1000" border="1" style="border-collapse:collapse; font-size:13px;"> <tr height="30"> <th width="483">標(biāo)題</th> <th width="141">來源</th> <th width="154">作者</th> <th width="194">添加時間</th> </tr> <?php foreach ($data as $v) { //循環(huán)取出數(shù)據(jù) ?> <tr> <td> <?php echo $v['title']; ?></td> <td align="center"><?php echo $v['source']; ?></td> <td align="center"><?php echo $v['writer']; ?></td> <td align="center"><?php echo $v['pubdate']; ?></td> </tr> <?php } ?> <tr> <td id="page" colspan="4"><?php echo $page->fpage(); ?></td> <!-- 調(diào)出分頁類 --> </tr> </table>
3. Access test effect
Open the browser and enter the test URL address. Your browser should have the following effect
Readers who are interested in more PHP related content can check out the special topics of this site: "php curl usage summary", "PHP operation and operator usage summary", "PHP network programming skills summary", "PHP basic syntax introductory tutorial" ", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "Introduction to PHP object-oriented programming tutorial", "Summary of PHP string (string) usage" , "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.
Articles you may be interested in:
- Mysql class implemented by PHP based on singleton mode
- PHP encapsulated connection Mysql class and usage analysis
- a You can refer to the php Mysql class to learn and get familiar with
- Twelve common PHP MySql class free CMS systems
- PHP implements a complete example of the Model base class based on mysqli
- PHP formatting MYSQL method that returns float type
- php implements Mysql simple operation class
- php simple operation mysql database class
- PHP implements complete example of generating MYSQL statement class through parameters

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)

Hot Topics

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.

InPHP,variablesarepassedbyvaluebydefault,meaningfunctionsorassignmentsreceiveacopyofthedata,whilepassingbyreferenceallowsmodificationstoaffecttheoriginalvariable.1.Whenpassingbyvalue,changestothecopydonotimpacttheoriginal,asshownwhenassigning$b=$aorp

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

The reason why header('Location:...') in AJAX request is invalid is that the browser will not automatically perform page redirects. Because in the AJAX request, the 302 status code and Location header information returned by the server will be processed as response data, rather than triggering the jump behavior. Solutions are: 1. Return JSON data in PHP and include a jump URL; 2. Check the redirect field in the front-end AJAX callback and jump manually with window.location.href; 3. Ensure that the PHP output is only JSON to avoid parsing failure; 4. To deal with cross-domain problems, you need to set appropriate CORS headers; 5. To prevent cache interference, you can add a timestamp or set cache:f

To prevent session hijacking in PHP, the following measures need to be taken: 1. Use HTTPS to encrypt the transmission and set session.cookie_secure=1 in php.ini; 2. Set the security cookie attributes, including httponly, secure and samesite; 3. Call session_regenerate_id(true) when the user logs in or permissions change to change to change the SessionID; 4. Limit the Session life cycle, reasonably configure gc_maxlifetime and record the user's activity time; 5. Prohibit exposing the SessionID to the URL, and set session.use_only

The urlencode() function is used to encode strings into URL-safe formats, where non-alphanumeric characters (except -, _, and .) are replaced with a percent sign followed by a two-digit hexadecimal number. For example, spaces are converted to signs, exclamation marks are converted to!, and Chinese characters are converted to their UTF-8 encoding form. When using, only the parameter values ??should be encoded, not the entire URL, to avoid damaging the URL structure. For other parts of the URL, such as path segments, the rawurlencode() function should be used, which converts the space to . When processing array parameters, you can use http_build_query() to automatically encode, or manually call urlencode() on each value to ensure safe transfer of data. just

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.

You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.
