Various DEMO--CI Framework Learning

Source: Internet
Author: User

Winter vacation to learn a CI framework, please advise!

I. helloworld! of CI

Note: CI prohibits access to the controller directly through the file directory.

./application/controllers/hello.php
1 <?php 2//Put the user directly through the path to access the controller, if so it will show (package) 3 if (! defined (' BasePath ')) exit (' No direct script access allowed '); 4 5 class Hello extends Ci_controller {6 7 public function SayHello ($name, $name 2) {8 echo $name, ", Hello CI to ", $name 2; 9 }10}

Two. CI's text timer demo--text operation and invoke view operation

Call the basic format of the view:

$this->load->view (' XXX ');

1 <?php 2//./applications/controllers/hello.php 3//release the user directly through the path to access the controller, if so it will show (package) 4 if (! defined (' BasePath ')) E XIT (' No Direct script access allowed '); 5  6 Class Hello extends Ci_controller {7  8 public     function SayHello ($name, $name 2) {9         echo $name, ", Hello CI to ", $name 2;10     }11 public     function Show () {         $name =" Deng ";         @ $count = file_get_contents ('./ Num.txt ');  Adorner         $count = $count? $count: 0;16         $count ++;17         $data = Array (' key ' = = $name, ' value ' = = $count); 18         $re = fopen ('./num.txt ', ' W '),         fwrite ($re, $count),         $this->load->view ("testview.php" , $data);   Load two view pages         $this->load->view ("testview2.php");     }25}

Three. CI database demo--to the data model, delete, change, check

Data Model--

1. The data model is a database class

2. One model for a single table

The creation of a model must be noted--

You must inherit the data core class Ci_model and overload the constructor method in the parent class

Class Model_name extends ci_model{    function __construct ()    {        parent::__construct ();}    }

Operations on the database--

1. Connect to the database ($this->load->database ());

2. Insert Data ($this->db->insert ($t _name, $arr);)

$t _name--The table you want to manipulate]

$arr-The data you want to insert ("key" =>value)

3. Updating data

$this->db->where (field name, field value)

$this->db->update (table name, array of modified values)

4. Querying data

$this->db->where (field name, field value)

$this->db->select (field)

$query = $this->db->get (table name)

return $query->result ();

5. Delete data

$this->db->where (field name, field value)

$this->db->delete (table name)

 1./application/controllers/user.php 2 <?php 3 if (! defined (' BasePath ')) exit (' No Direct script access allowed ');          4 5 class User extends Ci_controller {6 7 public Function Insert () {8 $this->load->model (' test_m '); 9 $arr = Array (' Usid ' =>1, ' uname ' = ' deng1 ', ' upass ' = ' 1234 '), $this->test_m->user_insert ($ar R);}12 Public Function Update () {$this->load->model (' test_m '); $arr = Array (' Usid '     =>22, ' uname ' = ' deng222 ', ' upass ' = ' 1233333 '); $this->test_m->user_update (2, $arr); 17}18 19      Public Function Delete () {$this->load->model (' test_m '); $this->test_m->user_delete (22); 22 }23 Public Function Select () $this->load->model (' test_m '); $arr = $this Test_m->user_select (1); Print_r ($arr); Echo $arr [0]->usid;30}32}33 */* End of file we lcome.php */35/* locatioN:./application/controllers/welcome.php */ 

