Simple example of generating static pages using php _ php instance

Source: Internet
Author: User
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:






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.