Php static page generation program and principle analysis-PHP Tutorial

Source: Internet
Author: User
Php generates static page programs and analysis of principles. Generating static pages is a good choice for php to reduce server load and seo website optimization, therefore, the php static page generation function is a good choice for php programmers to understand and master the generation of static pages to reduce server load and seo website optimization, so the php static page generation function is a knowledge point that almost all php programmers must understand and master. let me introduce you to the analysis of the principle of generating static pages in php, for more information, see.

Generating html principles

We will write the tag to be generated into a template file, and then use php to read and replace the specified tag with the content we want to replace. now the mainstream dedecms system does the same.

Generate static page code.


The template does not fill in the html file. For example:

The code is as follows:

Temp.html

  

  {Title}

  

This is a {file} fileArray; s templets

  

  
Templetest. php

$ Title = "Tomai international test template ";

$ File = "TwoMax Inter test templet,
Author: Matrix @ Two_Max ";

$ Fp = fopen ("temp.html", "r ");

$ Content = fread ($ fp, filesize ("temp.html "));

$ Content. = str_replace ("{file}", $ file, $ content );

$ Content. = str_replace ("{title}", $ title, $ content );

Echo $ content;

?>

This is an ultra-simple php function to generate static pages, but this is not practical in the application. next I will introduce a database to generate an instance.

1. create a test database and a user table as follows (several test databases are inserted by yourself ):

The code is as follows:


Create table if not exists 'news '(
'Id' int (10) not null AUTO_INCREMENT,
'Title' varchar (128) default null,
'Content' text,
'Time' int (10) default null,
Primary key ('id ')
) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 12;

2. create the connection data file conn. php

The code is as follows:
$ Dsn = "mysql: host = localhost; dbname = test ;";
$ User = "root ";
$ Password = "";
Try {
$ Dbh = new PDO ($ dsn, $ user, $ password );
} Catch (PDOException $ e ){
Echo "connection failed". $ e-> getMessage ();
}
?>

3. display the news list (news. php). Note that the connection is a static html connection, which is not generated yet. of course, the link cannot be opened:

The code is as follows:

Add article

Require_once "conn. php ";
$ SQL = "select * from news ";
Foreach ($ dbh-> query ($ SQL) as $ row ){
Echo "{$ row ['title']} ---- modify an article
";
}
?>

4. add and modify the article page:

The code is as follows:

// Obtain the modified content
If ($ _ GET ['id']) {
Require_once "conn. php ";
$ SQL = "select * from news where id = {$ _ GET ['id']}";
$ Res = $ dbh-> query ($ SQL)-> fetch ();
}
?>

5.template.html

The code is as follows:




{Title}



{Title} posted on {time}

{Content}

6. action. php is of course used to generate and update static files:

The code is as follows:


// Form processing operation
Header ("content-type: text/html; charset = utf-8 ");
Require_once 'Conn. php ';
$ Title = $ _ POST ['title'];
$ Content = $ _ POST ['content'];
$ Time = time ();
If ($ _ POST ['submit '] = 'ADD '){
$ SQL = "insert into news values ('', '$ title',' $ content', $ time )";
$ Dbh-> query ($ SQL );
$ Id = $ dbh-> lastInsertId ();
$ Filename = "news_registry.id=.html ";
$ Fp_tmp = fopen ("template.html", "r ");
$ Fp_html = fopen ($ filename, "w ");
While (! Feof ($ fp_tmp )){
$ Row = fgets ($ fp_tmp );
$ Row = replace ($ row, $ title, $ content, date ('Y-m-d H: I: S', $ time ));
Fwrite ($ fp_html, $ row );
}
Fclose ($ fp_tmp );
Fclose ($ fp_html );
Echo "added successfully and generated static files ";
} Else {
$ SQL = "update news set title = $ title, content = $ content, time = $ time where id ={$ _ POST ['id']}";
$ Dbh-> query ($ SQL );
$ Filename = "news_00000000_post0000'id'00000000.html ";
@ Unlink ($ filename );
$ Fp_tmp = fopen ("template.html", "r ");
$ Fp_html = fopen ($ filename, "w ");
While (! Feof ($ fp_tmp )){
$ Row = fgets ($ fp_tmp );
$ Row = replace ($ row, $ title, $ content, date ('Y-m-d H: I: S', $ time ));
Fwrite ($ fp_html, $ row );
}
Fclose ($ fp_tmp );
Fclose ($ fp_html );
Echo "the update is successful and the static file is updated ";
}
// Replace functions row by row
Function replace ($ row, $ title, $ content, $ time ){
$ Row = str_replace ("{title}", $ title, $ row );
$ Row = str_replace ("{content}", $ content, $ row );
$ Row = str_replace ("{time}", $ time, $ row );
Return $ row;
}
?>


In this way, a complete php generation static page system is complete.

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.