Home > Article > Backend Development > Classes combined with smarty specially written for novices Page 1/3_PHP Tutorial
A class specially written for novices to be used in conjunction with smarty. We sincerely invite everyone to provide valuable feedback
This is a class written for novices (and for myself) to be used in conjunction with smarty. It is unfinished Yes, the purpose of releasing it now is not to let novices use it immediately, so I haven’t written the annotations in very detail
I hope you experts can give me your opinions and I will try my best to improve it.
First of all, let me state that the purpose of writing this is to train myself. Although I know that there are many similar classes now, I still decided to write one.
So please be gentle when browsing.
I have also packaged and uploaded the files and put them below. Please download them and give your comments. If you have any questions, please ask me directly
Currently, this class includes the following functions (use example, assuming $m = new Machine_m())
[Database]
Currently supports MYSQL and ACCESS databases
Configuration refers to the config.php file
Use: $m->send_query (SQL statement) //
$m->select_query (SQL statement, whether to return resources, the default is false, which returns a two-dimensional array )
[Error handling]
Divided into system errors and user errors
System error:
$this->sys_err( 'Configuration error, please check the config configuration file', 'die ');
The first parameter will record the error information to /lib/error/system.err. The second parameter is the processing method (keep or die). If you need to modify the browser prompt, you can also set it. 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 is closed', 'die' , $_SERVER['HTTP_REFERER'] );
The first parameter is the prompt displayed on the browser, the second parameter is the processing method (keep or die), the third parameter is the jump page, if you need to record If there is an error message, you can also set the fourth parameter, which will record the error message
to /lib/error/user.err. If it is not set, it will not be saved by default.
The error prompt on the browser side calls the err_page.htm template file under /lib/error/ by default. You can also set your own error template file and then load it with $m->err_page=.
[Static generation]
Automatically generate a static page with just one line, and you can set the static page expiration time when jumping
(It is still not completely static, and it is more complicated at present. There is no integration. If you want to achieve complete static, you can combine my create_html function and text operation series functions to achieve it)
Usage:
$m->create_html (template file, static output path, output file name);
Jump:
$m->goto_html();
The output file name defaults to the file name of the current php file. The purpose of providing this parameter is to use static paging when it is necessary. You can use this parameter setting
[Two-dimensional array sorting (recommended)]
to sort the two-dimensional array like: "First ascending order by field a, then descending order by field b"
Usage:
Set an array like this: $x = array( array('name'=>'machine_马', 'age'=>23),array('name'=>'tom ',age=>28),... )
We now need to sort this array in ascending order by name, and then in descending order by age
The usage is 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]
Here I don’t know if I did it well or not. I first wrote a class and then a function to adjust it. The purpose is to make it more convenient to use
Usage method: m_page (number of data items, current page number, each page How many rows, how many jump links to display)
The function returns an array: array(
'rows' => How many rows to display on each page,
'prve' => The page number of the previous page ,//The so-called large page is a jump like the previous 7 pages and the next 7 pages
'next' => The page number of the next large page,
'pages' => How many pages are there in total,
'start' => Number of starting records of SQL query,
'count' => How many records are there,
'links' => Link page number, //If there are 13 pages in total, link The number is 7, and it is currently on the second largest page, then output array(8,9,10,11,12,13)
'current_page' => current page number
);
[Verification Form]
Write the form that needs to be verified into the function class in advance. When judging, you only need to pass in $_POST.
Usage method: You should be able to understand this by looking at the function. This function needs to be modified according to your own needs
[Prevent cross-site attacks]
This function is also written in a function
[Chinese interception function]
It was not written by me, I just modified it
[Upload file]
m_up_file($_FILES, upload path, file type, size limit)
The upload path can be set like this, 1: Write the folder path directly, 2:array('gif'=>'file/gif','jpg'=>'file=>jpg'), so that the gif file is automatically placed in the file/gif folder, jpg Put the file into the file/jpg folder
File type: Writing method 1:'jpg', Writing method 2:array('jpg','jpeg','gif')
Return array( 'arr' => Array of uploaded files, 'err_msg' => Error message during upload, 'num' => Number of successful uploads)
[Text operation (recommended)]
Suppose there is such a character String $str="Hellophpchina";
We can modify it like this $new_str=m_txt_replace('content','machine_马',$str);
Now the value of $new_str is "Hellomachine_马"
Several others Functions such as: m_txt_add, m_txt_delete, m_txt_get are similar, you can refer to them yourself.
Note: This is how to modify the static page after generating it.
You can refer to 6to23 and think about why he posted so many replies in one post and it is so fast
Answer: Because its replies are not entered into the database but written directly into the static file, and then use a method similar to mine above to modify. You can look at his source code and look for