Home  >  Article  >  Backend Development  >  Detailed explanation on generating static pages with PHP_PHP Tutorial

Detailed explanation on generating static pages with PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:53:41839browse




Let me explain in detail about generating static pages with PHP
Answer
Hello everyone, let me explain in detail the steps to generate static pages with PHP:
In order to study this static generation, I spent a lot of time and share my experience with you:
The following is the create html class I encapsulated:
class makehtml

{

//+--------------------------

//+ Create HTML folder according to the format

//+--------------------------------



function c_dir($html_type,$root)

{

$htmldata_dir=$root;

if (!is_dir($htmldata_dir))

{

if (!mkdir($htmldata_dir,0777))

{

exit('Sorry, there is no HTML folder '.$htmldata_dir.' and it cannot be created.');

}

}

elseif (!is_writable($htmldata_dir))

{

exit('Sorry, the folder '.$htmldata_dir.' is not writable');

}

return $htmldata_dir;

}





function replace_tpl($data,$input_data,$name)

{//+ Replace variables in html template

$data=str_replace("{title}",$input_data['title'],$data);

$data=str_replace("{content}",$input_data['content'],$data);

return $data;

}

function create_html($htmlpage,$name,$root,$input_data)

{//+ Generate html file function create_html('template','static file name','file storage directory','submit data');



$htmldata_dir=$this->c_dir($html_type,$root); //c_dir("format","name")



$modeltpl=$htmlpage; //You can add path

$fp=fopen($modeltpl,'r');

$tpldata.=fread($fp,filesize($modeltpl));

fclose($fp);



$tpl_data=$this->replace_tpl($tpldata,$input_data,$name);



$fp=fopen($htmldata_dir.'/'.$name.'.shtml','w+');

unset($htmldata_dir);

fwrite($fp,$tpl_data);

fclose($fp);

}

}

?>
Copy code application method:
In conjunction with database operations, everyone builds database tables by themselves.
include("mysql.php");

include("create_html.php");

$html=new makehtml(); //Instantiate CREATE HTML object

$op=$_GET["op"];

if($op=='insert')

{

$title=$_POST["title"];

$content=$_POST["content"];



$file_path=date("Y-m",time()); //Name the folder in time format

$query=$DB->query("insert into news values('','$title','$content','$times','')");

$name=$DB->insert_id();

$sql_data=array(

'title'=>$title,

'content'=>$content

);

$html->create_html('model.html',$name,$file_path,$sql_data);

//+ means respectively: template page, generated name, storage folder, and data information written to the template

}

?>
Copy code
Note: I just wrote a simple idea here. The specific needs will depend on your application. This is the simplest release. The editor also hopes that friends who want to learn will continue
Continue research.


Other solutions
Must support the tutorial
Other solutions
Give it a try yourself, it’s about to fall off
Other solutions
That’s great, I’m just about to learn something like this
Other solutions
Not only do you want to like it, but you also want to collect it! Hahao(∩_∩)o...haha
Other solutions
Like again...
Other solutions
Great. Support it!
[img]http://www.phpchina.com/bbs/images/smilies/default/smile.gif[/img]
Other solutions
Collected. Haha..
Other solutions
I studied the DZ code for a long time!
I want to see them doing static!
hehe!
But I have seen several static methods of PHP blog programs!
They are all similar!
It’s similar to yours!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632383.htmlTechArticleLet me explain in detail about generating static pages with PHP Answer Hello everyone, let me explain in detail about generating static pages with PHP Page steps: In order to study this static generation, I spent...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn