PHP は、次のコードに示すように、非常に長いテキストのページング表示を実装します。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>session_start(); </span></li><li class="alt"><span> if(isset($_REQUEST['page'])) </span></li><li><span> { </span></li><li class="alt"><span> $<span class="attribute">page</span><span> = </span><span class="attribute-value">intval</span><span>($_GET['page']); </span></span></li><li><span> } </span></li><li class="alt"><span> else </span></li><li><span> { </span></li><li class="alt"><span> $<span class="attribute">page</span><span> = </span><span class="attribute-value">1</span><span>; </span></span></li><li><span> } </span></li><li class="alt"><span> <span class="tag">?></span><span> </span></span></li> <li><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"><</span><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span> </span><span class="tag">/></span><span> </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>超长文本的分页显示 - www.cxybl.com</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"><</span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span> </span></span></li> <li class="alt"> <span><!-- </span></li><li><span>.STYLE1 {font-size: 12px}a </span></li><li class="alt"><span>.STYLE2 {font-size: 13px} </span></li><li><span>--<span class="tag">></span><span> </span> </li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">style</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"500"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">bgcolor</span><span>=</span><span class="attribute-value">"#9AFAFC"</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"27"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">strong</span><span class="tag">></span><span>超长文本的分页显示</span><span class="tag"></</span><span class="tag-name">strong</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"90"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"left"</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"middle"</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE2"</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li class="alt"><span> if($page){ </span></li><li><span> $<span class="attribute">fp</span><span>=</span><span class="attribute-value">fopen</span><span>("file/file.txt","r"); </span></span></li><li class="alt"><span> $<span class="attribute">counter</span><span>=</span><span class="attribute-value">fgets</span><span>($fp); </span></span></li><li><span> $<span class="attribute">length</span><span>=</span><span class="attribute-value">strlen</span><span>($counter); </span></span></li><li class="alt"><span> $<span class="attribute">page_count</span><span>=</span><span class="attribute-value">ceil</span><span>($length/200); </span></span></li><li><span> $<span class="attribute">fp</span><span>=</span><span class="attribute-value">fopen</span><span>("file/file.txt","r"); </span></span></li><li class="alt"><span> include("function.php"); </span></li><li><span> $<span class="attribute">c</span><span>=</span><span class="attribute-value">msubstr</span><span>($counter,0,($page-1)*200); </span></span></li><li class="alt"><span> $<span class="attribute">c1</span><span>=</span><span class="attribute-value">msubstr</span><span>($counter,0,$page*200); </span></span></li><li><span> echo substr($c1,strlen($c),strlen($c1)-strlen($c)); </span></li><li class="alt"><span> }<span class="tag">?></span><span> </span></span></li> <li><span> <span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"500"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">bgcolor</span><span>=</span><span class="attribute-value">"#9AFAFC"</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"42%"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"middle"</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span> 页次:</span><span class="tag"><?</span><span class="tag-name">php</span><span> echo $page;</span><span class="tag">?></span><span> / </span><span class="tag"><?</span><span class="tag-name">php</span><span> echo $page_count;</span><span class="tag">?></span><span> 页 </span><span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"58%"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"28"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"left"</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"middle"</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span> 分页: </span></span></li> <li><span> <span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li class="alt"><span> if($page!=1){ </span></li><li><span> echo "<span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">index</span><span>.php?</span><span class="attribute">page</span><span>=</span><span class="attribute-value">1</span><span class="tag">></span><span>首页</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span> "; </span></span></li> <li class="alt"><span> echo "<span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">index</span><span>.php?</span><span class="attribute">page</span><span>=</span><span class="attribute-value">".($page-1)."</span><span class="tag">></span><span>上一页</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span> "; </span></span></li> <li><span> } </span></li> <li class="alt"><span> if($page<span class="tag"><</span><span>$page_count){ </span></span></li><li><span> echo "<span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">index</span><span>.php?</span><span class="attribute">page</span><span>=</span><span class="attribute-value">".($page+1)."</span><span class="tag">></span><span>下一页</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span> "; </span></span></li> <li class="alt"><span> echo "<span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">index</span><span>.php?</span><span class="attribute">page</span><span>=</span><span class="attribute-value">".$page_count."</span><span class="tag">></span><span>尾页</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>"; </span></span></li> <li><span> } </span></li> <li class="alt"><span> <span class="tag">?></span><span> </span></span></li> <li><span> <span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li> </ol>function.php:
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></li><li><span><span class="keyword">function</span><span> msubstr(</span><span class="vars">$str</span><span>,</span><span class="vars">$start</span><span>,</span><span class="vars">$len</span><span>){ </span></span></li><li class="alt"><span> <span class="vars">$strlen</span><span>=</span><span class="vars">$start</span><span>+</span><span class="vars">$len</span><span>; </span></span></li><li><span> <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><</span><span class="vars">$strlen</span><span>;</span><span class="vars">$i</span><span>++) { </span></span></li><li class="alt"><span> <span class="keyword">if</span><span>(ord(</span><span class="func">substr</span><span>(</span><span class="vars">$str</span><span>,</span><span class="vars">$i</span><span>,1))>0xa0) { </span></span></li> <li><span> <span class="vars">$tmpstr</span><span>.=</span><span class="func">substr</span><span>(</span><span class="vars">$str</span><span>,</span><span class="vars">$i</span><span>,2); </span></span></li> <li class="alt"><span> <span class="vars">$i</span><span>++; </span></span></li> <li><span> }<span class="keyword">else</span><span>{ </span></span></li> <li class="alt"><span> <span class="vars">$tmpstr</span><span>.=</span><span class="func">substr</span><span>(</span><span class="vars">$str</span><span>,</span><span class="vars">$i</span><span>,1); } </span></span></li> <li><span> } </span></li> <li class="alt"><span> <span class="keyword">return</span><span> </span><span class="vars">$tmpstr</span><span>; </span></span></li> <li><span>} </span></li> <li class="alt"><span>?> </span></li> </ol>conn.php:
<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span><span class="vars">$id</span><span>=mysql_connect(</span><span class="string">"localhost"</span><span>,</span><span class="string">"root"</span><span>,</span><span class="string">"mysql"</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="string">'连接失败:'</span><span> . mysql_error()); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(mysql_select_db(</span><span class="string">"phpjcdb"</span><span>,</span><span class="vars">$id</span><span>)) </span><span class="comment">//说明:phpjcdb 是数据库名称</span><span> </span></span></li><li><span> <span class="func">echo</span><span> </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span> <span class="keyword">else</span><span> </span></span></li><li><span> <span class="func">echo</span><span> (</span><span class="string">'数据库选择失败:'</span><span> . mysql_error()); </span></span></li><li class="alt"><span>mysql_query(<span class="string">"set names gb2312"</span><span>); </span><span class="comment">//设置为简体中文</span><span> </span></span></li><li><span>?> </span></span></li></ol>