PHP操作文章列表相關(guān)技巧分享
PHP被許多程序員用于網(wǎng)站的開發(fā)建設(shè)中。在網(wǎng)站實際建設(shè)時,通常都會需要一個文章列表的需求。下面就為大家介紹有關(guān)PHP操作文章列表的一些實現(xiàn)技巧。#t#
在數(shù)據(jù)庫中創(chuàng)建字段,記錄文件名,每生成一個文件,將自動生成的文件名存入數(shù)據(jù)庫,對于推薦文章,只需指向存放靜態(tài)文件的指定文件夾中的該頁面即可。 利用PHP操作文章列表,存為字符串,生成頁面時替換此字符串即可。如,在頁面中放置文章列表的表格加入標記{articletable},而在 PHP處理文件中:
PHP操作文章列表實現(xiàn)代碼:
- < ?php
 - $title = "http://siyizhu.com測試模板";
 - $file = "TwoMax Inter test templet,
<br>author:Matrix@Two_Max"; - $fp = fopen ("temp.html","r");
 - $content = fread ($fp,filesize ("temp.html"));
 - $content = str_replace ("{file}",$file,$content);
 - $content = str_replace ("{title}",$title,$content);
 - // 生成列表開始
 - $list = '';
 - $sql = "select id,title,filename from article";
 - $query = mysql_query ($sql);
 - while ($result = mysql_fetch_array ($query)){
 - $list .= '<a href='.$root.$result['filename'].' 
target=_blank>'.$result['title'].'</a><br>'; - }
 - $content .= str_replace ("{articletable}"
,$list,$content); - //生成列表結(jié)束
 - // echo $content;
 - $filename = "test/test.html";
 - $handle = fopen ($filename,"w");
//打開文件指針,創(chuàng)建文件 - /*
 - 檢查文件是否被創(chuàng)建且可寫
 - */
 - if (!is_writable ($filename)){
 - die ("文件:".$filename."不可寫,
請檢查其屬性后重試!"); - }
 - if (!fwrite ($handle,$content)){ 
//將信息寫入文件 - die ("生成文件".$filename."失敗!");
 - }
 - fclose ($handle); //關(guān)閉指針
 - die ("創(chuàng)建文件".$filename."成功!");
 - ?>
 
以上這段代碼示例就是PHP操作文章列表的相關(guān)使用方法。















 
 
 
 
 
 
 