search
HomeBackend DevelopmentPHP TutorialPHP从练项目之数字分页效果

PHP自练项目之数字分页效果

学习要点:
1.LIMIT 用法
2.各种参数
3.超链接调用

第一:先在文件中设置数字分页模块;我的文件是(blog.php)

<span style="color: #008000;">//</span><span style="color: #008000;">分页模块</span><span style="color: #800080;">$_page</span> = <span style="color: #800080;">$_GET</span>['page'<span style="color: #000000;">];</span><span style="color: #800080;">$_pagesize</span> = 10<span style="color: #000000;">;</span><span style="color: #800080;">$_pagenum</span> = (<span style="color: #800080;">$_page</span> - 1) * <span style="color: #800080;">$_pagesize</span><span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">首页要得到所有的数据总和</span><span style="color: #800080;">$_num</span>=<span style="color: #008080;">mysql_num_rows</span>(_query("SELECT tg_id FROM tg_user"<span style="color: #000000;">));</span><span style="color: #800080;">$_pageabsolute</span>=<span style="color: #800080;">$_num</span> / <span style="color: #800080;">$_pagesize</span>;

要注意的是在数据库中取集的时候

//我们必须每次重新读取结果集,而不是从新去执行SQL语句。
$_result = _query("SELECT tg_username,tg_sex,tg_face FROM tg_user ORDER BY tg_reg_time DESC LIMIT $_pagenum,$_pagesize");
设置分页循环的效果

<span style="font-size: 16px;"><div id="page_num">    <ul>    <?php <span style="color: #0000ff;">for(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$_pageabsolute;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_page</span> == (<span style="color: #800080;">$i</span>+1<span style="color: #000000;">)) {            </span><span style="color: #0000ff;">echo</span> '<li><a href="blog.php?page='.(<span%20style=" color:>$i+1).'" class="selected">'.(<span style="color: #800080;">$i</span>+1).'</a></li>'<span style="color: #000000;">;        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{            </span><span style="color: #0000ff;">echo</span> '<li><a href="blog.php?page='.(<span%20style=" color:>$i+1).'">'.(<span style="color: #800080;">$i</span>+1).'</a></li>'<span style="color: #000000;">;        }    } </span>?>    </ul>    </div></span>

相对应的CSS

#page_num {	height:20px;	clear:both;	padding:10px 0;	position:relative;}#page_num ul {	position:absolute;	right:30px;	height:20px;}#page_num ul li {	float:left;	width:26px;	height:20px;}#page_num ul li a {	display:block;	width:20px;	height:20px;	line-height:20px;	border:1px solid #333;	text-align:center;	text-decoration:none;}#page_num ul li a:hover,#page_num ul li a.selected {	background:#666;	font-weight:bold;	color:#fff;}

  

在其中可能会由于编码出现容错误,解决的方法是

<span style="font-size: 18px;"><span style="color: #008000;">//</span><span style="color: #008000;"> 分页模块</span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span> ( <span style="color: #800080;">$_GET</span> ['page'<span style="color: #000000;">] )) {    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 在数据不再数据范围内出错的解决方法</span>    <span style="color: #800080;">$_page</span> = <span style="color: #800080;">$_GET</span>['page'<span style="color: #000000;">];    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 是否为空,是否小于0,是否不是数字。//如果其中有一个是,那么就=1</span>    <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span> ( <span style="color: #800080;">$_page</span> )||<span style="color: #800080;">$_page</span> is_numeric</span>( <span style="color: #800080;">$_page</span><span style="color: #000000;"> )) {        </span><span style="color: #800080;">$_page</span> = 1<span style="color: #000000;">;    } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {        </span><span style="color: #800080;">$_page</span> = <span style="color: #008080;">intval</span> ( <span style="color: #800080;">$_page</span> ); <span style="color: #008000;">//</span><span style="color: #008000;"> 如果是数字,但是小数,那么就$_page = intval($_page);转换成整数</span><span style="color: #000000;">    }} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {    </span><span style="color: #800080;">$_page</span> = 1<span style="color: #000000;">;}</span><span style="color: #800080;">$_pagesize</span> = 10<span style="color: #000000;">;</span><span style="color: #800080;">$_num</span> = _num_rows( _query ( "SELECT tg_id FROM tg_user"<span style="color: #000000;"> ) );</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_num</span>==0<span style="color: #000000;">) {    </span><span style="color: #800080;">$_pageabsolute</span>=1<span style="color: #000000;">;}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{    </span><span style="color: #800080;">$_pageabsolute</span>=<span style="color: #008080;">ceil</span>(<span style="color: #800080;">$_num</span>/<span style="color: #800080;">$_pagesize</span><span style="color: #000000;">);}</span><span style="color: #008000;">//</span><span style="color: #008000;">当页码大于总页码的时候,就会返回到总页码的最后一页</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_page</span>><span style="color: #800080;">$_pageabsolute</span><span style="color: #000000;">) {    </span><span style="color: #800080;">$_page</span>=<span style="color: #800080;">$_pageabsolute</span><span style="color: #000000;">;}</span><span style="color: #800080;">$_pagenum</span> = (<span style="color: #800080;">$_page</span> - 1) * <span style="color: #800080;">$_pagesize</span>;

 

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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version