程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php文章內容分頁並生成相應的htm靜態頁面代碼

php文章內容分頁並生成相應的htm靜態頁面代碼

編輯:關於PHP編程

復制代碼 代碼如下:
<?php
$url='test.php?1=1';
$contents="fjka;fjsa;#page#批量生成分成文件並且加上分頁代碼";
$ptext ='#page#';
ContentsPages($url,$contents,$ptext)
/**
* 函數名:ContentsPages
* 功能:文章內容分頁
* 參數:$url文章內容頁URL 類型string,$contents文章內容頁內容 類型string,$ptext分頁標識 類型string
* 作者:七夜
* QQ:64438136 [email protected] MSN:[email protected]
*/
//文章數組
$arr = explode($ptext,$contents);
//數組長度&總頁數
$total = count($arr);
//當前頁
$nowpage = $_GET['pages']?$_GET['pages']:1;
//上頁
$prepage = $nowpage==1?1:$nowpage-1;
//下頁
$nextpage = $nowpage>$total-1?$total:$nowpage+1;
//最後一頁
$lastpage = $total;
$pdiv = '<br /><div style=\"height:30px;width:500px;border:1px; blackground-color:#ff0000;\">';
$pdiv .= "第{$nowpage}頁 / 總{$total}頁  ";
//首頁鏈接
$pdiv .= "<li style=\"display:inline;\"><a href=\"{$url}&pages=1\" style=\"text-decoration:none;\">首頁</a></li>";
//上頁鏈接
$pdiv .= "<li style=\"display:inline;\"><a href=\"{$url}&pages={$prepage}\" style=\"text-decoration:none;\">  上一頁  </a></li>";
//顯示分頁列表
$color = '';
for($i = 1; $i<=$total; $i++)
{
if($i == $nowpage)
{
$color= "color:#ff0000;";
}
else
{
$color = '';
}
$pdiv .= "  <li style=\"display:inline;\"><a href=\"{$url}&pages={$i}\" style=\"text-decoration:none;\"><span style=\"{$color}\">{$i}</span></a></li>";
}
//下頁鏈接
$pdiv .= "  <li style=\"display:inline;\"><a href=\"{$url}&pages={$nextpage}\" style=\"text-decoration:none;\">  下一頁</a></li>";
//末頁鏈接
$pdiv .= "<li style=\"display:inline;\"><a href=\"{$url}&pages={$lastpage}\" style=\"text-decoration:none;\">  末頁  </a></li>";
$pdiv .= '</div>';
//輸出內容
echo $arr[$nowpage-1];
//不分頁則不顯示分頁列表
if( $total <=1) $pdiv = '';
//輸出分頁列表
echo $pdiv;
?>

下面是相關靜態生成頁面,雖然代碼不正確但思路是沒問題的。
復制代碼 代碼如下:
<?
$tmpBody = "fjka;fjsa;{page}批量生成分成文件並且加上分頁代碼";
$tmpUrl = 'www.jb51.net';
$tmpUrlarray = explode('.',$tmpUrl);
$tmpArray = explode('{page}',$tmpBody);
$total = count($tmpArray);
$id="2";
if( $total >1 )
{
$tmpStr = '';
for( $i=0;$i<$total;$i++ ) //遍歷所有分頁
{
if( $i==0 )
{
$str .='<a href='.$id.'.htm class=bodypage target=_self>'.($i+1).'</a> ';
}
else
{
$str .='<a href='.$id.'_'.$i.'.htm class=bodypage target=_self>'.($i+1).'</a> ';
}
}
//批量生成分成文件並且加上分頁代碼
$str = '<div class=mupage>'.$str.'</div>';
for( $j=0;$j<$total;$j++ )
{
if( $j==0 )
{
$fname =$id.'.htm';
}
else
{
$fname =$id.'_'.$j.'.htm';
}
$tempMu = str_replace('{htmlContent}',$tmpArray[$j].$str,$tempLate);
makeHtml($tempMu,$filePath,$fname); //這個函數就不說明了,一個創建文件的代碼
}
$mupage =1;
}
$tempLate = str_replace('{htmlContent}',$tmpBody,$tempLate); //普通文章 以{page}分頁取出保存到一個數組再生成分頁地址,然後再由根據explode分的數組的內容生成htm 頁面
$total=0;
$str='';
$i=0;
$j=0;
/*
本段代碼生成的原理是id_分頁號如100_1.htm,100_2.htm這樣的形式,先是把
*/
function makeHtml($tempMu,$filePath,$fname){
$fp = fopen($filePath.$filename, "w");
fwrite($fp, $tempMu);
fclose($fp);
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved