最近要实现一个PHP新闻内容分页类,找了老半天竟然找不到满意的东东,根据一些网友提供的思想,自己动手写了个内容分页类.贴在这里各位PHPer交流一下,给些意见!
类可保存为content2page.class.php
CODE:

<?php
/********************************************************
*类名:content2page.class.php
*描述:用于自动生成新闻静态页,手动添加分页功能,自动生成页码
*作者:程相闯
*日期:2006-11-27 14:20
*联系:cxc-0378@163.com

使用示例:
$con2page = new Content2Page;
$con2page->FileName = "";  //生成静态文件名
$con2page->FileDir = "html/";  //生成静态页面存放的目录文件夹,若不为空则要在末尾加“/”
$con2page->TemplateName = "";  //模班文件名
$con2page->Content = "";  //新闻内容,一般用POST或GET传递
$con2page->Content2Html();
*******************************************************/
class Content2Page
{
var $FileName = "test";//生成静态页面的文件名,默认为test
var $FileDir = "";//生成静态页面存放的目录文件夹,若不为空则要在末尾加“/”
var $TemplateName = "template.html";//调用模班页面名称,默认为template.html
var $NewsPage = "";//新闻分页
var $Content = "";//新闻内容
var $SplitSymbol = "*分页符*";//内容分页符,默认为“*分页符*”
var $NowPage = "";//当前页面
var $CountPage = "";//总分页数

/*************************************************
   Function:      Content2Html()
   Descrīption:  用来将新闻内容分页输出
   Calls:        ReadFromFile(),Write2File(),GetPageCount()
   Input:        含分页符的新闻内容
   Output:       已经分页的HTML静态页面
   Return:     void
   Access:    public
*************************************************/
  function Content2Html()
  {
   $FileNameTemp = $this->FileName;
   if ($_POST['Submit'])
   {
    if($this->Content=="")
      {
        echo "请输入内容";
        exit;
      }
      $ContentTemp = explode($this->SplitSymbol, $this->Content);
      $this->CountPage = count($ContentTemp);
      //文件操作
      for($k = 0 ; $k <= $this->CountPage-1 ; $k++)
      {
        /*******判断页数,成生页码 BEGIN***********/
        if ($this->CountPage > 1)
        {
         //若不是单页新闻,需要显示分页信息
          if ($k == 0)
          {
           $this->NowPage = 1;      //当前页
           $this->NewsPage = $this->GetPageCount();
          }
         else
         {
            $this->NowPage = $k + 1;
            $this->NewsPage = $this->GetPageCount();
            $this->FileName = $this->FileName . "_" . $this->NowPage;
         }
       }
     /**************生成页码 END *************/
     //将内容写入模班
        $read=$this->ReadFromFile($this->TemplateName);
        $read=str_replace("{Content}",$ContentTemp[$k],$read);
        $read=str_replace("{NewsPage}",$this->NewsPage,$read);
        $this->Write2File($read,$this->FileDir.$this->FileName . ".html");
        $this->FileName = $FileNameTemp;//初始化文件名
      }
  }
  }
  /****************************************
   Function:          ReadFromFile()
   Parameter:  $name 文件名
   Descrīption:      用来读取文件内容
   Called By:         Content2Html()
   Input:               文件名
   Output:            找到文件,读取起内容
   Return:     $read 内容字符串
   Access:    public
*****************************************/ 
  function ReadFromFile($name)
{
    $f=@file($name);
    if($f)
    {
      foreach($f as $in)
       {
         $read.=$in;
       }
    }
    return $read;
}
/******************************************
   Function:        Write2File()
   Parameter:   $content 待写入的内容
          $file 要写入的文件
   Descrīption:    用来写入文件内容
   Called By:       content2html()
   Return:      void
   Access:     public                                         
*******************************************/ 
function Write2File($content,$file)
{
    $fp=@fopen($file,"w");
    @fputs($fp,$content);
    @fclose($file);
}

/********************************************
   Function:        GetPageCount()
   Descrīption:    分几种情况输出页面分页的格式
   Calls:     GetColor()
   Called By:       Content2Html()
   Return:      $GetPageCount 自动分页字符串
   Access:     public
********************************************/ 
function GetPageCount()
{
  //自动生成页码
  //==========显示结果============
  //上一页 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($this->NowPage == 1)
   {
      $GetPageCount = $GetPageCount . "<a href='" . $this->FileName . ".html'><font color='#ff0000'><b>1</b></font></a> ";
   }
   else
   {
      $GetPageCount = $GetPageCount . "<a href='" . $this->FileName . ".html'>1</a> ";
   }
    
   if($this->CountPage <= $ShowPageNum)
   {
     for ($i = 2; $i <= $this->CountPage; $i++)
     {
       $GetPageCount = $GetPageCount . "<a href='" . $this->FileName . "_" . $i . ".html'>" . $this->GetColor($i) . "</a> ";
     }
   }
   else
   { 
     //页数大于自定义的显示页码数量
      if ((($this->NowPage - 3) > 1) && (($this->NowPage + 3) < $this->CountPage))
      {
        $GetPageCount = "... ";
        for ($i = $this->NowPage - 3; $i <= $this->NowPage + 3; $i++)
        {
          $GetPageCount = $GetPageCount . "<a href='" . $this->FileName . "_" . $i . ".html'>" . $this->GetColor($i) . "</a> ";
        }
        $GetPageCount = $GetPageCount . "...";
      }
      else
      {
        if ((($this->NowPage - 3) > 1) && (($this->NowPage + 3) >= $this->CountPage))
        {
          $GetPageCount = "... ";
          for ($i = $this->CountPage - $ShowPageNum+1; $i <= $this->CountPage; $i++)
          {
           $GetPageCount = $GetPageCount . "<a href='" . $this->FileName . "_" . $i . ".html'>" . $this->GetColor($i) . "</a> ";
          }
        }
        else
        {
          for ($i = 2; $i <= $ShowPageNum; $i++)
          {
           $GetPageCount = $GetPageCount . "<a href='" . $this->FileName . "_" . $i . ".html'>" . $this->GetColor($i) . "</a> ";
          }
          $GetPageCount = $GetPageCount . "...";
        }
      }
   }
   //加首头页尾
   if($this->NowPage > 1)
   {
      if($this->NowPage > 2)
      {
        $PageUp = "<a href='" . $this->FileName . "_" . ($this->NowPage - 1) . ".html'>上页</a> ";
      }
      else
      {
        $PageUp = "<a href='" . $this->FileName . ".html'>上页</a> ";
      }
   }
 
   if ($this->NowPage < $this->CountPage)
   {
     $PageDown = "<a href='" . $this->FileName . "_" . ($this->NowPage + 1) . ".html'>下页</a> ";
   }
   $GetPageCount = "<a href='" . $this->FileName . ".html'>首页</a>  " . $PageUp . $GetPageCount . $PageDown . "<a href='" . $this->FileName . "_" . $this->CountPage . ".html'>末页</a>";
   return $GetPageCount;
}

/*******************************************
   Function:        GetColor()
   Parameter:   $i 用来传递当前页面数
   Descrīption:    给当前页面标志醒目的颜色
   Called By:       GetPageCount()
   Return:      $GetColor 含当前页的字符串
   Access:     public
********************************************/ 
function GetColor($i)
{    
   //当前页标志色
    if($i == $this->NowPage)
    {
       $GetColor = "<font color='#ff0000'><b>" . $this->NowPage . "</b></font>";
    }
    else
    {
       $GetColor = $i;
    }
      return $GetColor;
}
}//end of class
?>

