<?php
$conn=mysql_connect('localhost','root','wy')
or die('串連失敗:'.mysql_error());
//選擇資料表
if (mysql_select_db('mynews',$conn))
{
echo'選擇資料庫成功!'.'<p>';
}
else
{
echo'資料庫選擇失敗!'.mysql_error().'<p>';
}
@header("Content-Type: text/html; charset=utf-8");
mysql_query("SET NAMES 'utf8'");
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$onepage =2;
$sql = "select id from news";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
for ($i = 0;$i<$allpages; $i++){
if ($i == 0){
$indexpath = "index.html";
} else {
$indexpath = "index_".$i.".html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select * from news limit $start,$onepage";
$result=mysql_query($sql_for_page);
while($row=mysql_fetch_array($result))
{
$list .= 'uid='.$row['id'].$row['title'].'<br>';
}
$content1 = str_replace ("{ articletable }",$list.$i,$content);
//分頁
$list1 = '';
for ($j = 0;$j<$allpages; $j++){
if ($j == 0){
$list1 .= '<a href="index.html" >第'.$j.'頁 </a>|';
} else {
$list1 .= "<a href='index_".$j.".html' >第".$j."頁 </a>|";
}
}
$content2 = str_replace ("{ mune }",$list1,$content1);
if (is_file ($indexpath)){
@unlink ($indexpath); //若檔案已存在,則刪除
}
$handle = fopen ($indexpath,"w"); //開啟檔案指標,建立檔案
/*
檢查檔案是否被建立且可寫
*/
if (!is_writable ($indexpath)){
echo "檔案:".$indexpath."不可寫,請檢查其屬性後重試!"; //修改為echo
}
if (!fwrite ($handle,$content2)){ //將資訊寫入檔案
echo "組建檔案".$indexpath."失敗!"; //修改為echo
}
fclose ($handle); //關閉指標
}
fclose ($fp);
die ("產生分頁檔案完成,如產生不完全,請檢查檔案許可權系統後重建!");
?>
temp.html
<HTML>
<TITLE>{ title }</TITLE>
<BODY>
this is a { file } file's templets
{ articletable }
分頁 { mune }
</BODY>
</HTML>