CodeIgniter framework--Access way URI Assignment variable database operation

Source: Internet
Author: User
Tags autoload vars codeigniter

1. Access method:

CodeIgniter access URL using pathinfo, ingress File/Controller/method (/Parameter list)

eg:localhost/index.php/welcome/index/id

The first paragraph represents the call to the Controller class. The second paragraph represents a function or method in the calling class. The third and more segments represent parameters passed to the controller, such as an ID or various other variables.

2. Uri parameter gets:

Controller-side Code

1  Public functionGetURI ($id,$name,$year)2         {3             Echo"ID--->".$id." ---name--->.$name." ---year---> ".$year." <br/> ";4             Echo"segment (1)--->".$this->uri->segment (1). " <br/> ";5             Echo"Segment (2)--->".$this->uri->segment (2). " <br/> ";6             Echo"Segment (3)--->".$this->uri->segment (3). " <br/> ";7             Echo"Segment (4)--->".$this->uri->segment (4). " <br/> ";8             Echo"Segment (5)--->".$this->uri->segment (5). " <br/> ";9}

Run effect

3. Assigning variables

Uploading data from the controller to the view

Controller-side Code

1  Public functionAddView ()2         {3             $this->load->vars ("title", "Value"));4             $list=Array(5             Array(' id ' =>1, ' name ' = ' MW ', ' email ' = ' [email protected] '),6             Array(' id ' =>2, ' name ' = ' mw2 ', ' email ' = ' [email protected] '),7             Array(' id ' =>3, ' name ' = ' mw3 ', ' email ' = ' [email protected] ')8             );9             $data[' New_title ']= "Test_title";Ten             $data[' List ']=$list; One             $this->load->vars ($data); A              -             $this->load->view ("View_test"); -}

View-side code

1234<body>5Echo $title;? >6Title is <?phpEcho $title;? >7      8Echo $new _title;? >9New_title is <?phpEcho $new _title;? >Ten      One A<table> -<?phpforeach($list  as $item):?> -<tr> the<td><?=$item[' ID ']?></td> -<td><?=$item[' Name ']?></td> -<td><?=$item[' Email ']?></td> -</tr> +<?phpEndforeach;? > -</table> +</body> A

Run effect

4. Load the database:

Such operations are placed in the Model in MVC, which must inherit the data core class Ci_model while overloading the parent class's construction methods.

1 class extends Ci_model 2 {3     function __construct () 4     {5         Parent::__construct (); 6     }7 }

Every time you use a database, you need to load the database once:

1 $this->load->database ();

For convenience, you can set the load of the database to be loaded automatically, in \application\config\autoload.php.

1 $autoload Array (' database ');

For database access objects, mount to the properties of the Super object $this->db

1 $res $this->db->query ($sql); // return Object 2 $res->result (); // returns an array that is an object of one in the array 3 $res->result_array (); // returns a two-dimensional array with associative arrays 4 $res->row (); // returns the first piece of data, directly an object

AR operation Database, in the database.php file, change the value of $active_recoed to true so that AR can be used.

1 //Enquiry2  Public functionindex ()3 {4     $res=$this->db->get (' table name ');//The prefix is automatically called here5     foreach($res->result () as $item)6     {7         Echo $item->name. " <br/> ";8     }9}
1 //Insert2  Public functionindex ()3 {4     $data=Array(5' Name ' = ' Lisi ',6' Password ' =MD5(' Lisi ')7     );8     $bool=$this->db->insert ("Table name",$data);9     Var_dump($bool);Ten}
1 //Update2  Public functionindex ()3 {4     $data=Array(5' Name ' = ' Wangwu ',6' Password ' =MD5(' Wangwu ')7     );8 9     $bool=$this->db->update (' Table name ',$data,Array(' ID ' =>3));Ten     Var_dump($bool); One}
1 // Delete 2 $bool $this->db->delete (' table name ',array(' id ' =>2)); 3 Var_dump ($bool);

Summarized as:

(1) Connect to the database:$this->load->database (); Note: Write it in the constructor of the model so that the database is connected while the model is loaded (2) Insert data:$this->database->insert ($t _name,$data); $t _name:table to be manipulated$data: Data to be inserted (Key name = Field name, key value =field value, the self-increment primary key is not writable) (3) Update the data:$this->db->where (field name, field value); $this->db->Update (table name, array of modified values);4) Query 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);

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.