Create the easiest in the world ------ PHP development model

xiaoxiao2021-03-06  49

/ *********************************** / * AUTHOR: Uld Youth / * Email: wnadmin@sina.com/* from: http: // Blog.9cbs.net/hahawen/***************

Original code download: http://club.phpe.net/index.php?s==at=attach&type=post&id=29432

(Recently, it is a problem that everyone has made a problem. It is a very confusion that uses PHP development design model, so write down this article, I hope to help everyone) PHP as "the easiest" web scripting language, The domestic market is getting bigger and bigger, and there are more and more phper. Important (play the play, more beautiful ...). MVC should be preferred, there is a lot of excellent MVC-based open source projects on www.sourceforge.net. You can rush to research research.

A few days ago, give your company website revision, mainly the article release system, the boss said that the background I want to design how to design, the only premise is fast. So I built a simple distribution system framework. If you are purely speaking on the article publishing system, you can basically meet the requirements of the article publishing system of the "Small and Medium" enterprise website. The total PHP code in the background does not exceed 800 rows, and supports any expansion and Plugin features.

Nances are no longer said, let's talk about my architecture, I hope to help you.

Note: Before starting, you need to download a template processing tool class: "smartTemplate" and learn from some templates.

My test environment: windows2k / apache2 / php4.3.2 / smartTemplate class library

Let's talk about the distribution of the entire Web site file. In the later chapter, it will create and populate the following directory and file My server's web root directory is "c: / apache2 / htdocs /" I have built one below. Folder "CMSTEST" as the subfile structure below the main folder folder "cmstest" in my website 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 smartTemplate's 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: 1. Consider the characteristics of your company's website and the structure of the template that has been designed, summarize the functions to be implemented, list the list.

2. Analyze the function list and classify the function. Each type of functionality is common, and can be implemented by the same method.

3, according to the function, design the table structure of the database

4, design a configuration file config.inc.php, used to record some basic information about the website, including the database name ........

5. Design the interface function of the database query for each class function, so similar operations can be called as long as this interface is called. This avoids a large number of code repetitions that may occur later, and the purpose of the code multiplexing is reached.

6. Define yourself to the package function of the template tool. When you call it, you don't have to use the problem of the template tool. Only the number of stuffers in your packaging function can be.

7. The base function has OK, starting a relaxed page implementation and template processing.

We now start designing a simple system to see how I realize a "simplest article release system" step by step, of course just a simple project I simulate, and one project may be complicated than this. Most.

First, analyze my case:

Oh, this customer project is simple, happiness ing ..........

List1.php: There are three articles list and a button, "PHP development article list" "PHP development hot article list" "ASP development latest article" "Add new article" list2.php: There are 2 articles list "ASP development articles list "" ASP Development Hot Articles List "new.php: A page for adding a form of a form Add.php: Processing New.PHP's form View.php: Article viewed page

Second, the analysis function

"PHP Development Article List" "ASP Development Article List" ------- According to the release order of the article, reverse order display, each page shows 5 articles "PHP Development Hot Articles List" "ASP Development Hot Articles List" - ------ According to the click of the articles, the number of candles appear to display the article, showing 3 articles "The latest article" in the post, reverse order display, display 3 articles "Add a new article" ----- - An article publishing function, including article title / authorship "article" --------- Show an article content

Take a synthesize, classify the functionality includes: 1. Article list: Normal Paging list, press the list of clicks 2, the article is released: a form of input and processing 3, the article looks: read the display Article content

Oh, the function is indeed too simple.

Third, design the database:

Database name: cmstest

data sheet:

Create Table `Article` (` ID` Int not null auto_increment, `Title` Varchar (100) Not null,` Content` TEXT NOT NULL, `datetime` Datetime Not Null,` Click` int (11), `pid` Tinyint (2) NOT NULL, PRIMARY Key (`ID`);

Create Table` Cat` (`Cid` Tinyint (2) Not null,` cname` Varchar (20) Not null, primary key (`CID`);

----------------------------- Article Table is the content table of the article, ------------- --------------- `Id` 编 number` title` article content` to 发 时间 `ClickS` 点` ``` -------------------------- CAT table is the category of the article ----------------- ----------- `CID` classification number` CNAME` Category Name -----------------------------------------------------------------------------------------------------------------------------------------------------------

The above is the database structure. It is not enough to have these, and there must be 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 ​​(10," JSP Development 3 "," JSP Development 3 Content "," 2004-8-3 1: 1: 1 ", 4, 2); Insert Into` Article` VALUES (11 , "JSP Development 4", "JSP Development 4 Content", "2004-8-4 1: 1: 1", 3, 2); Insert Into `Article` VALUES (12," JSP Development 5 "," JSP Development 5 content "," 2004-8-5 1: 1: 1 ", 2, 2); Insert Into` Article` Values ​​(13, "JSP Development 6", "JSP Development 6 Content", "2004-8-6 1: 1: 1", 1, 2 INSERT INTO `Article` VALUES (14," JSP Development 7 "," JSP Development 7 Content "," 2004-8-7 1: 1: 1 ", 0, 2); so our database is designed. Next, it began to involve specific implementation.

Fourth, design config.inc.php file

This file is used to set some data information and some parameters on the web. Other specific implementations are obtained by this page, and the following is the configuration list // database settings define ('db_username', 'root') Define ('db_password', ''); define ('db_host', 'localhost'); define ('db_name', 'cmstest'); define ('db_pconnect', true); // Web basic road setting Define ('cms_root', 'c: / apache2 / htdocs / cmstest /'); define ('cms_srcpath', cms_root.'src / '); // SmartTemplate Template Analytical Tool Setting 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 '); // file to include the base, there are some 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 ();?> which define (' CMS_ROOT ', 'C: / apache2 / htdocs / cmstest /'); the road is changed according to its own APACH WEB channel (refer to the place to change the folder structure). V. Production function interface (1) First pack the mysql database function, simplify the database operation, there are many open source classes on the Internet. But here my personal packaging according to my own needs and habits, I have been packaging my Mysql's function, and I don't care if I write it. This place is simple to see, and the class operation of different packaging is different, and the main purpose here is to understand this "architecture", no need 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)) ordie ( 'database connection errors'); 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; $ = mysql_query ($ SQL) OR DIE ('sql statement execution error'); if (MySQL_NUM_ROWS ($ RS)> 0) RETURN MySQL_FETCH_ASSOC ($ RS); Elsereturn Null;} Function DBGETONE ($ SQL, $ FILDNAME) {$ r = DBGETROW ($ SQL); Return Sizeof ($ r) == Null? null : (Isset ($ RS [$ FILDNAME])? $ r [= 20) {IF ($ Page === Null) Return DBQuery ($ SQL); $ countsql = preg_replace ('| select. * from | I', 'SELECT Count (*) Count from', $ SQL); $ n = (int) DBGETOENE ($ Countsql, 'Count') ; $ data ['PageSize'] = (int) $ pageize <1? 20: (int) $ pagesize; $ data ['recordcount'] = $ n; $ data ['PageCount'] = CEIL ($ data [" Record Count '] / $ data [' Pagesize ']); $ DATA [' Page '] = $ DATA [' PageCount '] == 0? 0: ((int) $ Page <1: (int) $ Page $ Data ['Page'] = $ DATA ['Page']? $ data ['PageCount']: $ data ['Page']; $ data ['isfirst'] = $ Data ['Page']> 1? false: true; $ data ['islast'] = $ data ['Page']? 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 WHER $ FIELD = $ ID ";} Function Sqlmakeinsert ($ TABLE, $ DATA) {$ T1 = $ T2 = Array (); Foreach ($ DATA AS $ Key => $ VALUE) {$ T1 [ ] = $ key; $ t2 [] = "'" .addslashes ($ value). "'";} return "Insert Into $ TABLE (". IMPLODE (",", $ T1). ") VALUES.". Implode (",", $ T2). ")";} Function SQLMakeUpdateByid ($ TABLE, $ FIELD, $ ID, $ DATA) {$ T1 = Array (); Foreach ($ DATA AS $ Key => $ VALUE) {$ T1 [] = "$ key = '". Addslashes ($ value). "'";} Return "Update $ TABLE SET". IMPLODE (",", $ t1). "Where $ field = ID" } Function SQLMAKEDEDELBYID ($ TABLE, $ FIELD, $ ID) {RETURN "DELETE FROM $ TABLE WHERE $ FIELD = ID";}?> 5. Production function interface (2) Let's officially see, our copies of the functionality of the functional packaging -------- ---- ARTICLEUTIL.PHP -----------------

// Display the function of the article // GetArticleList (Article Category, Sorting Method, Currently Show a few pages) Function GetArticleList ($ CATID, $ ORDER, $ PAGE, $ PAGESIZE) {$ SQL = "SELECT * From article where pid = $ catid ORDER BY $ ORDER "; RETURN DBPAGEQUERY ($ SQL, $ PAGE, $ PAGESIZE);} // Query 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 article // AddAndicle (Article content array) Function addArticle ($ data) {$ SQL = SQLMAKEINSERT ('Article', $ DATA); RETURN DBEXECUTE ($ SQL);}?> This code is not simple. ? This is the benefit of packaging my mysql function! Let's study how they do our function. "PHP development article list" -------- GetArticlelist (1, "ID DESC", $ PAGE, 5) "ASP Development Article List" -------- GetArticleList (2, "ID DESC" , $ PAGE, 5) "PHP Development Hot Articles List" ---- GetArticleList (1, "ClickS DESC, ID DESC", 1, 3) "ASP Development Hot Point List" ---- GetArticleList (2, "ClickS DESC, ID DESC ", 1, 3)" ASP Development Latest Articles "-------- GetArticleList (2," ID DESC ", 1, 3)" Add a new article "-------- ----- ADDATICLE ($ DATA) "See Article" -------------- GetArticle ($ ID) 6. Packaging the SmartTemplate class (revolution has not been successful, comrades must work hard ) The specific SmartTemplate is not talking here. Otherwise, the water is not talking, it can't finish it. This is the specific pair of packaging functions ------------- Parsetpl.php ----------------

Function RendertPL ($ ViewFile, $ DATA) {$ ViewFile; Foreach ($ DATA AS $ Key => $ VALUE) {ix (isset ($ value [data)) {$ page-> assign ($ KEY, $ Value [Data); unset ($ Value [data]); $ page-> assign ($ key. "_ page", $ value);} else {$ page-> assign ($ key, $ Value);}} $ page-> OUTPUT ();}?> 7: Article list looks at the page implementation and template processing (the last step of the long language) first look at the implementation of the page list1, in List1 panements use a Page .js file, this file is a JS pagination that writes himself, it is very easy to use -------------------------------------------------------------------------------------------------------------------------- ----- // -------- Total 20 records, current 86/99 page [1] ... [82] [83] [84] [85] [86] [87] [ 88] [89] [90] ... [99] GO ------------------- // RecordCount = 20; // show = 20 // Pageshow = 11 ; // pageCount = 100; // pageNow = 86; "? page = _page _" // pageStr =; // document.write (showListPage (recordCount, show, pageCount, pageNow, pageStr)); function showListPage (recordCount, show , PageShow, PageStr) {IF (PageCount <1) PageCount = 0; IF (PageNow <1) Pagenow = 0; str = 'total' recordcount 'record, current' Pagenow '/' PageCount '; If (pagecount <= pageshow) {starthave = false; endhave = false; startnum = 1; ELSE I PageCount;} else i f (Pagenow-1 <= Pageshow / 2) {starthave = false; endhave = true; startnum = 1; ENDNUM = Pageshow-1;} else}} {starthave = true; endhave = false; startNum = pageCount - pageShow 2; endNum = pageCount;} else {startHave = true; endHave = true; startNum = pageNow - Math.floor ((pageShow-2) / 2); endNum = startNum pageShow - 3; } IF (starthave) {startstr = "[1] ..."; str = startstr;} for (i = startnum; i <= endnum; i

) {if (Pagenow == i) STR = "[" i "]"; ElseStr = "[" i "]";} if (endhave) {endstr = "... [" PageCount "]"; Str = endstr;} Return Str;} -------------------------------------------------------------------------------------------------------------------------------- Add a new article PHP development article {id} - {Title}