Home php教程 php手册 PHP生成HTML的技术原理

PHP生成HTML的技术原理

Jun 13, 2016 am 11:34 AM
database html mysql php one principle Can exist technology database New generate of customize

1)在MYSQL里新建一数据库,命名为 database (可自定义),新建一表,命名为 news(因为是新闻发布嘛,取个好记的名字即可,可自定义),然后建立这几个字段名:


  id (自动递增,这是关键,类型:INT)
  title (顾名思义,新闻标题,类型可取 TEXT)
  content (新闻内容,类型可取 TEXT)
  path (HTML文件路径,类型可取 TEXT)

   2)建立 conn.php


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


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


   

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

  新闻内容:

  
  




   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);
//利用上面自动计数的值获得HTML的路径$path
  $houzui=”.html”;
  $path=$num.$houzui;
  //这样形成的路径是自动增长的,如1.html,2.html,3.html……….添加一条新闻便自动加上1

  //以下用SQL语句添加数据至表 news

$sql=”insert into news (title,content,path) values (‘”.$title.”’,’”.$content.”’,’”.$path.”’)”;
  $query=mysql_query($sql);
//以下为关键之处,把从表单获得的数据替换模板中的{title},{content}标记

  $fp=fopen(“model.htm”,”r”) //只读打开模板
  $str=fread($fp,filesize(“mode.htm”));//读取模板中内容
  $str=str_replace(“{title}”,$title,$str);
  $str=str_replace(“{content}”,$content,$str);//替换内容
  fclose($fp);  

  $handle=fopen($path,”w”); //写入方式打开新闻路径
  fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
  fclose($handle);
//收尾工作:

echo “查看刚才添加的新闻”;
OK,整个生成HTML的示例源码就到这里,其关键是用了替换的方法。
  $str=str_replace(“{被替换的内容}”,$替换的内容,$str);

    因此,总结一下以上的做法:先设计好新闻模板,把需要被替换的内容用{ }放到模板中相应的位置,然后设计表单,再是最后的表单处理程序,把从表单中获取的变量替换模板中相应的内容即可,这样每次都会生成不同的HTML;如果 需要修改HTML的内容也是一样的,获得修改后的表单内容后,先用 update 语句更新数据库,再重新替换一下模板中的内容即可;删除的话,先delete表中要删除的内容,再用unlink($path) 来删除HTML的物理文件即可。
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

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

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

How to Add Elements to the End of an Array in PHP How to Add Elements to the End of an Array in PHP Feb 07, 2025 am 11:17 AM

Arrays are linear data structures used to process data in programming. Sometimes when we are processing arrays we need to add new elements to the existing array. In this article, we will discuss several ways to add elements to the end of an array in PHP, with code examples, output, and time and space complexity analysis for each method. Here are the different ways to add elements to an array: Use square brackets [] In PHP, the way to add elements to the end of an array is to use square brackets []. This syntax only works in cases where we want to add only a single element. The following is the syntax: $array[] = value; Example

See all articles