Codeigniter framework-Function Analysis + small knowledge points

Source: Internet
Author: User
Tags echo date custom name

Connect to the database:

Format: mysql-H host address-u user name-P User Password

Database prompt: mysql>

Exit database:

Exit (Press ENTER)

 

Knowledge Point accumulation:

 

1. date_default_timezone_set-sets the default time zone used for all date and time functions in a script.

Format: bool date_default_timezone_set (string$timezone_identifier)

Parameters:Timezone_identifier, the time zone identifier, for exampleUTCOrEurope/Lisbon. For the list of valid identifiers, see the supported Time Zone List.

Return Value: Iftimezone_identifierIf the parameter is invalid, returnFALSEOtherwise, returnTRUE.

 

2. date_default_timezone_get-gets the default time zone used by all the date and time functions in a script.

Format: String date_default_timezone_get (void)

Return Value: returns a string.

 

3. Time-returns the current UNIX Timestamp

Format: int time (void)

Returned value: returns the number of seconds from the Unix epoch (GMT 00:00:00, January 1, January 1, 1970) to the current time.

 

4. Date-format a local time/date 

Format: String date (string$format[, Int$timestamp])

Parameter: format-required. The format of the specified timestamp.

Timestamp-optional. The specified timestamp. The default value is the current time and date.

Note: a timestamp is a string that represents the date and event of a specific event.

The following are some commonly used date characters: D-represents the day of the month (01-31) m-represents the month (01-12) Y-represents the year (four digits) 1-other characters in the day of the week, such "/",". "or"-"can also be inserted into characters to add other formats. The following example uses three different formats: <? Phpecho "today is ". date ("Y/M/D "). "<br>"; echo "today is ". date ("Y. m. d "). "<br>"; echo "today is ". date ("Y-m-d "). "<br>"; echo "today is ". date ("L");?>
The following are commonly used time characters: H-with the first zero 12-hour format I-with the first zero minute S-with the first zero second (00-59) the following example of a-lowercase midday and afternoon (AM or PM) outputs the current time in the specified format: <? Phpecho "the current time is". date ("H: I: SA");?>
Create a date using PHP mktime ()The specified timestamp is an optional timestamp parameter in the date () function. If you do not specify a timestamp, the current date and time will be used (as in the previous example ). The mktime () function returns the Unix timestamp of the date. The UNIX timestamp contains the number of seconds between the Unix epoch (January 1, 1970 00:00:00 GMT) and the specified time. Syntax: mktime (hour, minute, second, month, day, year)
Use PHP strtotime() Use a string to create a dateThe PHP strtotime () function is used to convert a human-readable string to a Unix time. Syntax: strtotime (time, now) instance: <? PHP $ d = strtotime ("tomorrow"); echo date ("Y-m-d h: I: SA", $ D ). "<br>"; $ d = strtotime ("next Saturday"); echo date ("Y-m-d h: I: SA", $ D ). "<br>"; $ d = strtotime ("+ 3 months"); echo date ("Y-m-d h: I: SA", $ D ). "<br>";?>

 

5. Use post, cookie, and server data

Cross-Site Script Filtering: $ DATA = $ this-> input-> xss_clean ($ data );

Note: This function only processes submitted data. It should not be used in other cases because it consumes a lot of CPU resources.

Codeigniter has three helper methods for users to obtain post, Cookie, or server content, as shown below:

(1) $ this-> input-> post () Format: $ this-> input-> post ('some _ data', true); parameter: the first parameter is the data in the post to be obtained. If the data does not exist, the method returns false (Boolean ). The second parameter is optional. If you want to filter the acquired data through XSS filtering, set the second parameter to true. Extension: $ this-> input-> get ('some _ data', true); used to obtain get data. $ This-> input-> get_post ('some _ data', true); search for post data and get data first. (2) $ this-> input-> cookie () (3) $ this-> input-> server ()

 

6. Loading class

