Ci framework (II): ci framework. Ci framework (II): ci framework custom SQL statements when the provided API cannot meet our requirements for SQL statements, we usually write SQL statements by ourselves, CI also provides a powerful ci framework (II ).
Custom SQL statements
When the provided API does not meet our requirements for SQL statements, we usually write SQL statements by ourselves, and CI also provides powerful, A general SQL API that can meet our needs.
$ Res = $ this-> db-> select ('Id, name')-> from ('Table name')-> whrer ('Id> = ', 5) // note that there must be a space behind the id-> limit () // here the order of limit with SQL is reversed-> order_by ('Id desc ') -> get (); // translate it into an SQL statement var_dump ($ res-> result (); echo $ this-> db-> last_query (); // first, the latest SQL statement
Custom extended controller
Create MY_Controller.php in application/core
Class MY_Controller extends CI_Controller {public function _ construct () {parent ::__ construct (); // you must first call the constructor of the parent class // logon verification, permission verification, and other operations... }}
You also need to configure in application/config. php:
$config['subclass_prefix'] = 'MY_';
Custom extension model
Create user_model.php in application/models
Class User_model extends CI_Model {public function getAll () {$ res = $ this-> db-> get ('Table name'); return $ res-> result ();}}
Call the custom model in the controller
Application/controllers:
Class User extends MY_Controller {public function index () {$ this-> load-> model ('User _ Model'); // the main class name is called instead of the file name.
$ List = $ this-> User_model-> getAll (); // call the model to obtain data
$ This-> load-> view ('user/Index', array ('list' => $ list); // load view }}
When loading a model, you can give the model a name:
$ This-> load-> model ('User _ model', 'User'); // the main class name is called, instead of file name $ list = $ this-> user-> getAll (); // call the model to obtain data
Url-related functions
During form verification, data needs to be transmitted to the controller. how can we accurately and elastically write actions and call the API:
Public function addView () {$ this-> load-> helper ('URL'); // to avoid writing the address passed by the form, use the url function $ this-> load-> view ('user/add ');}
In the user/add. php view:
For the index. php Directory, use:
base_url();
This API.
At the same time, it is very troublesome to load the url each time, but it is also set to automatic loading. in config/config. php, modify:
$config['helper'] = array('url');
This feature may not be automatically loaded in later versions.
Routing
$ Route ['rouxx/showxx/([\ d] +) \. html '] = 'rou/show/$ 1'; // Insert this sentence
Paging
Total number of records
How many records are required for one page?
Total number of pages
How many paging links are displayed before and after the current page
- Set some basic CI paging parameters
// Total number of items $ config ['total _ rows '] // several items are displayed on a page. $ config ['per _ page'] // you can specify the number of links before and after the current page. $ config ['Num _ link'] // defines no paging parameters, main URL $ config ['base _ url']
- Paging class that calls CI
$this->load->library('pagination');
$this->pagination->initialize($config);
echo $this->pagination->create_links();
- Querying some data (limit)
Echo $ this-> db-> limit ($ num, $ start); // query $ num entries from $ start
Load-> model ('test _ m'); for ($ I = 1; $ I <= 100; $ I ++) {$ name = 'U '. $ I; $ arr = array ("usid" => $ I, "uname" => $ name, "upass" => 123456 ); $ this-> test_m-> user_insert ($ arr) ;}} public function pagelist () {$ this-> load-> model ('test _ m '); $ user = $ this-> test_m-> user_select_all (); $ allnum = count ($ user); $ pagenum = 20; $ config ['total _ rows '] = $ allnum; $ config ['per _ page'] = $ pagenum; $ config ['Num _ links'] = 3; $ config ['base _ url'] = "/CI/index. php/page/pagelist "; $ config ['use _ page_numbers '] = true; $ this-> load-> library ('pagination '); $ this-> pagination-> initialize ($ config); var_dump ($ this-> pagination-> create_links (); echo $ this-> pagination-> create_links (); echo"
"; $ Id = $ this-> uri-> segment (3); // get the third character of the url $ id = $ id? $ Id: 1; $ start = ($ id-1) * $ pagenum; $ list = $ this-> test_m-> user_select_limit ($ start, $ pagenum ); var_dump ($ list );}}
Upload files
View/views/up. php:
Controller:
- Define an array and set upload-related parameters
$ Config ['upload _ path'] = '. /uploads/'; // Set the upload type $ config ['allowed _ types'] = 'gif | jpg | png '; $ config ['max _ size'] = '000000'; // you can set the maximum height and width for an image. $ config ['max _ height'] = 100; $ config ['max _ width'] = 1024;
You can also set additional parameters. For more information, see the user manual.
- Call the CI Upload generic class and execute the Upload
// Upload is the name of the called class, all in lower case $ this-> load-> library ('upload', $ config); // if the name of the upload box is userfile, you do not need to pass the parameter. if not, transfer the name value to $ this-> upload-> do_upload ('upload box name ');
- Receive error message or successful message
// Error message $ error = array ('error' => $ this-> upload-> display_error ()); // success information $ data = array ('upload _ data' = >$ this-> upload-> data ());
Load-> view ('up');} // display the upload information. public function up () {$ config ['upload _ path'] = '. /uploads/'; $ config ['allowed _ types'] = 'gif | jpg | png '; $ config ['max _ size'] = "2000 "; $ this-> load-> library ('upload', $ config ); // Print the success or error message if ($ this-> upload-> do_upload ('upfile ')) {$ data = array ("upload_data" = >$ this-> upload-> data (); var_dump ($ data );} else {$ error = array ("error" => $ this-> upload-> display_errors (); var_dump ($ error );}}}
Session
Implement session login using CI class
- Modify the configuration file (config. php)
// Generate a random, non-repeated string location encryption key and save it to config. php's encryption_key $ config ['encryption _ key'] = 'adb8bf6d0ac4e17b42a80941510997a4 ';
$this->load->library('session');
$array = array('id'=>3,'name'=>'jack');$this->session->set_userdata($array);
$ This-> session-> userdata (session name );
$ This-> session-> unset_userdata ('session name ');
$ Config ['sess _ cookie_name '] = 'ci _ session'; $ config ['sess _ expiration'] = 7200; $ config ['sess _ expire_on_close '] = FALSE; $ config ['sess _ encrypt_cookie '] = TRUE $ config ['sess _ use_database'] = FALSE; $ config ['sess _ table_name '] = 'ci _ session '; $ config ['sess _ match_ip '] = FALSE; $ config ['sess _ match_useragent'] = TRUE; $ config ['sess _ time_to_update '] = 300;
- One-time data, read only once
// Set $ this-> session-> set_flashdata ('test', 'aaaaa '); // read $ test = $ this-> session-> flashdata ('test ');
How does the php CI framework display values in the database (a two-dimensional array) in the form of tables to the view layer?
Control Layer
Function test_func (){
// Obtain the two parameters required for the model page
$ Competition_id = $ _ GET ["competition_id"];
$ Report_class = $ _ GET ["report_class"];
$ This-> load-> model ("Action"); // introduce model
$ Data ["head"] = $ this-> Action-> get_report_item ($ competition_id, $ report_class); // reference the model function
$ This-> load-> view ("test_result", $ data); // display the result on the test_result.php page.
}
View layer:
Add result Display // Here, the result of loop output transmission from the control layer is selected.
Field name (meaning) </td> // This td displays the meaning of the data you have obtained from the database, that is, the model layer, how much data you want to display, and which one to display, confirm here </Tr>
|
Test;?> </Td> </Tr> </Table> Echo "123 "; }?> </P>
Php CI framework problems? Younger brother is a beginner You can use $ title directly in the view file. Yes. CI can declare a variable in $ data to view and call it directly. The CI architecture is working like this. how to declare it?
Ci framework custom SQL statements when the provided API cannot meet our requirements for SQL statements, we usually write SQL statements by ourselves, CI also provides relatively powerful... |