PHP实现超长文本的分页显示,如下代码:
- <?php
- session_start();
- if(isset($_REQUEST['page']))
- {
- $page = intval($_GET['page']);
- }
- else
- {
- $page = 1;
- }
- ?>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>超长文本的分页显示 - www.cxybl.com</title>
- <style type="text/css">
- <!--
- .STYLE1 {font-size: 12px}a
- .STYLE2 {font-size: 13px}
- -->
- </style>
- </head>
- <body>
- <table width="500" border="1" cellpadding="0" cellspacing="0" bgcolor="#9AFAFC">
- <tr>
- <td height="27" align="center"><strong>超长文本的分页显示</strong></td>
- </tr>
- <tr>
- <td height="90" align="left" valign="middle"><span class="STYLE2">
- <?php
- if($page){
- $fp=fopen("file/file.txt","r");
- $counter=fgets($fp);
- $length=strlen($counter);
- $page_count=ceil($length/200);
- $fp=fopen("file/file.txt","r");
- include("function.php");
- $c=msubstr($counter,0,($page-1)*200);
- $c1=msubstr($counter,0,$page*200);
- echo substr($c1,strlen($c),strlen($c1)-strlen($c));
- }?>
- </span></td>
- </tr>
- </table>
- <table width="500" border="1" cellpadding="0" cellspacing="0" bgcolor="#9AFAFC">
- <tr>
- <td width="42%" align="center" valign="middle">
- <span class="STYLE1"> 页次:<?php echo $page;?> / <?php echo $page_count;?> 页 </span></td>
- <td width="58%" height="28" align="left" valign="middle"><span class="STYLE1"> 分页:
- <?php
- if($page!=1){
- echo "<a href=index.php?page=1>首页</a> ";
- echo "<a href=index.php?page=".($page-1).">上一页</a> ";
- }
- if($page<$page_count){
- echo "<a href=index.php?page=".($page+1).">下一页</a> ";
- echo "<a href=index.php?page=".$page_count.">尾页</a>";
- }
- ?>
- </span> </td>
- </tr>
- </table>
- </body>
- </html>
function.php:
- <?php
- function msubstr($str,$start,$len){
- $strlen=$start+$len;
- for($i=0;$i<$strlen;$i++) {
- if(ord(substr($str,$i,1))>0xa0) {
- $tmpstr.=substr($str,$i,2);
- $i++;
- }else{
- $tmpstr.=substr($str,$i,1); }
- }
- return $tmpstr;
- }
- ?>
conn.php:
- <?php
- $id=mysql_connect("localhost","root","mysql") or die('连接失败:' . mysql_error());
- if(mysql_select_db("phpjcdb",$id)) //说明:phpjcdb 是数据库名称
- echo "";
- else
- echo ('数据库选择失败:' . mysql_error());
- mysql_query("set names gb2312"); //设置为简体中文
- ?>