PHP新闻分页类一例
本节内容:
PHP新闻分页类
将以下内容,保存为文件:content2page.class.php。
代码:
复制代码 代码示例:
<?php
/***
*类名:content2page.class.php
*描述:用于自动生成新闻静态页,手动添加分页功能,自动生成页码
*整理:
使用示例:
$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()
Description: 用来将新闻内容分页输出
Calls: ReadFromFile(),Write2File(),GetPageCount()
Input: 含分页符的新闻内容
Output: 已经分页的HTML静态页面
Return: void
Access: public
***/
function Content2Html()
{
$FileNameTemp = $this->FileName;
if ($_POST['Submit'])
{
if($this->Content==http://www.jquerycn.cn/a_24506/"")
{
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 文件名
Description: 用来读取文件内容
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 要写入的文件
Description: 用来写入文件内容
Called By: content2html()
Return: void
Access: public
*******************************************/
function Write2File($content,$file)
{
$fp=@fopen($file,"w");
@fputs($fp,$content);
@fclose($file);
}
/********************************************
Function: GetPageCount()
Description: 分几种情况输出页面分页的格式
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 用来传递当前页面数
Description: 给当前页面标志醒目的颜色
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
?>
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://www.juheyunku.com/jiaob/shell/10661.shtml
相关文章
热门TAG
命令 权重 外链 企业网站 白帽 php 织梦教程 dedecms修改内容 javascript 织梦 功能 标签 调用 详解 服务器 网站流量 实例解析 Dedecms 织梦cms HTML tags标签 python jquery教程 jquery windows SEO优化 蜘蛛 搜索引擎 网站收录 JSP最新文章
-
Centos下PHP5升级为PHP7的方法
时间:2021-01-03
-
在php当中常量和变量的区
时间:2020-12-29
-
PHP中经典的四大排序算法
时间:2020-12-29
-
dw怎么运行php文件?
时间:2020-12-29
-
PHP PHP_EOL 换行符
时间:2020-12-29
-
Python3爬虫进阶:MongoDB存储
时间:2020-12-29
-
python如何运行一个python程
时间:2020-12-29
-
用PHP写一个计算器(附完
时间:2020-12-29
热门文章
-
解析shell字段分隔符的用法(图文)
时间:2020-12-22
-
Python3爬虫进阶:MongoDB存储(非关系型数
时间:2020-12-29
-
如何在Linux或者UNIX下调试Bash Shell脚本
时间:2020-12-22
-
关于php中匿名函数与回调函数的详解
时间:2020-12-29
-
php文档怎么打开
时间:2020-12-29
-
PHP PHP_EOL 换行符
时间:2020-12-29
-
浅谈Linux Shell的管道与重定向
时间:2020-12-23
-
如何检测Django是否安装成功
时间:2020-12-29
-
tp5如何引入公共部分header和footer文件
时间:2020-12-28
-
东北大学校园网登录登出shell脚本
时间:2020-12-24