Load, as the name implies, is used to load elements. These elements can be Library (class) view files, auxiliary functions, models, or your own files.

 (1) $ this-> load-> vars ($ array );

Note: This function uses an associated array as the input parameter and converts the array into a variable corresponding to this array using the PHP extract function. You may need to use this function independently because you want to set some global variables in the Controller constructor so that these variables can be used in the View of any function call. You can call this function multiple times. Array data is cached and incorporated into an array for conversion to variables.

 (2) $ this-> load-> helper ('file _ name ');

Note: This function is used to load the auxiliary function. file_name is the file name corresponding to the auxiliary function, not including_ Helper. phpExtension.

(3) $ this-> load-> Library ('class _ name', $ config, 'object name ');

Note: This function is used to load core classes.

Class_name is the name of the class to be loaded. Tip: "class" and "library" can be replaced.

For example, if you want to use codeigniter to send emails, the first step is to load the email class in your controller. $ This-> load-> Library ('email '); this class can be used once loaded. use $ this-> email-> some_function ().

The second parameter is optional, which allows you to selectively pass configuration parameters. In general, you can pass parameters in the form of arrays. Configuration parameters can also be stored in a configuration file.

$config = array (                  ‘mailtype‘ => ‘html‘,                  ‘charset‘  => ‘utf-8,                  ‘priority‘ => ‘1‘               );$this->load->library(‘email‘, $config);

The third parameter is optional. If it is null, the class library is usually assigned an object with the same name as the class library. For example, if the class library name isSession, It will be assigned to$ This-> session. If you want to use your custom name, you can pass it through the third parameter.

$ This-> load-> Library ('session ', '', 'My _ Session'); // The session class can be accessed in the following way: $ this-> my_session

 (4) $ this-> load-> file ('filepath/filename', true/false );

Note: This is a common file loading function. In the first parameter, the path and file name of the file are given. The corresponding file will be opened. By default, data is sent to the browser, just like a View File, But if you set the second parameterTrue(Boolean) data is returned in the form of a string instead of sent to the browser.

 (5) $ this-> load-> database ('options', true/false );

This function is used to load the database class. Both parameters are optional. Please refer to the database section for more information.

 (6) $ this-> load-> View ('Filename', $ Data, true/false );

This function is used to load your view files.

The first parameter is required. Specify the name of the View File to be loaded. Note: you do not need to add the. php extension unless you use another extension.

Second ParameterOptionalYou can input arrays or objects. The input arrays or objects will be exported using the PHP extract function. You can use these exported variables in the view.

The third parameter is optional. It is used to change the function running mode and return data in the form of a string instead of sending it to the browser. This parameter is useful when you want to process data in different ways. If you set this parameterTrue(Boolean), the function returns data. The default value of this parameter isFalseThat is, the data will be sent to the browser. If you want the data to be returned, remember to assign it to a variable:

$string = $this->load->view(‘myfile‘, ‘‘, true);

(7) $ this-> load-> database ('model _ name ');

This function is used to load your model file.

 

7. url auxiliary functions

Redirect () function:

By sending the HTTP header, the command client redirects to the URL you specified. You can specify a complete URL or a relative URL Based on the root directory of the website. This function automatically constructs a complete URL based on your configuration file.

You can set the second parameter to location (default) or refresh. Location operations are faster than refresh operations, but sometimes errors are reported on Windows servers. The optional 3rd parameters allow you to send a specific HTTP request return code-for example, this can be used to create 303 request redirection to serve the search engine. The default request response code is 302 (temporary redirection ). The first parameter is only used for 'location' redirection, not 'refresh '. Example:

// Note !! Do not forget to load $ this-> load-> helper ('url ');If ($ logged_in = false) {redirect ('/login/form/', 'refresh');} // with 301 redirectredirect ('/article/13 ', 'location', 301 );
Note: Because this function needs to process header files, it must be called before outputting anything to the client. Note: If you want to configure HTTP headers in more detail, you can use the set_header () function of the output class.

 

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.