


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.
三、 PHP生成HTML的技术原理。
哈哈。费话说了那么多,终于到了该讲的地方了。其实,这一原理并不复杂,综合地来讲的话,应该是PHP中一个替换数据语法的应用。OK,讲一个简单范例吧,一步一步地分析!相信聪明的你能看得明白的咯,仔细看好每一步即可,在这里,只是引导大家如何做而已,具体的可以实践一下!
(1)在MYSQL里新建一数据库,命名为 database (可自定义),新建一表,命名为 news(因为是新闻发布嘛,取个好记的名字即可,可自定义),然后建立这几个字段名:
id (自动递增,这是关键,类型:INT)
title (顾名思义,新闻标题,类型可取 TEXT)
content (新闻内容,类型可取 TEXT)
path (HTML文件路径,类型可取 TEXT)
(2)建立 conn.php
这是连接数据库的PHP文件,你可以把连接数据的语句单独放在这一文件里,以后多个需要连接数据库的文件直接引用这个文件即可。
(3)设计添加新闻的表格 add.form 简单的源代码如下:
代码拷贝框
[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);
代码拷贝框
//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 a>”;
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.

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.