This article mainly introduces a simple example of generating a static page in php. if you need it, you can refer to publish news to achieve static and static news pages.
Add. php
The code is as follows:
Add News
Config. php
The code is as follows:
Define ("HOST", "localhost ");
Define ("USER", "justfan ");
Define ("PWD", "justfan ");
Define ("DB", "justfanDB ");
Define ("PORT", "3360 ");
?>
DB_class.php
The code is as follows:
Class DB
{
Private $ host = '';
Private $ uname = '';
Private $ pwd = '';
Private $ port = '';
Private $ db = '';
Public static $ instance = null;
Private function _ construct ($ host, $ uname, $ pwd, $ port, $ db)
{
$ This-> host = $ host;
$ This-> uname = $ uname;
$ This-> port = $ port;
$ This-> pwd = $ pwd;
$ This-> db = $ db;
Mysql_connect ($ host, $ uname, $ pwd );
Mysql_select_db ($ this-> db );
}
Public static function Instance ()
{
If (Db: $ instance = null ){
Include 'config. php ';
Return Db: $ instance = new DB (HOST, USER, PWD, PORT, DB );
}
Else
Return Db: $ instance;
}
Public function query ($ SQL)
{
Mysql_query ("set names UTF8 ");
$ Query = mysql_query ($ SQL) or die ($ SQL. "error ");
If (! $ Query) return false;
Else return $ query;
}
Public function getAll ($ SQL)
{
$ Query = $ this-> query ($ SQL );
If ($ query)
{
While ($ ret = mysql_fetch_assoc ($ query ))
{
$ Result [] = $ ret;
}
}
Return $ result;
}
}
?>
Doadd. php
The code is as follows:
Include 'DB _ class. php ';
$ Db = DB: Instance ();
$ Title = $ _ POST ["title"];
$ Content = $ _ POST ["content"];
$ Num = uniqid ();
$ Houzui = ". html ";
$ Filename = date ('ymd'). '/'. $ num. $ houzui;
$ SQL = "insert into news (title, content, path) values ('{$ title}', '{$ content}', '{$ filename }')";
$ Query = $ db-> query ($ SQL );
$ Fp = fopen ("model.htm", "r ");
$ Str = fread ($ fp, filesize ("model.htm "));
$ Str = str_replace ("{title}", $ title, $ str );
$ Str = str_replace ("{content}", $ content, $ str );
Fclose ($ fp );
$ Dir = dirname ($ filename );
If (! Is_dir ($ dir )){
Mkdir ($ dir );
}
$ Handle = fopen ($ filename, "w ");
Fwrite ($ handle, $ str );
Fclose ($ handle );
Echo "view the news just added ";
Echo "add news ";
?>
Model.htm
The code is as follows: