安哥网络 发表于 2014-1-8 15:00:36

PHP新闻内容自动分页程序-it论坛-计算机论坛

PHP新闻内容自动分页程序-it论坛-计算机论坛<?php
/*==================================================================
程序说明:用于自动生成新闻静态页,手动添加分页功能,自动生成页码

程序作者:Www.Leadbbs.Com 简体中文版 QQ: 7275244

程序用途:演示用

时间:2005年9月30日

使用方法:绝对路径下建立info目录,建立Template.Html文件
=================================================================*/
if($Submit){
   $FileName = "Test";
   $NewsContent = "";
   $NewsPage = "";
   $ContentTemp = "";
   $Content = htmlspecialchars($_POST['Content']);
   if($Content==""){
         echo "请输入内容";
         exit;
   }
   $ContentTemp = explode("*分页符*", $Content);
   $CountPage = count($ContentTemp);
   //文件操作
   for($k = 0 ; $k <= $CountPage-1 ; $k++){
                     //判断页数,成生页码
                           if ($CountPage > 1){      //不是单页新闻,需要显示分页信息
                                 if ($k == 0){
                                       $NowPage = 1;      //当前页
                                       $NewsPage = GetPageCount($NowPage,$CountPage,$FileName);
                                 }else{
                                       $NowPage = $k + 1;
                                       $NewsPage = GetPageCount($NowPage,$CountPage,$FileName);
                                       $FileName = $FileName . "_" . $NowPage;
                                 }
                           }
                     //生成页码结束
               $name="Template.html";
               $read=open($name);
               $read=str_replace("{Content}",$ContentTemp[$k],$read);
               $read=str_replace("{NewsPage}",$NewsPage,$read);
               write($read,"Info/" . $FileName . ".html");
               $FileName = "Test";
   }
}

function open($name){
         $f=@file($name);
         if($f){
                  foreach($f as $in){
                        $read.=$in;
                  }
         }
         return $read;
}
function write($s,$to){
         $fp=@fopen($to,"w");
         @fputs($fp,$s);
         @fclose($to);
}

function GetPageCount($NowPage,$CountPage,$FileName){ //自动生成页码
//==========显示结果============
//上一页 1 2 3 下一页
//上一页 ... 4 5 6 下一页
//上一页 1 2 3 ... 下一页
//上一页 ... 4 5 6 ... 下一页
//==============================
$ShowPageNum = 7;            //最好是单数,好看一些 ... 11 12 13 <14> 15 16 17 ...
$PageUp = "";
$PageDown = "";
$GetPageCount = "";
   if($NowPage == 1){
         $GetPageCount = $GetPageCount . "<a href='" . $FileName . ".html'><font color='#ff0000'><b>1</b></font></a> ";
   }else{
         $GetPageCount = $GetPageCount . "<a href='" . $FileName . ".html'>1</a> ";
   }
   
   if($CountPage <= $ShowPageNum){
         for ($i = 2; $i <= $CountPage; $i++) {
               $GetPageCount = $GetPageCount . "<a href='" . $FileName . "_" . $i . ".html'>" . GetColor($i,$NowPage) . "</a> ";
         }
   }else{            //页数大于自定义的显示页码数量
         if ((($NowPage - 3) > 1) && (($NowPage + 3) < $CountPage)){
               $GetPageCount = "... ";
               for ($i = $NowPage - 3; $i <= $NowPage + 3; $i++) {
                     $GetPageCount = $GetPageCount . "<a href='" . $FileName . "_" . $i . ".html'>" . GetColor($i,$NowPage) . "</a> ";
               }
               $GetPageCount = $GetPageCount . "...";
         }else{
               if ((($NowPage - 3) > 1) && (($NowPage + 3) >= $CountPage)){
                     $GetPageCount = "... ";
                     for ($i = $CountPage - $ShowPageNum+1; $i <= $CountPage; $i++) {
                           $GetPageCount = $GetPageCount . "<a href='" . $FileName . "_" . $i . ".html'>" . GetColor($i,$NowPage) . "</a> ";
                     }
               }else{
                     for ($i = 2; $i <= $ShowPageNum; $i++) {
                           $GetPageCount = $GetPageCount . "<a href='" . $FileName . "_" . $i . ".html'>" . GetColor($i,$NowPage) . "</a> ";
                     }
                     $GetPageCount = $GetPageCount . "...";
               }
         }
   }
   //加首头页尾
   if($NowPage > 1){
         if($NowPage > 2){
               $PageUp = "<a href='" . $FileName . "_" . ($NowPage - 1) . ".html'>上页</a> ";
         }else{
               $PageUp = "<a href='" . $FileName . ".html'>上页</a> ";
         }
   }
   if ($NowPage < $CountPage){
         $PageDown = "<a href='" . $FileName . "_" . ($NowPage + 1) . ".html'>下页</a> ";
   }
   $GetPageCount = "<a href='" . $FileName . ".html'>首页</a>" . $PageUp . $GetPageCount . $PageDown . "<a href='" . $FileName . "_" . $CountPage . ".html'>末页</a>";
   return $GetPageCount;
}

function GetColor($i,$NowPage){      //当前页标志色
   if($i == $NowPage){
         $GetColor = "<font color='#ff0000'><b>" . $NowPage . "</b></font>";
   }else{
         $GetColor = $i;
   }
   return $GetColor;
}
?>
<form name="form1" method="post" action="HtmlAutoPage.php">
<textarea name="Content" cols="100" rows="8"></textarea>
<input type="button" name="Submit" value="插入分页" onClick="form1.Content.value+='*分页符*';form1.Content.focus();">
<input type="submit" name="Submit" value="提交">
</form>文章引用自:http://w.leadbbs.com/a/a.asp?B=212&ID=2154168&ac=nxt&rd=717594

页: [1]
查看完整版本: PHP新闻内容自动分页程序-it论坛-计算机论坛