php use database information to generate static page instance code

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags beginners button clear code content database information date delete

This is a user's own php generated html page code, simple and practical More suitable for beginners, if you are learning php to generate static files, this one instance from the database to the php Template files are very clear step by step Oh.

$ con = mysql tutorial _connect ("localhost", "root", "") or die ("link error !!!");
mysql_select_db ("message", $ con) or die ("database link error !!!");
mysql_query ("set names 'gbk'");
$ sql = mysql_query ("select * from message");

if ($ _ post [button]) {
while ($ row = mysql_fetch_array ($ sql)) {
$ path = $ row [0]. '. html';
$ user = $ row [1];
$ title = $ row [2];
$ content = $ row [3];
$ date = $ row [4];

$ fp = fopen ("moban / html.html", "r");
$ str = fread ($ fp, filesize ("./ moban / html.html"));
$ str = str_replace ("{user}", $ user, $ str);
$ str = str_replace ("{title}", $ title, $ str);
$ str = str_replace ("{content}", $ content, $ str);
$ str = str_replace ("{date}", $ date, $ str);
fclose ($ fp);

$ handle = fopen ("news /".$ path," w ");
fwrite ($ handle, $ str);
fclose ($ handle);
echo "<script> alert ('generate success !!!'); window.location.href = 'html.php'; </ script>";
}
}
?>
<! doctype html public "- // w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "content-type" content = "text / html; charset = gb2312" />
<title> Untitled Document </ title>
</ head>

<body>
<form id = "form1" name = "form1" method = "post" action = "">
<label>
<input type = "submit" name = "button" id = "button" value = "Generate Static Pages" />
</ label>
</ form>
</ body>
</ html>
Template file code

<! doctype html public "- // w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns = "http://www.jzread.com/1999/xhtml">
<head>
<meta http-equiv = "content-type" content = "text / html; charset = gb2312" />
<title> Untitled Document </ title>
</ head>

<body>
<table width = "500">
<tr>
<td width = "80"> <div align = "right"> Name: </ div> </ td>
<td width = "408"> <div align = "left"> {user} </ div> </ td>
</ tr>
<tr>
<td> <div align = "right"> Title: </ div> </ td>
<td> <div align = "left"> {title} </ div> </ td>
</ tr>
<tr>
<td> <div align = "right"> content: </ div> </ td>
<td> <div align = "left"> {content} </ div> </ td>
</ tr>
<tr>
<td> <div align = "right"> Date: </ div> </ td>
<td> <div align = "left"> {date} </ div> </ td>
</ tr>
<tr>
<td> <div align = "right"> </ div> </ td>
<td> <div align = "left"> </ div> </ td>
</ tr>
</ table>
</ body>
Remove html code

<? php
$ con = mysql_connect ("localhost", "root", "") or die ("link error !!!");
mysql_select_db ("message", $ con) or die ("database link error !!!");
mysql_query ("set names 'gbk'");
$ sql = mysql_query ("select * from message");

if ($ _ post [button]) {
while ($ row = mysql_fetch_array ($ sql)) {
$ path = $ row [0]. '. html';


if (@unlink ("news /".$ path)) {
echo "<script> alert ('delete successfully !!!'); window.location.href = 'unlink_html.php'; </ script>";
} else {
echo "<script> alert ('no content can be removed !!!'); window.location.href = 'unlink_html.php'; </ script>";
}
}
}
?>
<form id = "form1" name = "form1" method = "post" action = "">
<label>
<input type = "submit" name = "button" id = "button" value = "Delete Static Page" />
</ label>
</ form>
// Database structure, using phpmyadmin import on the list

The structure of the table `message`
-

create table `message` (
`id` int (4) not null auto_increment,
`user` varchar (20) not null,
`title` varchar (100) not null,
`content` text not null,
`date` date not null,
primary key (`id`)
) engine = myisam default charset = gbk auto_increment = 3;

-
- Export the data `message` in the table
-

insert into `message` (` id`, `user`,` title`, `content`,` date`) values
(1, 'dfkk', 'database information to generate static pages', 'www.jzread.com', '2010-02-12'),
(2, 'dfkk', 'database information to generate static pages', 'the principle is very simple', '2010-02-12');


Write your own, through the button to generate static pages and delete generated static pages.

moban save the template folder (do not delete).

news generate static pages folder (not deleted).


Note: phpmyadmin tool to import the database file message.sql.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.