zoukankan      html  css  js  c++  java
  • html模板生成静态页面及模板分页处理

    它只让你修改页面的某一部分,当然这“某一部分”是由你来确定的。美工先做好一个页面,然后我们把这个页面当作模板(要注意的是这个模板就没必要使用EditRegion3这样的代码了,这种代码是Dreamwerver为了方便自己设计而弄的标识),把这个模板中我们需要改变的地方用一个与HTML可以区分的字符代替,如“{title}”、“[title]”。在生成静态页面的时候只需要把数据和这些字符串替换即可。这就是模板的含义了。

    新建一个php页面和一个html页面[模板页];注:如果是从数据库调用数据,则将数据以数组的形式保存,然后循环生成;
    在php页面,打开html页面->读取html页面的内容->替换参数->新建(打开)一个新的html页面->将替换的内容写入新文件中->关闭新文件->生成成功;

    $open = fopen("template.htm","r"); //打开模板文件
    $content = fread($open,filesize("template.htm")); //读取模板文件内容
    //print_r($content);
    $content = str_replace("{title}","测试标题",$content);//替换
    $content = str_replace("{contents}","测试内容",$content);
    
    $newtemp = fopen("1.htm","w");//生成,用写入方式打开一个不存在(新)的页面
    fwrite($newtemp,$content);//将刚刚替换的内容写入新文件中
    fclose($newtemp);
    echo "生成";

    php批量生成html测试:

    //假设从数据库中调的数据存放在二维数组$arr中
    $arr = array(array("新闻标题一","新闻内容一"),array("新闻标题二","新闻内容二")); 
    
    foreach($arr as $key=>$value){
     $title = $value[0];
     $contents = $value[1];
     //echo $title.''.$contents.'';
     $path = $key.'.html';
     $open = fopen("template.htm","r"); //打开模板文件
     $handle = fread($open,filesize("template.htm")); //读取模板文件内容
    
     $content = str_replace("{title}",$title,$handle);//替换
     $content = str_replace("{contents}",$contents,$handle);
    
     $newtemp = fopen($path,"w");//用写入方式打开一个不存在(新)的页面
     fwrite($newtemp,$content);//将刚刚替换的内容写入新文件中
     fclose($newtemp);
     echo "生成";
    }

    分页问题:

    如我们指定分页时,每页20篇。某子频道列表内文章经数据库查询为45条,则,首先我们通过查询得到如下参数:1,总页数;2,每页篇数。第二步,for ($i = 0; $i < allpages; $i++),页面元素获取,分析,文章生成,都在此循环中执行。不同的是,die ("创建文件".$filename."成功!";这句去掉,放到循环后的显示,因为该语句将中止程序执行。例:

    $fp= fopen ("temp.html","r");
    $content= fread ($fp,filesize ("temp.html"));
    $onepage= ’20’;
    $sql    = "select id from article where channel=’$channelid’";
    $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 name,filename,title from article where channel=’$channelid’ limit $start,$onepage";
        $query_for_page = mysql_query ($sql_for_page);
        while ($result = $query_for_page){
            $list .= ’<a href=’.$root.$result[’filename’].’ target=_blank>’.$title.’</a><br>’;
        }
        $content = str_replace ("{articletable}",$list,$content);
        if(is_file ($indexpath)){
            @unlink ($indexpath); //若文件已存在,则删除
        }
        $handle = fopen ($indexpath,"w"); //打开文件指针,创建文件
        if (!is_writable ($indexpath)){
            echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo
        }
        if (!fwrite ($handle,$content)){   //将信息写入文件
            echo "生成文件".$indexpath."失败!"; //修改为echo
        }
        fclose ($handle); //关闭指针
    }
    fclose ($fp);
    die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");
  • 相关阅读:
    UML序列图总结
    数据库水平切分的实现原理解析
    oracle imp file data
    putty的设置
    run java jar command
    forex website
    forex tables
    ubuntu set defult jdk
    友情连接
    jstl tag
  • 原文地址:https://www.cnblogs.com/sztx/p/9499762.html
Copyright © 2011-2022 走看看