PHP操作文章列表相關(guān)技巧分享
PHP被許多程序員用于網(wǎng)站的開(kāi)發(fā)建設(shè)中。在網(wǎng)站實(shí)際建設(shè)時(shí),通常都會(huì)需要一個(gè)文章列表的需求。下面就為大家介紹有關(guān)PHP操作文章列表的一些實(shí)現(xiàn)技巧。#t#
在數(shù)據(jù)庫(kù)中創(chuàng)建字段,記錄文件名,每生成一個(gè)文件,將自動(dòng)生成的文件名存入數(shù)據(jù)庫(kù),對(duì)于推薦文章,只需指向存放靜態(tài)文件的指定文件夾中的該頁(yè)面即可。 利用PHP操作文章列表,存為字符串,生成頁(yè)面時(shí)替換此字符串即可。如,在頁(yè)面中放置文章列表的表格加入標(biāo)記{articletable},而在 PHP處理文件中:
PHP操作文章列表實(shí)現(xiàn)代碼:
- < ?php
- $title = "http://siyizhu.com測(cè)試模板";
- $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);
- // 生成列表開(kāi)始
- $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");
//打開(kāi)文件指針,創(chuàng)建文件 - /*
- 檢查文件是否被創(chuàng)建且可寫(xiě)
- */
- if (!is_writable ($filename)){
- die ("文件:".$filename."不可寫(xiě),
請(qǐng)檢查其屬性后重試!"); - }
- if (!fwrite ($handle,$content)){
//將信息寫(xiě)入文件 - die ("生成文件".$filename."失??!");
- }
- fclose ($handle); //關(guān)閉指針
- die ("創(chuàng)建文件".$filename."成功!");
- ?>
以上這段代碼示例就是PHP操作文章列表的相關(guān)使用方法。