Home Backend Development PHP Tutorial A brief introduction to the idea of ​​generating HTML with PHP_PHP Tutorial

A brief introduction to the idea of ​​generating HTML with PHP_PHP Tutorial

Jul 13, 2016 pm 05:20 PM
html php superior Preface dynamic release Ideas news server generate of at present system net network

Foreword:

At present, the news release systems of many websites on the Internet use dynamic server technology to generate static HTML. The advantages of this are: first, it can reduce the burden on their servers, and second, because HTML static pages are generated, their The website has a greater chance of being found by search engines. The author's website once used PHP, a dynamic technology, to build a news release system. The principle is to apply PHP's technology to generate HTML static pages. The relevant platform is Windows XP Sp2+php4.32+mysql. Therefore, here, I want to simply Let’s talk about the idea of ​​​​this approach. This article is suitable for friends who have some basic knowledge of PHP+MYSQL database operations, SQL statements and web design. If you are a friend who is learning from scratch, then please lay a solid foundation first! There is no need to look down here. If you meet the above conditions, congratulations, please read on. However, before actually building it, you need to make the following preparations.

1. With the function of local debugging of PHP

Under the WINDOWS XP operating system, the author recommends that you download a PHP+MYSQL+APHCHE server package from the Internet, such as Huajun Software Park, search there and you can download it. After downloading, you can install it by default. In this way, you will have the function of testing PHP locally, saving a lot of trouble of manual configuration. How about, simple, OK, this is just the first step.

2. Conceive the functions of the news release system

The news release on the homepage is often updated through the background. The background updates are nothing more than basic functions such as adding, editing, and deleting data. Here, you can use web design software to build the backend interface you want. Of course, PHP is used to realize its functions. In this step, it is recommended that you first think about the functions that the news release system should have. Here, how to use PHP to add, edit, and delete data will not be repeated, because the focus is on how to generate static technology on this basis.

This news has 2 pages in total, currently on page 1 1 2

三、 PHP生成HTML的技术原理。

哈哈。费话说了那么多,终于到了该讲的地方了。其实,这一原理并不复杂,综合地来讲的话,应该是PHP中一个替换数据语法的应用。OK,讲一个简单范例吧,一步一步地分析!相信聪明的你能看得明白的咯,仔细看好每一步即可,在这里,只是引导大家如何做而已,具体的可以实践一下!

(1)在MYSQL里新建一数据库,命名为 database (可自定义),新建一表,命名为 news(因为是新闻发布嘛,取个好记的名字即可,可自定义),然后建立这几个字段名:
  id (自动递增,这是关键,类型:INT)
  title (顾名思义,新闻标题,类型可取 TEXT)
  content (新闻内容,类型可取 TEXT)
  path (HTML文件路径,类型可取 TEXT)

(2)建立 conn.php
  这是连接数据库的PHP文件,你可以把连接数据的语句单独放在这一文件里,以后多个需要连接数据库的文件直接引用这个文件即可。

(3)设计添加新闻的表格 add.form 简单的源代码如下:
  

//提交至 add.php
  新闻标题:

  新闻内容:

  
  

代码拷贝框


[Ctrl+A 全部选择 然后拷贝]

(4)建立一个 HTML 的模板,另存为model.htm,和 add.php可以在同一目录下。
  示例源代码:
  
  
  此新闻的标题:{title}
  此新闻的内容:{content}
  
  
  { }大括号内的内容即是要被替换的内容,整个静态模板的设计可以根据自己的思路,但{ }内被替换的内容必须包含在内,如上面的{title},{content};咔咔~简单地说,设计好一个很好看的新闻模板后,把要被替换的如{title},{content}等标记放到需要的地方就可以了撒。

(5)详解 add.php 源码
   require_once(“conn.php”); //引用conn.php,连接数据库
$title=$_POST[“title”];
$content=$_POST[“content”]; //获得表单变量

//以下建立一文本文档,其值自动计数
$countfile="count.txt";
if(!file_exists($countfile))
{
fopen($countfile,"w"); //如果此文件不存在,则自动建立一个
}
$fp=fopen($countfile,"r");
$num=fgets($fp,20);
$num=$num+1; //每次其值自动加一
fclose($fp);
$fp=fopen($countfile,"w");
fwrite($fp,$num); //更新其值
fclose($fp);

代码拷贝框

[Ctrl+A 全部选择 然后拷贝]


//Use the automatically counted value above to obtain the HTML path $path
$houzui=”.html”;
$path=$num.$houzui;
//This is formed The path is automatically increased, such as 1.html, 2.html, 3.html…………. Adding a news will automatically add 1

//The following uses SQL statements to add data to the table news
$sql="insert into news (title,content,path) values ​​('".$title."','".$content."','".$path."')";
$query=mysql_query($sql);

//The following is the key point, replace the {title}, {content} tags in the template with the data obtained from the form $fp=fopen(“model.htm”,”r”) //Only Read and open the template
$str=fread($fp,filesize(“mode.htm”));//Read the content in the template
$str=str_replace(“{title}”,$title,$str );
$str=str_replace(“{content}”,$content,$str);//Replace content
fclose($fp);

$handle=fopen($path, "w"); //Open the news path in writing mode
fwrite($handle,$str); //Write the content just replaced into the generated HTML file
fclose($handle);

Code copy box


[Ctrl+A select all and copy]
//Finishing work:
echo “View the news just added”;

Code copy box

[Ctrl+A select all and then copy]
OK, the entire sample source code for generating HTML is here. The key is to use the replacement method.
$str=str_replace("{replaced content}",$replaced content,$str);

Therefore, to summarize the above approach: first design the news template and put the content that needs to be replaced Use { } to put the content in the corresponding position in the template, then design the form, and then the final form handler, replace the variables obtained from the form with the corresponding content in the template, so that different HTML will be generated every time; The same is true if you need to modify the content of the HTML. After obtaining the modified form content, first update the database with the update statement, and then replace the content in the template; if you want to delete, first delete the content to be deleted in the table, and then use unlink($path) to delete the physical file of HTML.

This news has a total of 2 pages, currently on page 2 1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532608.htmlTechArticlePreface: At present, the news release systems of many websites on the Internet use dynamic server technology to generate static HTML. The benefits are: First, it can reduce the burden on its server...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles