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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis: Understanding Its Architecture and Purpose Redis: Understanding Its Architecture and Purpose Apr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

MongoDB's Future: The State of the Database MongoDB's Future: The State of the Database Apr 25, 2025 am 12:21 AM

MongoDB's future is full of possibilities: 1. The development of cloud-native databases, 2. The fields of artificial intelligence and big data are focused, 3. The improvement of security and compliance. MongoDB continues to advance and make breakthroughs in technological innovation, market position and future development direction.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

See all articles