/*************************************/
/* author: older youth
/* email :wenadmin@sina.com
/* from: http://blog.csdn.net/hahawen
/*************************************/
As the "simplest" Web scripting language, php is becoming bigger and bigger in the domestic market, and there are more and more phpers. However, it feels like most people have not considered the mode issue. What design mode is the best? It is the one that is most suitable for your current job. After all, efficiency is the most important (how beautiful it is to use the saved time to play games...). MVC should be the first choice. There are many excellent open source projects based on MVC on www.sourceforge.net. You can rush there to study them.
A few days ago, I revamped my company’s website, which mainly focused on the article publishing system. My boss said that I can design the backend however I want. The only prerequisite is that it be fast. So I built a simple publishing system framework. If we look purely at the article publishing system, it can basically meet the requirements of the article publishing system for "small and medium-sized" enterprise websites. The total PHP code in the background does not exceed 800 lines, and it supports arbitrary expansion and plugin functions.
No more nonsense, let me talk about my structure below, I hope it will be helpful to you.
Note: Before you start, you need to download a template processing tool class: "smarttemplate" and understand the simple use of some templates.
My test environment: windows2k/apache2/php4.3.2/smarttemplate class library
Let’s first talk about the distribution of files in the entire web site. In the following chapters, the following directories and files will be created and filled in.
My server’s web The root directory is "C:/Apache2/htdocs/"
I created a folder "cmstest" below as the main folder of my website
The sub-file structure under the 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 your own company’s website Features and structure of the designed template, summarize the functions to be implemented, and make a list.
Analyze the function list and classify the functions. Each type of function has something in common and can be implemented through the same method.
Design the table structure of the database based on the functions
Design a configuration file config.inc.php to record some basic information of the website, including the database name...
Design database queries for each type of function Interface function, so that similar operations in the future only need to call this interface. This avoids a large number of code duplication operations that may occur in the future, and achieves the purpose of code reuse.
Define your own packaging function for the template tool. When you call it in the future, you don’t have to worry about the use of the template tool. You only need to stuff it into your own packaging function.
The basic functions are now ok, let’s start easy page implementation and template processing.
We will now start to design a simple system to see how I implement the "simplest article publishing system" step by step. Of course, it is just a simple project that I simulated. In reality, a project may be longer than this. Much more complicated.
1. Analysis of my case:
Haha, this customer project is so simple, happy...
list1.php: There are three article lists and one button, "php development article list" "php development hot article list" "asp development latest article" "add new article"
list2.php: There are 2 article lists "asp development article list" "asp development hot article list"
new.php: one to add articles Form page
add.php: Processing page of new.php form
view.php: Article viewing page
2. Analysis function
"php development article list" "asp development article list" ------- According to the publication order of the articles, displayed in reverse order, each page displays 5 articles
"php development hot article list" "asp development hot article list" -------Display articles sorted by the number of clicks and views of the article, displaying 3 Articles
"Latest articles on asp development" are displayed in reverse order according to the publication order of the articles, showing 3 articles
"Add new article"------The publishing function of an article, including article title/author/content
" Article View”---------Display the content of an article
Take a comprehensive look and classify the functions including:
1. Article list: normal paging list, list by number of clicks, list by publishing order
2. Article publishing: input and processing of a form
3. Article viewing: reading and displaying article content
Haha, the function is indeed too simple.
3. Design database:
Database name: cmstest
Data table:
CREATE TABLE `article` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR( 100 ) NOT NULL ,
`content` TEXT 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( 20 ) NOT NULL ,
PRIMARY KEY ( `cid` )
);
-------------------------------- ----------
article table is the article content table,
----------------------------
`id `Article number
`title` Article title
`content` Article content
`datetime` Release time
`clicks` Number of clicks
`pid` Classification number
-------------------------- ---------------
cat table is the category table of articles
-------------------------- --
`cid` Classification table number
`cname` Classification name
----------------------------
The above is the database of the table Structure, these are not enough, but also 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);
This completes the design of our database. Next comes the specific implementation.
4. Design the config.inc.php file
This file is used to set some common data information and some parameters on the web. Other specific implementation pages obtain the required data through this page. The following is a list of configurations
< ?php
//Database settings
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('DB_NAME', ' cmstest');
define('DB_PCONNECT', true);
// Basic path settings for web
define('CMS_ROOT', 'C:/Apache2/htdocs/cmstest/');
define('CMS_SRCPATH', CMS_ROOT.'src/');
//smarttemplate template parsing tool settings
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 basic files to be included contain 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();
?>
where define('CMS_ROOT', 'C:/ Apache2/htdocs /cmstest/'); Change the path according to your own apach web path (refer to the folder structure introduced at the beginning).
5. Making functional interfaces (1)
First, wrap the mysql database function to simplify database operations. There are many such open source classes on the Internet. But here I personally package the mysql function based on my own needs and habits, and I don’t care whether it is good or bad. Just take a quick look at this place. The class operations of different packages are different, and the main purpose here is to understand this "architecture" without being too tight on the 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('数据库连接错误');
mysql_select_db(DB_NAME, $cnn) or die('数据库选择错误');
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语句执行错误');
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? 20: (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语句执行错误');
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){
$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 sqlMakeDelById($ table, $field, $id){
return "DELETE FROM $table WHERE $field=$id";
}
?>
5. Making functional interface (2)
Let’s take a formal look at it. What we need in total Packaging of the implemented functions
----------------ArticleUtil.php----------------
//Display the article list Function
//getArticleList(article category, sorting method, which page is currently displayed, how many articles are displayed on each page)
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 ; return dbGetRow($ sql);
}
//Add article
//addArticle (article content array)
function addArticle($data){
$sql = sqlMakeInsert('article', $data);
return dbExecute($sql);
}
?>
Isn’t this code much simpler? This is the benefit of wrapping the mysql function yourself!
Let’s study how they implement our functions.
"php development article list"--------getArticleList(1, "id DESC", $page, 5)
"asp development article list"--------getArticleList(2, "id DESC", $page, 5)
"php development hot article list"----getArticleList(1, "clicks DESC, id DESC", 1, 3)
"asp development hot article list"----getArticleList( 2, "clicks DESC, id DESC", 1, 3)
"ASP development latest articles"--------getArticleList(2, "id DESC", 1, 3)
"Add new articles"-- -----------addArticle($data)
"View article"---------------getArticle($id)
6. Pack the smarttemplate class ( The revolution has not yet succeeded, comrades still have to work hard)
I won’t go into the specific use of smarttemplate here, otherwise I will run out of words and won’t be able to finish it. The following is a specific 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]);
$page ->assign($key, $value);
Current page 1/5 12345Next page
The above introduces the simplest method of removing freckles. Creating the world's simplest PHP development model, page 1/5, includes the content of the simplest method of removing freckles. I hope it will be helpful to friends who are interested in PHP tutorials.