php產生靜態html分頁實現方法

來源:互聯網
上載者:User

<?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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.