Home  >  Article  >  php教程  >  php文章分页

php文章分页

WBOY
WBOYOriginal
2016-06-13 10:50:051665browse

1.  2. /*文章分页代码*/ 
3.  
4. $fp=fopen('txt.txt','r'); 
5. while (!feof($fp)){ 
6.      $content.=fgets($fp);  
7. } 
8. $c_page=""; 
9. if(isset($_GET['page'])) $c_page=$_GET['page'];  
10. $listlen=100;   //分页显示字符数  
11. $conlen=strlen($content);   //内容长度  
12. $j=0; 
13. $listat[0]=0; 
14. $lookat=0; 
15. for ($i=1;$i 16.     //echo $conlen/$listlen;  
17.     if($lookat 18.         $lookat=$listlen; 
19.         if($lookat>$conlen){ 
20.             $j++; 
21.             $listat[$j]=$conlen; 
22.             break; 
23.         } 
24.     $endat=strpos($content,"\n",$lookat); 
25.     if($endat>$conlen-$listlen/5 or intval($endat) 26.         $j++; 
27.         $listat[$j]=$conlen; 
28.         break; 
29.     }else { 
30.         $j++; 
31.         $listat[$j]=$endat; 
32.         $lookat=$endat+$listlen; 
33.     } 
34.          
35. } 
36. //print page  
37. $pagenum=$j;  //总页数  
38. $page=intval($c_page); 
39. if(emptyempty($page) or $page$pagenum) $page=1; 
40. if($pagenum >1)  
41. for ($i=1;$i 42.     echo " {$i} "; 
43. } 
44. echo "
"; 
45. $stag=$page-1; 
46. $startb=$listat[$stag]; 
47. if($startb>0) //去除首个换行  www.2cto.com
48. $startb=$startb+1; 
49. $cont=substr($content,$startb,$listat[$page]-$startb); 
50. echo "
文章内容:
"; 
51. if($page>1) 
52. echo "[接上页]       "; 
53. echo $cont; 
54.  
55. ?> 
 
作者:chaojie2009

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