html template generation static page and template paging

Keywords: PHP Database SQL

It only allows you to modify a part of the page, of course, this "part" is determined by you. Artists first make a page, and then we use it as a template. (Note that this template does not need to use EditRegion3 code, which Dreamwerver designed to facilitate their own logo.) Replace the template with a character that can be distinguished from HTML, such as "{title}" and "[title]. When generating static pages, you just need to replace the data with these strings. That's what templates mean.

Create a new php page and an html page [template page]; note: if you call data from the database, the data will be saved in the form of arrays, and then generated circularly;
On the php page, open the html page - > read the content of the html page - > replace parameters - > create a new html page - > write the replaced content to the new file - > close the new file - > generate successfully;

$open = fopen("template.htm","r"); //Open the template file
$content = fread($open,filesize("template.htm")); //Read template file content
//print_r($content);
$content = str_replace("{title}","Test title",$content);//replace
$content = str_replace("{contents}","Test content",$content);

$newtemp = fopen("1.htm","w");//generate,Open a non-existent (new) page by writing
fwrite($newtemp,$content);//Write the newly replaced content to a new file
fclose($newtemp);
echo "generate";

php batch generation html test:

//Suppose that the data transferred from the database is stored in a two-dimensional array. $arr in
$arr = array(array("Headline 1","News Content 1"),array("Headline II","News Content II")); 

foreach($arr as $key=>$value){
 $title = $value[0];
 $contents = $value[1];
 //echo $title.''.$contents.'';
 $path = $key.'.html';
 $open = fopen("template.htm","r"); //Open the template file
 $handle = fread($open,filesize("template.htm")); //Read template file content

 $content = str_replace("{title}",$title,$handle);//replace
 $content = str_replace("{contents}",$contents,$handle);

 $newtemp = fopen($path,"w");//Open a non-existent (new) page by writing
 fwrite($newtemp,$content);//Write the newly replaced content to a new file
 fclose($newtemp);
 echo "generate";
}

Paging issues:

If we specify paging, there are 20 pages per page. Firstly, we get the following parameters by querying 45 articles in a subchannel list: 1, total pages; 2, number of pages per page. The second step, for ($i = 0; $i < allpages; $i ++), page element acquisition, analysis, article generation, are all executed in this loop. The difference is that die ("Create a file". $filename. "Succeed! "; This sentence is removed and displayed after the loop, because it will stop the 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); //If the file already exists, delete it
    }
    $handle = fopen ($indexpath,"w"); //Open the file pointer and create the file
    if (!is_writable ($indexpath)){
        echo "Document:".$indexpath."Unwritable, please check its properties and try again!"; //Modified to echo
    }
    if (!fwrite ($handle,$content)){   //Write information to a file
        echo "Generate file".$indexpath."Failure!"; //Modified to echo
    }
    fclose ($handle); //Close the pointer
}
fclose ($fp);
die ("The generation of paging files is completed. If the generation is incomplete, please check the file permission system and regenerate it.");

Posted by rockinaway on Sun, 02 Jun 2019 16:00:13 -0700