demo:page.php

<?php
include_once("content2page.class.php");
$con2page = new Content2Page;
$con2page->FileName = "test";//文件名,不要带后缀
$con2page->FileDir = "html/";//生成静态页面存放的目录文件夹,默认为当前文件夹
$con2page->TemplateName = "tmpl/Template1.html";//模班文件
$con2page->Content = htmlspecialchars($_POST['Content']);//新闻内容
$con2page->Content2Html();
?>
<form name="form1" method="POST" action="">
  <textarea name="Content" cols="100" rows="8"></textarea>
  <input type="button" name="insert" value="插入分页" ōnClick="form1.Content.value+='*分页符*';form1.Content.focus();">
  <input type="submit" name="Submit" value="提交">
</form>

template:Template1.html

<div style="font-size:14px; line-height:150%">
{Content}
</div>
<br />
<div style="font-size:12px;" align="center">
{NewsPage}
</div>

可以根据自己的程序修改,动态程序,伪静态翻页均可以使用。

PHP新闻内容分页类相关推荐

  1. php分页类smary,php新闻内容分页实例(smarty 模板)

    项目需要用到 新闻内容页 实现分页功能,随便写了一下. 给大家做个参考,写的不好还望谅解啊! //新闻内容分页开始 The page next for news content start $next ...

  2. php新闻表怎么分页,新闻列表分页类求助

    新闻列表分页类求助 include("conn.php"); $sql="select * from news_content order by news_id Desc ...

  3. EF/SQL/新闻中分页应用

    1.EF分页 public IList<V_Test> GetTestPageLoad(int pagesize, int pageindex, out int total){try{Te ...

  4. php分页操作,PHP实现适用于文件内容操作的分页类

    本文实例为大家分享了PHP实现文件内容操作的分页类,强调一下只针对文件的操作,供大家参考,具体内容如下 class StrPage { private $current; //当前页 private ...

  5. 封装一个新闻类News,包含新闻标题,新闻作者,新闻内容,新闻类型三个属性,提供必要的访问器和修改器方法重写toString方法,要求打印对象时输出格式为“标题;

    封装一个新闻类News,包含新闻标题,新闻作者,新闻内容, 新闻类型三个属性,提供必要的访问器和修改器方法,重写toString方法, 要求打印对象时输出格式为"标题:类型:作者" ...

  6. PHP分页类(较完美)

    <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $lis ...

  7. PHP数据库操作分页类

    MySQL数据库操作类: 1 <?php 2 class mysql { 3 private $db_host; //数据库主机 4 private $db_user; //数据库用户名 5 p ...

  8. php分页类 seo,HTML_写一个对搜索引擎友好的文章SEO分页类,使用jsp/php/asp等动态程序生成 - phpStudy...

    写一个对搜索引擎友好的文章SEO分页类 使用jsp/php/asp等动态程序生成的页面如何对搜索引擎友好呢?你可能想使用url_rewrite.不过,最好还是让同一个网址在任意时间对应的页面内容都是一 ...

  9. 分页原理+分页代码+分页类制作

      分页显示是一种非常常见的浏览和显示大量数据的方法,属于web编程中最常处理的事件之一.对于web编程的老手来说,编写这种代码实在是和呼吸一样自然,但是对于初学者来说,常常对这个问题摸不着头绪,因此 ...

最新文章

  1. Linux centos 主机名颜色设置 和 别名设置
  2. 下午回来才后知百密于一疏忽
  3. E:Johnny and Grandmaster
  4. 前端学习(684):循环导读
  5. html4符合web的标准吗,在生成HTML,表格等时,CakePhp是否“符合标准”?
  6. 二叉查找树(BST)的性质
  7. 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
  8. c语言读取windows注册表,C\C++修改windows注册表
  9. CANape a2l文件的编辑教程
  10. Java-计算器(简易版)
  11. 天津大学材料与化工考研考情与难度、参考书及上岸前辈备考经验
  12. opencv的RGB 颜色表
  13. 算术平均数、几何平均数、
  14. 300ETF期权和50ETF期权的区别
  15. Ubuntu-默认浏览器Firefox不能播放B站视频的修复过程
  16. iOS 苹果开发者注册 和 App Store问题咨询
  17. c语言极限方法求e方法,极限值确定函数式中参数的求解方法.pdf
  18. Deep Blind Video Super-resolution
  19. Excel数据如何导入到Oracle数据库中
  20. numpy.core._exceptions.MemoryError: Unable to allocate 167. MiB for an array with shape (2970, 2460,

热门文章

  1. Windows 服务程序(一)
  2. 面向对象与原型-ps:这一章简直是天书
  3. 对于国内数据交换平台的分析
  4. win10从零安装配置pytorch全过程
  5. 给所有正在寻找另一半的朋友
  6. STM32CUBEIDE小技巧: Bookmark 书签
  7. fake_useragent—Error occurred during loading data报错问题
  8. 28python成功输出腾讯位置大数据北京地区迁出数据
  9. 常数变易法_打印图形
  10. CAD如何免费转换PDF