php中長(zhǎng)文章分頁(yè)顯示實(shí)現(xiàn)代碼
Jun 13, 2016 am 11:57 AM
歡迎交流!實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
/**
*Author:烏鳥heart
*實(shí)現(xiàn)長(zhǎng)文章分頁(yè)的代碼
*原理:
*利用一個(gè)數(shù)組來記錄文章每一頁(yè)(用p0、p1、p2...做手動(dòng)標(biāo)記)的起始字節(jié)數(shù),然后通過利用php函數(shù)操作這個(gè)數(shù)組去顯示分頁(yè)后的文章。分頁(yè)顯示,傳遞ptag(與tag的值一樣)值。
*利用到的php函數(shù):
*1、strlen("字符串") - Returns the length of the given string. - 返回字符串的字節(jié)總數(shù)。
*2、strpos("字符串","匹配字符") - Returns the numeric position of the first occurrence of needle in the haystack string. - 返回字符串中出現(xiàn)的第一個(gè)相匹配的字符所在的字節(jié)序數(shù)。
*3、substr("字符串","起始位置","終止位置") - substr() returns the portion of string specified by the start and length parameters. - 返回字符串中指定起止位置的若干字符。
*/
$sql = "select * from article where id = 41";//定義sql語(yǔ)句,返回id為41的內(nèi)容
$result = mysql_query($sql);//執(zhí)行sql語(yǔ)句,返回結(jié)果集
$row = mysql_fetch_array($result);//以數(shù)組的形式從記錄集返回
$content = $row['content'];//把文章賦給變量$content
$articleCounts = strlen($content);//返回$content(文章)的總字節(jié)數(shù)
$isTrue = true;//循環(huán)標(biāo)記
$tag = 0;//分頁(yè)標(biāo)記、數(shù)組下標(biāo)
echo "字節(jié)總數(shù):".$articleCounts."
";//測(cè)試信息
//尋找標(biāo)記“ptag”,并把其位置(所在的字節(jié)數(shù))賦給數(shù)組array[]------------------------------------------
while($isTrue){
$startAt = strpos($content,"p".$tag);//得到相應(yīng)ptag的字節(jié)序數(shù)
if($startAt != false){ //如果有標(biāo)記(返回值不是false),則記錄位置
$array[$tag++] = $startAt;
}else{ //如果沒有標(biāo)記,則將數(shù)組array[0]賦值'\0'
$array[$tag] = '\0';
$isTrue = false;
}
}
//循環(huán)輸出標(biāo)記位置-------------------------------------------------------------測(cè)試信息
for($i = 0; $i echo $array[$i]."
";
}
echo "------------------------------
";
//輸出內(nèi)容---------------------------------------------------------------------
if($array[0] == '\0'){ //判斷是否有標(biāo)記
echo $content; //沒有標(biāo)記的情況,單頁(yè)顯示
}else{ //有標(biāo)記的情況,分頁(yè)顯示
//輸出分頁(yè)內(nèi)容
if( isset($_GET['ptag']) ){ //判斷是否有ptag值傳遞,有則顯示第 ptag+1 頁(yè),否則顯示第一頁(yè)(ptag=0)
$ptag = $_GET['ptag']; //把ptag的值賦給變量$ptag
if($ptag echo "有值傳遞,顯示第".($ptag+1)."頁(yè)
"; //測(cè)試信息
echo "值為:".$ptag."
"; //測(cè)試信息
echo substr($content,$array[$ptag - 1] + 2,$array[$ptag] - $array[$ptag - 1] - 2);//顯示ptag+1頁(yè)的內(nèi)容
}else{echo "參數(shù)有誤";}
}
else{ //沒有ptag值傳遞的情況,顯示第一頁(yè)(ptag=0)
echo "無值傳遞,顯示第1頁(yè)
"; //測(cè)試信息
echo substr($content,0,$array[0] - 1);//顯示第一頁(yè)的內(nèi)容
}
}
//循環(huán)顯示頁(yè)數(shù)鏈接-------------------------------------------------------------
if($array[0] != '\0'){ //在有手動(dòng)標(biāo)記的情況下才顯示頁(yè)數(shù)鏈接
for($i = 0;$i if($ptag == $i){ //如果是本頁(yè),則粗體顯示
$pager .= " ".($i+1)." ";
}else{ //不是本頁(yè)
$pager .= " ".($i+1)." ";
}
}
echo "
跳轉(zhuǎn)至第".$pager."頁(yè)"; //輸出鏈接
}
?>

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

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.
