Generate HTML
Objective:
At present, many Web sites in the news release system has adopted dynamic server technology to generate static HTML practices, the benefits are: one can reduce the burden on their servers, the second is because the generation of HTML static pages, so its Web site by search engines to search the probability of a greater number. The author's website has used PHP this dynamic technology to build news release system, its principle is to use PHP to generate HTML static page technology, the related platform is Windows XP Sp2+php4.32+mysql, therefore, here, want to simply talk about this approach. This article is suitable for PHP+MYSQL database operations, SQL statements and Web design a little bit of a friend, if you are a friend from the beginning, then please lay a good foundation! You don't have to look down here. If you all meet the above conditions, then congratulations, please continue to look down. However, you need to do the following to prepare yourself before you start building a concrete action.
One, have the function of debugging PHP locally
Under the Windows XP operating system, I suggest that you can download a Php+mysql+aphche server suite, such as the Huajun Software Park, and go there and search for it. After downloading the default installation can, so you have a local test PHP function, save a lot of manual configuration trouble, how, simple, OK, this is just the first step.
Second, the concept of the news release system features
The first page of the news is often updated through the background, the background of the update is simply by adding, editing, delete data and other basic functions realized. Here, you can use web design software to build your desired background interface, the realization of the function of course is to use PHP. This step suggests that you first think about the features that the news release system needs. Here, how to add, edit, delete data in PHP no longer repeats, because the focus is on how to generate static technology on this basis.
<?php
Require_once ("conn.php");//reference conn.php, connection database
$title =$_post["title"];
$content =$_post["Content"]; Get form variables
The following is a document that automatically counts the values
$countfile = "Count.txt";
if (!file_exists ($countfile))
{
fopen ($countfile, "w"); If this file does not exist, it automatically creates a
}
$FP =fopen ($countfile, "R");
$num =fgets ($FP, 20);
$num = $num +1; Each time its value automatically adds a
Fclose ($FP);
$FP =fopen ($countfile, "w");
Fwrite ($fp, $num); Update its value
Fclose ($FP);
[Ctrl + a All select and copy]
Use the value of the automatic counting above to get the HTML path $path
$houzui = ". html";
$path = $num. $houzui;
This form of path is automatically increased, such as 1.html,2.html,3.html ... add a piece of news and automatically adds 1.
The following add data to table news with SQL statements
$sql = "INSERT INTO News (Title,content,path) VALUES ('". $title. "', '". $content. "', '". $path. ")";
$query =mysql_query ($sql);
The key here is to replace the {title},{content} tag in the template with the data obtained from the form $fp =fopen ("model.htm", "R")//read-only open template
$str =fread ($fp, FileSize ("mode.htm"))//Read the contents of the template
$str =str_replace ("{title}", $title, $STR);
$str =str_replace ("{content}", $content, $STR);/replace content
Fclose ($FP);
$handle =fopen ($path, "w"); Write way to open a news path
Fwrite ($handle, $STR); Write what you just replaced into the generated HTML file
Fclose ($handle);
Code Copy Box
$FP =fopen ("model.htm", "R")//read-only open template
$str =fread ($fp, FileSize ("mode.htm"))//Read the contents of the template
$str =str_replace ("{title}", $title, $STR);
$str =str_replace ("{content}", $content, $STR);/replace content
Fclose ($FP);
$handle =fopen ($path, "w"); Write way to open a news path
Fwrite ($handle, $STR); Write what you just replaced into the generated HTML file
Fclose ($handle);
[Ctrl + a All select and copy]
Finishing work:
echo "<a href= $path target=_blank> View just added news </a>";
Code Copy Box
echo "<a href= $path target=_blank> View just added news </a>";
[Ctrl + a All select and copy]
OK, the whole generation of HTML sample source code is here, the key is to use the replacement method.
$str =str_replace ("{Replaced content}", $ replaced content, $STR);
So, to sum up the above practice: first design a news template, place what needs to be replaced with {} into the appropriate location in the template, then design the form, then the final form handler, and replace the variable from the form with the corresponding content in the template, so that each time a different HTML is generated , if you need to modify the content of HTML is the same, get the modified form content, first update the database with the UPDATE statement, and then replace the contents of the template can be deleted, first delete the table to delete the content, and then use unlink ($path) To delete the physical file of HTML.