Home  >  Article  >  Backend Development  >  PHP article paging implementation code

PHP article paging implementation code

WBOY
WBOYOriginal
2016-07-25 09:00:20846browse
PHP implements paging of article content and automatically paging according to the length of the content. Friends who plan to study this area can refer to it.

The code is as follows:

<?php   
    /*
    * 文章分页
    * bbs.it-home.org
    */  
      
    $fp=fopen('txt.txt','r');  
    while (!feof($fp)){  
         $content.=fgets($fp);   
    }  
    $c_page="";  
    if(isset($_GET['page'])) $c_page=$_GET['page'];   
    $listlen=100;   //分页显示字符数  
    $conlen=strlen($content);   //内容长度  
    $j=0;  
    $listat[0]=0;  
    $lookat=0;  
    for ($i=1;$i<$conlen/$listlen+1;$i++){  
        //echo $conlen/$listlen;  
        if($lookat<$listlen)  
            $lookat=$listlen;  
            if($lookat>$conlen){  
                $j++;  
                $listat[$j]=$conlen;  
                break;  
            }  
        $endat=strpos($content,"\n",$lookat);  
        if($endat>$conlen-$listlen/5 or intval($endat)<1){  
            $j++;  
            $listat[$j]=$conlen;  
            break;  
        }else {  
            $j++;  
            $listat[$j]=$endat;  
            $lookat=$endat+$listlen;  
        }  
              
    }  
    //print page  
    $pagenum=$j;  //总页数  
    $page=intval($c_page);  
    if(emptyempty($page) or $page<1 or $page>$pagenum) $page=1;  
    if($pagenum >1)   
    for ($i=1;$i<$pagenum+1;$i++){  
        echo " {$i} ";  
    }  
    echo "<br>";  
    $stag=$page-1;  
    $startb=$listat[$stag];  
    if($startb>0) //去除首个换行  
    $startb=$startb+1;  
    $cont=substr($content,$startb,$listat[$page]-$startb);  
    echo "<BR/>文章内容:<BR/>";  
    if($page>1)  
    echo "[接上页]       ";  
    echo $cont;      
?> 


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