Home >Backend Development >PHP Tutorial >The html template completes page static and template paging processing
It only allows you to modify a certain part of the page. Of course, this "certain part" is determined by you. The artist first makes a page, and then we use this page as a template (it should be noted that this template does not need to use code like EditRegion3, this code is a logo made by Dreamwerver to facilitate its own design), and use this template The place we need to change is replaced with a character that can be distinguished from HTML, such as "{title}", "[title]". When generating a static page, you only need to replace the data with these strings. This is what templates are about.
Create a php page and an html page [template page]; Note: If the data is called from the database, save the data in the form of an array, and then generate it in a loop;
In the php page, open the html Page->Read the content of the html page->Replace parameters->Create (open) a new html page->Write the replaced content into a new file->Close the new file->Generate successfully ;
$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 batch generation html test:
//假设从数据库中调的数据存放在二维数组$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 "生成"; }
Paging problem:
Like us When paging is specified, there are 20 articles per page. There are 45 articles in a certain sub-channel list after database query. First, we obtain the following parameters through query: 1, total number of pages; 2, number of articles per page. The second step, for ($i = 0; $i < allpages; $i++), page element acquisition, analysis, and article generation are all executed in this loop. The difference is that the sentence die ("Create file".$filename."Success!"; is removed and placed in the display after the loop, because this statement will abort program execution. Example:
$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 ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");
The above is the detailed content of The html template completes page static and template paging processing. For more information, please follow other related articles on the PHP Chinese website!