1 <?php 2/** 3 *  ./application/models/test_m.php 4 */5 class Test_m extends Ci_model 6 {7      8     function __c Onstruct () 9     {         parent::__construct ();//connect to the         database12         $this->load->database ( )         ///$this->load->insert ($t _name, $data)     }15     function User_insert ($arr) {         $this- >db->insert (' user ', $arr);     }19     function user_update ($id, $arr)     {         $this->db- >where (' Usid ', $id),         $this->db->update (' user ', $arr),     }25     function User_delete ($id) {         $this->db->where (' Usid ', $id),         $this->db->delete (' user ');     }30     function User_select ($id) {         $this->db->where (' Usid ', $id),         $this->db->select (' * '); 34         $query = $this->db->get (' user '),         return  $query->result ();     }37}38?>

four. ci File upload demo

1. Process-oriented file upload method

 1 #/controllers/upload.php 2 <?php 3 if (! defined (' BasePath ')) exit (' No Direct script access allowed '); 4 5 class Upload extends Ci_controller {6 7//Show view with Form 8 Public Function index () {9 $this->load->             View (' up '); 10 11}12 13//Display upload information from public function up () {!empty ($_post[' Sub ')) {17   function of printing variables//var_dump ($_files[' upfile '); $file = $_files[' Upfile '];20 if ($file [' Size '] >= 20000000) {echo "Size no! }23 else{24 switch ($file [' type ']) {case ' image/jpeg ': $BR = '. jpg '; break;28 def ault:30 $br = false;31 break;32}33 if ( $BR) {$time = time (); Move_uploaded_file ($file [' Tmp_namE '], "./upload/$time $br"), PNS}39 else{40 echo "type No"; 41 }42}43 44}45}46 47}

1 #/views/up.php2 

2. Object-oriented CI Framework file Upload method

A. Define an array and set some parameters related to the upload

Set the upload directory, which is written here./, the directory to be built in the Web site root, that is, and application peer//If you want to put in the application directory, you can use the system-defined path constant apppath//for example: APPPATH. ' uploads/' $config [' upload_path '] = './uploads/';//Set the type of upload allowed $config[' allowed_types '] = ' gif|jpg|png '; $config [' Max_ Size '] = ' 100 ';//If the picture can also be set to the maximum height and width $config[' max_height '] = 768; $config [' max_width '] = 1024;

B. You can also set some additional parameters to see the user manual in detail

C. 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 ');

D. receive error messages or success messages

Error message

$error = Array (' error ' = = $this->upload->display_error ());

Success Information

$data = Array (' upload_data ' = = $this->upload->data ());

1 <?php  2 if (! defined (' BasePath ')) exit (' No Direct script access allowed '); 3  4 class Upload extends Ci_cont Roller {5  6     //Show view with Form 7 public     function Index () {8         $this->load->view (' Up '); 9     }11 12     //Display upload information public     function up () {         $config [' upload_path '] = './uploads/';         $config [' Allowed_ Types '] = ' gif|jpg|png ';         $config [' max_size '] = "$";         $this->load->library (' upload ', $config )         ///Print success or error information in the         if ($this->upload->do_upload (' Upfile ')) at Max         {             $data = Array (" Upload_data "= $this->upload->data ());             Var_dump ($data);         }27         else28         {             $ Error = Array ("error" = $this->upload->display_errors ());             Var_dump ($error);         }32     }33 34 }

Five. CI Login Verification Demo

1. Using CI class to implement session login

A. Modifying a configuration file (config.php)

$config [' Encryption_key ']

B. Load Session Class

$this->load->library (' Session ');

C. Create session

$this->session->set_userdata ($array);

D. View Session

$this->session->userdata (session name);

E. Delete session

$this->session->unset_userdata (' Session name ');

 1./application/controllers/login.php 2 <?php 3 if (! defined (' BasePath ')) exit (' No Direct script access allowed '); 4 5 class Login extends Ci_controller {6 7 Public Function index () 8 {9 $this->load->view (' Login . html '),}11 public Function Checklogin () {$this->load->model ("Test_m"), $user = $t His->test_m->user_select ($_post[' uname ')), if ($user) {if ($user [0]->upass = = $_post[' UPass ']) {echo ' password is correct! '; $this->load->library (' Session '), $arr = Array ("uid" = = $user [0]->usid);  $this->session->set_userdata ($arr), echo "<br/>"; $this->session->userdata (' uid ');}24 else{25 echo ' Password is incorrect! ';}27}28 else{29 Echo ' user name does not exist ';}31}32 public Function C HeckseSsion () {$this->load->library (' Session '), ($this->session->userdata (' uid ')) {36 Echo ' is already logged in. '; PNs}38 else{39 echo ' is not logged in. ';}41}42 public Function loginout () {$this->load->library (' Session '); $th Is->session->unset_userdata (' uid '); 46}47}

Six. CI's paging function demo

1. Some parameters that must be known

A. How many records are there in total?

B. How many records will be on a page

C. Total number of pages

D. How many paging links to display before and after the current page

2. 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 ']

3. Paging class that calls CI

$this->load->library (' pagination ');

4. Perform paging methods

$this->pagination->initialize ($config);

5. Output Paging link

echo $this->pagination->create_links ();

6. Querying part of the data (limit)

echo $this->db->limit ($num, $start); Check $num from $start

 1./applications/controllers/page.php 2 <?php 3 if (! defined (' BasePath ')) exit (' No Direct script access allowed ');  4 5 class Page extends Ci_controller {6 7 public Function User_add () {8 $this->load->model (' test_m '); 9 for ($i = 1; $i <=, $i + +) {One $name = ' u '. $i; $arr = Array ("Usid" + $i, "UNAM E "= $name," UPass "=>123456), $this->test_m->user_insert ($arr),}16}17 PU Blic function PageList () {$this->load->model (' test_m '); $user = $this->test_m->user_select         _all (); $allnum = count ($user), $pagenum = 20;23 $config [' total_rows '] = $allnum; 25 $config [' per_page '] = $pagenum; $config [' num_links '] = 3;27 $config [' base_url '] = "/ci/index.php/page/p Agelist "; $config [' use_page_numbers '] = true;29 $this->load->library (' pagination '); 31 $ This->pagination->Initialize ($config); Var_dump ($this->pagination->create_links ()); Echo $this->pagination-&  Gt;create_links (); echo "<br/>"; PNs $id = $this->uri->segment (3); Get URL The third paragraph of the character $id = $id? $id: 1;39 $start = ($id-1) * $pagenum; $list = $this->test_m->user_select_limit ($start, $pagenum); Var_dump ($list); 42}43}

Various DEMO--CI Framework Learning

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.