Home Backend Development PHP Tutorial The simplest method to remove freckles and create the world's simplest PHP development model Page 1/5

The simplest method to remove freckles and create the world's simplest PHP development model Page 1/5

Jul 29, 2016 am 08:35 AM

/*************************************/
/* 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.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles