Page 1/3 of the category specifically designed for new handwriting combined with smarty

Source: Internet
Author: User
Tags dsn

We sincerely invite you to give more valuable comments for the new hand-written smarty class.


This is a combination of smarty classes written for beginners (and for their own). It is not complete. The purpose of this Code is not to be immediately used by new users, therefore, the annotations are not detailed.
I hope you can give me more comments and try to improve it.
First of all, I wrote this to train myself. Although I know there are already many similar classes, I decided to write one,
Therefore, please show your mercy during browsing.

I have also packed and uploaded files. Put them below. Please download more files and give more comments. Q me if you have any questions

This class currently includes the following functions (for example, set $ m = new Machine_m ())
[Database]
Currently, MYSQL and ACCESS databases are supported.
For configuration, refer to the config. php file.
Use: $ m-> send_query (SQL statement )//
$ M-> select_query (whether to return resources in an SQL statement. If the default value is false, a two-dimensional array is returned)

[Error handling]
System errors and user errors
System Error:
$ This-> sys_err ('configuration error, please check the config configuration file', 'di ');
The first parameter records the error information to/lib/error/system. in err, the second parameter is the processing method (keep or die). If you need to modify the browser prompt, you can also set the third parameter, which defaults to "sorry, A system error occurred on this site. Please try again later."
User error:
$ M-> user_err ('registration system shut', 'die', $ _ SERVER ['HTTP _ referer']);
The first parameter is the prompt displayed on the browser, the second parameter is the processing method (keep or die), and the third parameter is the jump page. If you need to record the error information, you can also set the fourth parameter to record the error message.
To/lib/error/user. err, It is not saved by default.

The browser prompts that the err_page.htm template file under/lib/error/is called by default. You can also set your own error template file and load it with $ m-> err_page =.

[Static generation]
A static page is automatically generated in just one row. You can set the expiration time of the static page when you jump to the page.
(It is still not completely static, completely complex, and not integrated at present. If you want to implement completely static, you can combine my create_html functions and text operation functions)
Usage:
$ M-> create_html (template file, static output path, output file name );
Jump:
$ M-> goto_html ();
The output file name is equal to the name of the current PHP file by default. this parameter is provided to enable static paging.

[Two-dimensional array sorting (recommended)]
The two-dimensional array can be sorted as follows: "sort by field a in ascending order and then by field B in descending order"
Usage:
There is an array like this: $ x = array ('name' => 'machine _ Map', 'age' => 23 ), array ('name' => 'Tom ', age => 28 ),...... )
Now we want to sort this array in ascending order by name, and then by age in descending order.
M_sort ($ x, 'name', SORT_ASC, 'age', SORT_DESC)

[Dynamic Loading]
For infrequently used functions, I use the loading method. I personally think this can save resources.
For example, if we want to use the m_sort function, this function is not loaded by default.
You need to load it like this: $ m-> load_func ('m _ sort ')
Then you can use the m_sort function.

[Paging]
I don't know whether to do well. I first wrote a class and then wrote a function to call it. The purpose is to make it easier to use.
Usage: m_page (number of data entries, current page number, number of lines per page, and number of redirect links displayed)
The function returns an array: array (
'Rows '=> how many lines are displayed on each page,
'Prve' => the previous page number, // the so-called large page, is a jump similar to the last 7 pages, next 7 pages
'Next' => next page number,
'Pages' => total number of pages,
'Start' => Number of SQL query start records,
'Count' => total number of records,
'Link' => link page number. // if there are 13 pages in total and the number of links is 7, and the current page is the second largest, the output array)
'Current _ page' => current page number
);

[Verification Form]
Write the form to be verified into the function class in advance. You only need to pass $ _ POST into the function class for identification.
Usage: You can see the function as needed.

[Preventing cross-site attacks]
Write this function into a function.

[Chinese truncation function]
I did not write it. I just modified it.

[Upload files]
M_up_file ($ _ FILES, upload path, file type, size limit)
You can set the upload path as follows: 1: Write the folder path directly, 2: array ('gif' => 'file/gif ', 'jpg '=> 'file => jpg'). In this way, the gif file is automatically placed in the file/gif folder, and the jpg file is placed in the file/jpg folder.
File Type: Write 1: 'jpg ', write 2: array ('jpg', 'jpeg ', 'gif ')
Returns array ('arr' => array of uploaded files, 'err _ msg '=> error message during upload, 'num' => Number of uploaded files)

[Text operations (recommended)]
Suppose there is such a string $ str = "Hello <! -- Content --> phpchina <! --/Content --> ";
We can modify $ new_str = m_txt_replace ('content', 'machine _ Mar', $ str );
The value of $ new_str is "Hello <! -- Content --> machine _ horse <! --/Content -->"
Other functions, such as m_txt_add, m_txt_delete, and m_txt_get, are similar.

Note: This is the modification method after the static page is generated.
Refer to 6to23 and think about why he put so many replies on one post at a high speed.
A: Because its reply is not written into the database, it is directly written into the static file, and then modified using methods similar to above. You can look for his source code! -- You will know.


The above functions have been basically implemented.
Next, we plan to do

[Image Processing]
[UBB code output]

And other common functions

I hope you will give more comments.
Main class:
Copy codeThe Code is as follows:

<? Php
Session_cache_limiter ('Private, must-revalidate ');
Session_start ();

If (! Defined ('this _ dir '))
{
Die ('System error: path constant login ');
}
If (@! Include_once (THIS_DIR. 'lib/smarty/Smarty. class. php '))
{
Die ('System error: smarty file loan ');
}

Unset ($ GLOBALS );
Unset ($ HTTP_COOKIE_VARS );
Unset ($ HTTP_ENV_VARS );
Unset ($ HTTP_GET_VARS );
Unset ($ HTTP_POST_FILES );
Unset ($ HTTP_POST_VARS );
Unset ($ HTTP_SERVER_VARS );
Unset ($ HTTP_SESSION_VARS );

Class Machine_m extends Smarty
{
// Database resources
Private $ conn;
// Path
Private $ the_dir;
// Configuration file
Private $ config = array ();
Private $ config_url;
// External function list
Private $ func_list = array ();
Private $ func_list_url;
// Error message page
Public $ err_page = 'lib/error/err_page.htm ';
// Static generation
Public $ html_cache = 'html ';
Public $ html_cache_lifetime = 86400;

// Constructor
Public function _ construct ($ test = false)
{
// Retain the construction part of the smarty class
$ This-> assign ('script _ name', isset ($ _ SERVER ['script _ name'])? $ _ SERVER ['script _ name']
: @ $ GLOBALS ['HTTP _ server_vars'] ['script _ name']);

// It is the construction part of machine_m.
$ This-> left_delimiter = '<% ';
$ This-> right_delimiter = '%> ';
$ This-> the_dir = THIS_DIR;
$ This-> config_url = "{$ this-> the_dir} lib/config. php ";
$ This-> config = $ this-> parse_ini ();
$ This-> func_list_url = "{$ this-> the_dir} lib/config/func_list.php ";
$ This-> func_list = $ this-> parse_func ();
$ This-> state ($ test );
$ This-> load_func (array ('m _ addslashes ','m _ stripslashes '));
$ This-> connect ();
}

// Destructor
Public function _ destruct ()
{
}

// Set the website status Function
Private function state ($ test)
{
If ($ test = true)
{
$ This-> load_func (array ('m _ print_r ','m _ var_dump '));
$ This-> compile_check = true;
Error_reporting (E_ALL );
}
Else
{
$ This-> compile_check = false;
Error_reporting (0 );
}
}

// Parse the configuration file Function
Private function parse_ini ()
{
If (! File_exists ($ this-> config_url ))
{
$ This-> sys_err ("config configuration file loss", 'die ');
}
$ Config = parse_ini_file ($ this-> config_url );
If (! Array_key_exists ('host _ name', $ config)
|! Array_key_exists ('user _ name', $ config)
|! Array_key_exists ('db _ name', $ config)
|! Array_key_exists ('Password', $ config ))
{
$ This-> sys_err ('configuration error, please check the config configuration file', 'di ');
}
$ Config = $ this-> decode_config ($ config );
Settype ($ config, 'object ');
Return $ config;
}

// Parse the function list Function
Private function parse_func ()
{
If (! File_exists ($ this-> func_list_url ))
{
$ This-> sys_err ("missing configuration file func_list", 'die ');
}
$ Func_list = parse_ini_file ($ this-> func_list_url );
Return $ func_list;
}

// External function Loading Function
Public function load_func ($ func)
{
If (is_array ($ func ))
{
Foreach ($ func as $ func_name)
{
$ This-> include_file ($ this-> func_list [$ func_name]);
}
}
Else
{
$ This-> include_file ($ this-> func_list [$ func]);
}
}

// External function inclusion Function
Public function include_file ($ file_url)
{
$ File_url = $ this-> the_dir. $ file_url;
@ $ OK = include_once ($ file_url );
If ($ OK! = True)
{
$ This-> sys_err ("loading failed for file {$ file_url}", 'die ');
}
}

// The config File decoding function (it is not safe to record the database username and password in plaintext. It is best to encrypt and decrypt it here. This function can be reloaded)
Protected function decode_config ($ config)
{
Return $ config;
}

// Connect to the Database Function
Private function connect ()
{
Switch (strtoupper ($ this-> config-> database ))
{
Case 'mysql ':
$ This-> connect_mysql ();
Break;
Case 'access ':
$ This-> connect_access ();
Break;
Default:
$ This-> sys_err ('database type error, this class currently only supports MYSQL and ACCESS databases', 'die ');
Break;
}
}

// Connect to MYSQL database functions
Private function connect_mysql ()
{
If ($ this-> conn! = Null)
{
@ Mysql_close ($ this-> conn );
$ This-> conn = null;
}
@ $ This-> conn = mysql_connect ($ this-> config-> host_name, $ this-> config-> user_name, $ this-> config-> password );
If ($ this-> conn = false)
{
$ Mysql_err = mysql_error ();
$ This-> sys_err ("MYSQL database connection failed because: {{$ mysql_err }}", 'die ');
}
@ $ Db = mysql_select_db ($ this-> config-> db_name, $ this-> conn );
If ($ db = false)
{
$ Mysql_err = mysql_error ();
$ This-> sys_err ("data table connection failed because: {{$ mysql_err }}", 'die ');
}
}

// ACCESS database functions
Private function connect_access ()
{
If ($ this-> conn! = Null)
{
@ Odbc_close ($ this-> conn );
$ This-> conn = null;
}
$ Dsn = 'driver = Microsoft Access Driver (*. mdb); dbq = '. realpath ($ this-> the_dir. $ this-> config-> db_name );
@ $ This-> conn = odbc_connect ($ dsn, $ this-> config-> user_name, $ this-> config-> password );
If ($ this-> conn = false)
{
@ $ Odbc_err = odbc_errormsg ($ this-> conn );
$ This-> sys_err ("ACCESS database connection failed because: {$ odbc_err}", 'die ');
}
}

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.