Creating the world's simplest PHP development model 1th/5 page _php Tutorial

Source: Internet
Author: User
/*************************************/
/* Author: older Youth
/* email:wenadmin@sina.com
/* From:http://blog.csdn.net/hahawen
/*************************************/

PHP as the "simplest" Web scripting language, in the domestic market more and more, phper more and more, but feel that most people do not seem to consider the pattern problem, what kind of design mode is the best, is the most suitable for their current work, after all, efficiency is the most important (save time to play games, how beautiful AH ... )。 MVC should be the first choice, there are a lot of good MVC-based open source projects on www.sourceforge.net, and you can go through the research.

A few days ago to their company's website revision, mainly the article release system, the boss said backstage I want to design how to design, the only premise is fast. The framework of a simple release system was built. If simply from the article publishing system, basically can meet the "small and Medium" enterprise website article publishing system requirements, the total PHP code in the background no more than 800 lines, and support arbitrary expansion and plugin functions.

No more nonsense, let's talk about my architecture, I hope you can help.

Note: Before you start, you need to download a template processing tool class: "Smarttemplate" and learn some simple uses of templates.

My test environment: Windows2k/apache2/php4.3.2/smarttemplate class Library

Let's start with the distribution of the files for the entire Web site, which will be created and populated with the following directories and files in a later section
The root of my server's Web is "c:/apache2/htdocs/"
I set up a folder "Cmstest" as the home folder of my site below
The sub-file structure under folder "Cmstest" is:

/config.inc.php
/list1.php
/list2.php
/new.php
/add.php
/view.php
/page.js
/src/mysqlutil.php
/src/articleutil.php
/src/coreutil.php
/src/parsetpl.php
/src/lib/smarttemplate/*.* This directory is used to store the Smarttemplate class library
/smart/template/list1.htm
/smart/template/list2.htm
/smart/template/new.htm
/smart/template/add.htm
/smart/template/view.htm
/smart/cache/
/smart/temp/


Design steps:

Consider the features of your company's website and the structure of the templates that have been designed, summarize the features to be implemented, and list the listings.
Analyze the function list and classify the functions. Each type of function has a common denominator and can be implemented in the same way.
Design the table structure of the database according to the function
Design a profile, config.inc.php, to record some basic information about the site, including the database name ....
The interface functions for database queries are designed for each type of function, so that similar operations can be done as soon as the interface is called. This avoids a large number of code duplication operations that may occur later, and it achieves the purpose of code reuse.
Define your own wrapper function for the template tool, and later call it without the use of the template tool, only to their own packing function inside the plug number on it.
The basic function is OK, and the easy implementation of the page and the processing of the template are started.

Now we're going to start designing a simple system to see how I'm going to implement a "simplest article release system" step-by-stage, and of course just a simple project that I'm simulating, in which a project might be more complex than that.


First, analysis of my case:

Oh, this customer project is very simple ah, happy ing ...

List1.php: There are three article lists and a button, "PHP Development article List" "PHP Development Hotspot Article List" "ASP Development Latest article" "Add new article"
List2.php: There are 2 articles in the list "ASP Development article List" "ASP Development Hotspot article List"
new.php: A page that adds a form to an article
add.php: A page that processes new.php forms
view.php: Article View of the page

Second, analysis function

"PHP Development article List" "ASP Development article List"-------in the order in which the articles are published, sorted in reverse, showing 5 articles per page
"PHP Development Hotspot Article List" "ASP Development Hotspot article List"-------sorted by the number of hits in the article, showing 3 articles
"ASP Development Latest article" according to the article in the order of publication, reverse display, showing 3 articles
"Add new article"------The publishing feature of an article, including the article title/author/content
"Article View"---------show the content of an article

In a comprehensive look, the classification of functions includes:
1. List of articles: normal page list, click-to-Count list, list by publish order
2. Article release: input and processing of a form
3, article view: Read the contents of the article

Oh, the function is indeed too simple.

Third, the design database:

Database name: cmstest

Data sheet:

CREATE TABLE ' article ' (
' ID ' INT not NULL auto_increment,
' title ' VARCHAR (+) not NULL,
' Content ' TEXT is not NULL,
' DateTime ' datetime not NULL,
' Clicks ' INT (11),
' PID ' TINYINT (2) Not NULL,
PRIMARY KEY (' id ')
);

CREATE TABLE ' Cat ' (
' CID ' TINYINT (2) Not NULL,
' CNAME ' VARCHAR (not NULL),
PRIMARY KEY (' CID ')
);

------------------------------
Article table is an article content table,
----------------------------
' ID ' article number
' title ' article title
' Content ' articles
' DateTime ' release time
' clicks ' Number of hits
' PID ' classification table number
------------------------------
Cat table is the category table of the article
----------------------------
' CID ' classification table number
' CNAME ' category name
----------------------------

Above is the database structure of the table, the light has not enough, but also to have data
INSERT into ' Cat ' VALUES (1, "PHP Development"), (2, "ASP development");
INSERT into ' article ' VALUES (1, "PHP Development 1", "PHP Development 1 content", "2004-8-1 1:1:1", 0, 1);
INSERT into ' article ' VALUES (2, "PHP Development 2", "PHP Development 2 Content", "2004-8-2 1:1:1", 0, 1);
INSERT into ' article ' VALUES (3, "PHP Development 3", "PHP Development 3 content", "2004-8-3 1:1:1", 4, 1);
INSERT into ' article ' VALUES (4, "PHP Development 4", "PHP Development 4 Content", "2004-8-4 1:1:1", 3, 1);
INSERT into ' article ' VALUES (5, "PHP Development 5", "PHP Development 5 content", "2004-8-5 1:1:1", 2, 1);
INSERT into ' article ' VALUES (6, "PHP Development 6", "PHP Development 6 content", "2004-8-6 1:1:1", 1, 1);
INSERT into ' article ' VALUES (7, "PHP Development 7", "PHP Development 7 Content", "2004-8-7 1:1:1", 0, 1);
INSERT into ' article ' VALUES (8, "JSP Development 1", "JSP Development 1 Content", "2004-8-1 1:1:1", 0, 2);
INSERT into ' article ' VALUES (9, "JSP Development 2", "JSP Development 2 Content", "2004-8-2 1:1:1", 0, 2);
INSERT into ' article ' VALUES ("JSP Development 3", "JSP development 3 Content", "2004-8-3 1:1:1", 4, 2);
INSERT into ' article ' VALUES (one, "JSP development 4", "JSP development 4 Content", "2004-8-4 1:1:1", 3, 2);
INSERT into ' article ' VALUES ("JSP Development 5", "JSP development 5 content", "2004-8-5 1:1:1", 2, 2);
INSERT into ' article ' VALUES ("JSP Development 6", "JSP development 6 Content", "2004-8-6 1:1:1", 1, 2);
INSERT into ' article ' VALUES ("JSP Development 7", "JSP development 7 Content", "2004-8-7 1:1:1", 0, 2);


So our database is finished. The next step is to come up with concrete implementations.

Iv. design of the config.inc.php file

This file is used to set some common web data information and some parameters, the other specific implementation of the page to get the required data through this page, the following is a list of the configuration


Database Settings
Define (' Db_username ', ' root ');
Define (' Db_password ', ');
Define (' db_host ', ' localhost ');
Define (' db_name ', ' cmstest ');
Define (' Db_pconnect ', true);

The basic path of the web is set
Define (' Cms_root ', ' c:/apache2/htdocs/cmstest/');
Define (' Cms_srcpath ', cms_root. ' src/');

Smarttemplate settings for template parsing tools
Define (' Smart_reuse_code ', false);
Define (' Smart_template_dir ', cms_root. ' smart/template/');
Define (' Smart_temp_dir ', cms_root. ' smart/temp/');
Define (' Smart_cache_dir ', cms_root. ' smart/cache/');
Define (' Smart_cache_lifetime ', 100);
Require_once (Cms_srcpath. ' lib/smarttemplate/class.smarttemplate.php ');

The underlying files to include are some of the basic functions
Require_once cms_srcpath. ' mysqlutil.php ';
Require_once cms_srcpath. ' articleutil.php ';
Require_once cms_srcpath. ' coreutil.php ';
Require_once cms_srcpath. ' parsetpl.php ';

Session control
Session_cache_limiter (' Private_no_expire ');
Session_Start ();

?>


One of the define (' Cms_root ', ' c:/apache2/htdocs/cmstest/'); The road is based on its own apach of the Web path to change (referring to the first introduction to the folder structure of the place to change).


Five, the Production function interface (1)

First, the MySQL database functions are packaged to simplify the operation of the database, there are many such open-source classes on the Web. But here I personally according to their own needs and habits, their own functions of MySQL packaging, write good or bad first. This place is simple to look at, the different packaging of the class operation is different, and the main purpose here is to understand this set of "architecture", do not have to buckle code.

-------mysqlutil.php--------

function Dbconnect () {
Global $cnn;
$CNN = (Db_pconnect mysql_pconnect (Db_host, Db_name, Db_password):
Mysql_connect (Db_host, Db_name, Db_password)) or
Die (' Database connection error ');
mysql_select_db (db_name, $cnn) or Die (' Database selection error ');
mysql_query ("SET autocommit=1");
}

function &dbquery ($sql) {
Global $cnn;
$rs = &mysql_query ($sql, $CNN);
while ($item =mysql_fetch_assoc ($rs)) {
$data [] = $item;
}
return $data;
}

function &dbgetrow ($sql) {
Global $cnn;
$rs = mysql_query ($sql) or Die (' SQL statement execution error ');
if (mysql_num_rows ($rs) >0)
Return Mysql_fetch_assoc ($RS);
Else
return null;
}

function Dbgetone ($sql, $fildName) {
$rs = Dbgetrow ($sql);
return sizeof ($RS) ==null? Null: (Isset ($rs [$fildName])? $rs [$fildName]: null);
}

function &dbpagequery ($sql, $page =1, $pageSize =20) {
if ($page ===null) return dbquery ($sql);
$COUNTSQL = preg_replace (' | Select.*from|i ', ' SELECT count (*) Count from ', $sql);
$n = (int) dbgetone ($countSql, ' count ');
$data [' pageSize '] = (int) $pageSize <1? : (int) $pageSize;
$data [' recordCount '] = $n;
$data [' pagecount '] = ceil ($data [' RecordCount ']/$data [' pageSize ']);
$data [' page '] = $data [' PageCount ']==0? 0: (int) $page <1 1: (int) $page);
$data [' page '] = $data [' page ']> $data [' PageCount ']? $data [' PageCount ']: $data [' page '];
$data [' isFirst '] = $data [' Page ']>1? False:true;
$data [' islast '] = $data [' page ']< $data [' PageCount ']? False:true;
$data [' start '] = ($data [' page ']==0)? 0: ($data [' page ']-1] * $data [' pageSize ']+1;
$data [' end '] = ($data [' Start ']+ $data [' pageSize ']-1);
$data [' end '] = $data [' End ']> $data [' RecordCount ']? $data [' RecordCount ']: $data [' End '];
$data [' sql '] = $sql. ' LIMIT '. ($data [' Start ']-1). ', '. $data [' pageSize '];
$data [' data '] = &dbquery ($data [' SQL ']);
return $data;
}

function Dbexecute ($sql) {
Global $cnn;
mysql_query ($sql, $cnn) or Die (' SQL statement execution error ');
Return Mysql_affected_rows ($CNN);
}

function Dbdisconnect () {
Global $cnn;
Mysql_close ($CNN);
}

function Sqlgetonebyid ($table, $field, $id) {
Return "SELECT * from $table WHERE $field = $id";
}

function Sqlmakeinsert ($table, $data) {
$t 1 = $t 2 = array ();
foreach ($data as $key = = $value) {
$t 1[] = $key;
$t 2[] = "'". Addslashes ($value). "'";
}
Return "INSERT into $table (". Implode (",", $t 1). ") VALUES (". Implode (", ", $t 2).") ";
}

function Sqlmakeupdatebyid ($table, $field, $id, $data) {
$t 1 = array ();
foreach ($data as $key = = $value) {
$t 1[] = "$key = '". Addslashes ($value). "'";
}
Return "UPDATE $table SET". Implode (",", $t 1). " WHERE $field = $id ";
}

function Sqlmakedelbyid ($table, $field, $id) {
Return "DELETE from $table WHERE $field = $id";
}

?>

Five, the Production function interface (2)

Below to take a look formally, we have to implement the function of packaging

------------articleutil.php----------------
Functions that display the list of articles
Getarticlelist (article category, sorting method, currently showing the first few pages, each page shows a few)
function Getarticlelist ($catId, $order, $page, $pageSize) {
$sql = "SELECT * from article WHERE pid= $catId ORDER by $order";
Return Dbpagequery ($sql, $page, $pageSize);
}
Querying the content of an article
GetArticle (article number)
function GetArticle ($id) {
$sqlUpdate = "UPDATE article SET clicks=clicks+1 WHERE id= $id";
Dbexecute ($sqlUpdate);
$sql = "SELECT * from article WHERE art_id= $id";
Return dbgetrow ($sql);
}
Add an article
Addarticle (article content Array)
function Addarticle ($data) {
$sql = Sqlmakeinsert (' article ', $data);
Return Dbexecute ($sql);
}
?>

Is this code a lot easier? This is the benefit of packaging your MySQL function!
Let's look at how they do our functions.
"PHP Development article List"--------Getarticlelist (1, "id DESC", $page, 5)
"ASP Development article List"--------Getarticlelist (2, "id DESC", $page, 5)
"PHP Development Hotspot article List"----getarticlelist (1, "clicks Desc, id desc", 1, 3)
"ASP Development Hotspot article List"----getarticlelist (2, "clicks Desc, id desc", 1, 3)
"ASP Development Latest article"--------Getarticlelist (2, "ID DESC", 1, 3)
"Add new article"-------------addarticle ($data)
"View article"---------------getArticle ($id)


Vi. packaging for the Smarttemplate class (the revolution has not been successful, comrades still have to work)

The use of specific smarttemplate here will not speak, or Spit said no, are not finished. The following is specific to the wrapper function

-------------parsetpl.php----------------

function Rendertpl ($viewFile, $data) {
$page = new Smarttemplate ($viewFile);
foreach ($data as $key = = $value) {
if (Isset ($value [data])) {
$page->assign ($key, $value [data]);
unset ($value [data]);
$page->assign ($key. " _page ", $value);
} else {
$page->assign ($key, $value);
}
}
$page->output ();
}

?>

http://www.bkjia.com/PHPjc/317380.html www.bkjia.com true http://www.bkjia.com/PHPjc/317380.html techarticle /*************************************//*author: older youth/*email:wenadmin@sina.com/*from:http://blog.csdn.net/ Hahawen/*************************************/php as "the simplest ...

  • 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.