PHP從練項(xiàng)目之?dāng)?shù)字分頁(yè)效果
Jun 13, 2016 pm 12:23 PM
PHP自練項(xiàng)目之?dāng)?shù)字分頁(yè)效果
學(xué)習(xí)要點(diǎn):
1.LIMIT 用法
2.各種參數(shù)
3.超鏈接調(diào)用
第一:先在文件中設(shè)置數(shù)字分頁(yè)模塊;我的文件是(blog.php)
<span style="color: #008000;">//</span><span style="color: #008000;">分頁(yè)模塊</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;">首頁(yè)要得到所有的數(shù)據(jù)總和</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>;
要注意的是在數(shù)據(jù)庫(kù)中取集的時(shí)候
//我們必須每次重新讀取結(jié)果集,而不是從新去執(zhí)行SQL語(yǔ)句。
$_result = _query("SELECT tg_username,tg_sex,tg_face FROM tg_user ORDER BY tg_reg_time DESC LIMIT $_pagenum,$_pagesize");
設(shè)置分頁(yè)循環(huán)的效果
<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>
相對(duì)應(yīng)的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;}
在其中可能會(huì)由于編碼出現(xiàn)容錯(cuò)誤,解決的方法是
<span style="font-size: 18px;"><span style="color: #008000;">//</span><span style="color: #008000;"> 分頁(yè)模塊</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;"> 在數(shù)據(jù)不再數(shù)據(jù)范圍內(nèi)出錯(cuò)的解決方法</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,是否不是數(shù)字。//如果其中有一個(gè)是,那么就=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> < 0 || !<span style="color: #008080;">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;"> 如果是數(shù)字,但是小數(shù),那么就$_page = intval($_page);轉(zhuǎn)換成整數(shù)</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;">當(dāng)頁(yè)碼大于總頁(yè)碼的時(shí)候,就會(huì)返回到總頁(yè)碼的最后一頁(yè)</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>;</span>
?

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

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez
