- 论坛徽章:
- 0
|
从网上找了几个代码,多多少少有些问题,现在发一个没有错误的代码上来,希望刚接触这方面的人少走弯路
- <?php
- $aa = date("Ym");
- mkdir ($aa, 0777);
- define('__root', './'.$aa.'/');
- $connect = mysql_connect("localhost","root","123");
- mysql_select_db("html");
- $time = date("Ymd");
- $onepage = '20';
- $sql = "select id from article";
- $query = mysql_query($sql);
- $num = mysql_num_rows($query);
- $allpages = ceil($num / $onepage);
- for($i = 0;$i < $allpages;$i++){
- if($i == 0){
- $indexpath = __root.$time.$i.".html";
- }else {
- $indexpath = __root.$time.$i.".html";
- }
- $start = $i * $onepage;
- // 生成列表开始
- $list = '';
- $sql = "select id,title,filename from article limit $start,$onepage";
- $query = mysql_query ($sql,$connect);
- while($result = mysql_fetch_array ($query)){
- $list .= "<br><a href=".__root.$result['filename'].".html target=_blank>".$result['title']."</a><br/>n";
- }
- $title = "拓迈国际测试模板";
- $file = "TwoMax Inter test templet,<br/>author:Matrix@Two_Max";
- /*******************求分页*************/
- $fenye = '';
- if($i == 0){$fenye .= '<a href="'.$time.($i+1).'.html"> 下一页 </a>
- <a href="'.$time.($allpages-1).'.html"> 最后一页 </a> ';
- $fenye .= "\n";}
- if($i >= 1 && $i< ($allpages -1)){$fenye .= '<a href="'.$time.'0.html"> 第一页 </a>
- <a href="'.$time.($i-1).'.html"> 上一页 </a>
- <a href="'.$time.($i+1).'.html"> 下一页 </a>
- <a href="'.$time.($allpages-1).'.html"> 最后一页 </a> ';
- $fenye .= "\n";}
- if($i == ($allpages-1)){$fenye .= '<a href="'.$time.'0.html"> 第一页 </a>
- <a href="'.$time.($i-1).'.html"> 上一页 </a> ';
- $fenye .= "\n";}
- $fenye .= ' 共 '.$allpages.' 页,当前第 '.($i+1).' 页 ';
- /****************字符串替换*****************/
- $fp = fopen ("temp.html","r");
- $content = fread ($fp,filesize ("temp.html"));
- $content = str_replace ("n{articletable}",$list,$content);
- $content = str_replace ("n{file}",$file,$content);
- $content = str_replace ("{title}",$title,$content);
- $content = str_replace ("{fenye}",$fenye,$content);
- if (is_file($indexpath)){
- @unlink($indexpath);//若文件已存在,则删除
- }
- $handle = fopen ($indexpath,"w"); //打开文件指针,创建文件
- if (!is_writable ($indexpath)){
- die ("文件:".$indexpath."不可写,请检查其属性后重试!");
- }
- if (!fwrite ($handle,$content)){ //将信息写入文件
- die ("生成文件".$indexpath."失败!");
- }
- echo ("创建文件".$indexpath."成功!<br>");
- }
- fclose ($handle); //关闭指针
- ?>
复制代码
[ 本帖最后由 yanjing5462 于 2005-11-19 11:37 编辑 ] |
|