CI Framework (ii), CI framework
Custom SQL statements
When the API provided does not meet our requirements for SQL statements, we usually write our own SQL statements, CI also provides a more powerful, to meet our needs of the general SQL API.
$res=$this->db->select (' id,name ') ->from (' table name ') ->whrer (' ID >= ', 5) // Note that there is a space after the ID ->limit (3,2 )//here with the limit of SQL is the order is reversed ->order_by (' id desc ') , Get (); // translate into SQL statements Var_dump ($res, result ()); Echo $this->db->last_query (); // first a recent SQL
Custom Extension Controller
Create a new my_controller.php in Application/core
class extends ci_controller{ publicfunction __construct () { parent::__ Construct (); // Be sure. Call the parent class's constructor method //login authentication, permission validation, other actions ... }}
Also need to be configured in application/config/config.php:
$config [' subclass_prefix '] = ' my_ ';
Custom extension model
Create user_model.php in Application/models
class extends ci_model{ publicfunction getAll () { $res $this->db->get (' table name '); return $res, result ();} }
Invoking a custom model in the controller
Application/controllers:
class extends my_controller{ publicfunction index () { $this Load->model (' User_model '); // The call is based on the class name instead of the file name
$list $this->user_model->getall (); // call model to get data
$this->load->view (' User/index ',array(' list ' = =$list)); // Load View } }
You can give the model a name when loading the model:
$this->load->model (' User_model ', ' User '); // The call is based on the class name instead of the file name $list $this->user->getall (); // call model to get data
URL-related functions
In the form verification, you need to pass the data to the controller, how accurate and extensible write action, call API:
Public function AddView () { $this->load->helper (' url '); $this->load->view (' User/add ') with the URL function in order not to write the address that the form is passed to ;}
In the user/add.php view:
<action= "
" method= "POST">
form >
If it is a index.php directory, use:
Base_url ();
This API.
At the same time, each load URL is cumbersome, but also set to auto-load, modified in config/config.php:
$config Array (' url ');
It may not be loaded automatically in later versions.
Routing
$route [' rouxx/showxx/([\d]+) \.html '] = ' rou/show/$1 '; // Insert this sentence
Page out
- Some parameters to be aware of
How many records are there in total?
How many records are on a page?
Total number of pages
How many paging links to display before and after the current page
- Set some CI paging class basic parameters
// total number of bars $config [' Total_rows '] // a page showing a few $config [' Per_page '] // Define a few digital links to the front and back of the current page $config [' Num_links '] // define no paging parameters, master URL $config [' Base_url ']
- Paging class for Calling CI
$this->load->library (' pagination ');
- To perform a paging method
$this->pagination->initialize ($config);
Echo $this->pagination->create_links ();
- Querying partial data (limit)
Echo $this->db->limit ($num,$start); // check $num from $start
Phpif( !defined(' BasePath '))Exit(' No Direct script access allowed ');classPageextendsCi_controller { Public functionUser_add () {$this->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 functionpagelist () {$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 URL Third paragraph character $id=$id?$id: 1; $start= ($id-1) *$pagenum; $list=$this->test_m->user_select_limit ($start,$pagenum); Var_dump($list); }}
Uploading files
View/views/up.php:
< HTML > < action= "Ci/codeigniter_2.2.0/index.php/upload/up" method= "POST" enctype = "Multipart/form-data" > < type= "file" name= "Upfile"/> < type = "Submit" name = "Sub" value = "Commit" />
form>
HTML >
Controller:
- Define an array, set some parameters related to the upload
$config [' upload_path '] = './uploads/'; // set the type of upload allowed $config [' allowed_types '] = ' gif|jpg|png '; $config [' max_size '] = ' + '; // If you have a picture, you can also set the maximum height and width $config [' max_height '] = 768; $config [' max_width '] = 1024;
You can also set some additional parameters to see the user manual in detail.
- Call the CI to upload the generic class and perform the upload
// upload is the class name of the call, all lowercase $this->load->library (' upload ',$config); // if the name of the upload box is written in UserFile, then it is not necessary to pass the parameter, if not, the value of name is passed in $this->upload->do_upload (' name ' of the ' upload box ');
- Receive error messages or success messages
// error Message $error Array $this->upload->display_error ()); // Success Information $data Array $this->upload->data ());
Phpif( !defined(' BasePath '))Exit(' No Direct script access allowed ');classUploadextendsCi_controller {//display a view with a form Public functionindex () {$this->load->view (' Up ')); } //Display upload Information Public functionUp () {$config[' upload_path '] = './uploads/'; $config[' allowed_types '] = ' gif|jpg|png '; $config[' max_size '] = "2000"; $this->load->library (' Upload ',$config); //print a successful or incorrect 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
Using CI class to implement session login
- Modifying a configuration file (config.php)
// generates a random, non-repeating string of bits encrypted key saved to Config.php's Encryption_key $config [' Encryption_key ']= ' adb8bf6d0ac4e17b42a80941582497a4 ';
$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_sessions '; $config [' sess_match_ip '] = FALSE; $config [' sess_match_useragent '] = TRUE; $config [' sess_time_to_update '] = 300;
- Disposable data, can only be read once
// Set $this->session->set_flashdata (' Test ', ' aaaaa '); // Read $test $this->session->flashdata (' Test ');
How the PHP CI framework takes values from a database (a two-dimensional array) to a view layer in a tabular format
Control layer
function Test_func () {
Get the two parameters required for the Model page
$competition _id = $_get["competition_id"];
$report _class = $_get["Report_class"];
$this->load->model ("Action"); Introducing Model
$data ["head"] = $this->action->get_report_item ($competition _id, $report _class); Functions that Reference Model
$this->load->view ("Test_result", $data); Display the results in the test_result.php page
}
View Layer:
Add result display The results of the cyclic output transfer from the control layer are selected here
Field name (meaning) </td>//The TD shows the meaning of the data you get from the database, the model layer, how much you want to display, which to show, and where to confirm </tr>
|
!--? php echo $item--->test;? ></td> </tr> </table> !--? PHP}else { echo "123"; }? </div> PHP ci framework problem? The younger brother is a beginner is CI according to each of the $data can declare a variable to view can be directly called CI schema that's how you work. Specifically how to declare not too much scrutiny http://www.bkjia.com/phpjc/838913. HTML www.bkjia.com true http://www.bkjia.com/phpjc/838913.html techarticle ci frame (ii), CI Framework custom SQL statement When the API provided does not meet our requirements for SQL statements, we usually write our own SQL statements, CI also provides a more powerful ... |