[CI] CodeIgniter Quick Development Guide, cicodeigniter_php Tutorial

Source: Internet
Author: User
Tags codeigniter

[CI] CodeIgniter Rapid Development Guide, Cicodeigniter


---------------------------------------------------------------------------------------------------------

The strongest feeling since using CI is its thorough MVC design, for example: In the Application/modesl directory, write our model operations and unify the inheritance of Ci_model.

But in the controller only writes the logic, cannot directly manipulate the database, needs the data to call the model directly, finally is invokes the template.

The following shows the collaboration between models, controllers, and views, respectively.

/** * User model, complete curd example * @Chenwei*/classUser_modelextendsCi_model
{ Public function__construct () {Parent::__constrcut (); } /** * Query the user information, here is not recommended to use the single ID parameter as a condition, in order to facilitate the controller to assemble the conditions of their own re-use this model method * @param array format such as: $where = array (' ID ' =>1); * @return Array*/ Public functionUserInfo ($where = Array ()) { if($where&&Is_array($where)) { $res=$this->db->select (' ID, username, age ')->where ($where)->get (' users '); return $res->result_array ();//returns the result as a two-dimensional array } Else {
$res = $this->db->select (' ID, username, age ')->get (' users ');
return $res->result_array (); } } /** * Add user * @param array format such as: $data = Array (' username ' = ' chenwei ', ' age ' = ' 18 '); * @reteurn BOOL*/ Public functionUSERADD ($data) { if($data&&Is_array($data)) { $bool=$this->db->insert (' Users ',$data); return $bool; } Else { return false; } } /** * Delete user * @param int $id * @reteurn bool*/ Public functionUserdel ($id) { if($id) { $where=Array(' id ' = =$id); $bool=$this->db->where ($where)->delete (' users '); return $bool; } Else { return false; } } /** * Modify user * @param array $where conditions * @param array $data new data * @reteurn BOOL*/ Public functionUseredit ($where,$data) { if($where&&Is_array($where)) { $bool=$this->db->where ($where)->update (' users ',$data); return $bool; } Else { return false; } }}/** * NOTE: * 1. Model class name User_model first letter uppercase, the remaining letters lowercase, inheriting the underlying model class Ci_model * 2. class file name application/models/user_model.php * 3. How to load this model into the controller:
$this->load->model (' User_model ', ' User '); This is introduced by the user as the object name;
$this->load->model (' User_model '); This is introduced by default with User_model as the object name. The model file supports subdirectories;
If the class file is in application/models/blog/user_model.php, it can be introduced as follows: $this->load->model (' Blog/user_model '); * 4. If necessary, you can set the auto-load in the application/config/autoload.php file.
* 5. If you do not set up the automatic connection database, add in the model when you can set the connection, like this $this->load->model (' User_model ', ', TRUE); */

Ps:
Here is an example of a federated query that you might want to try:
$res = $this->db->select (' P.id, P.uid, P.order_no, P.amount, P.pay_way, P.pay_type, P.pay_bank, P.pay_time, P.goods_type, P.contact_tel, P.detail_desc, P.add_time, U.username ')->from (' payment as P ')->join (' Users as U ', ' P.uid = U.id ')->order_by (' p.id ', ' desc ')->get ();

/** * User Controller, curd example * @Chenwei*/classUsersextendsci_controller{ Public function__construct () {Parent::__construct (); $this->load->model (' User_model ', ' User '); }        /** user list*/     Public functionindex () {$data[' user_list '] =$this->user->UserInfo (); $this->load->view (' User_list ',$data); Invoke the template and output the data to the foreground}/** * Add Users*/     Public functionUser_add () {$data=Array(            ' Username ' =$this->input->post (' name '); ' Age ' =intval($this->input->post (' Age '));        ); $bool=$this->user->useradd ($data); if($bool)        {           $this->show_tips (' Operation succeeded! ')); }        Else        {            $this->show_tips (' operation failed! ')); }    }    /** * Modify user*/     Public functionUser_edit () {$id=$this->input->post (' id '); $data=Array(            ' Username ' =$this->input->post (' name '); ' Age ' =intval($this->input->post (' Age '));        ); if($id)        {
$where = array (' id ' = = $id);
$bool=$this->user->useredit ($where,$data); if($bool) { $this->show_tips (' Operation succeeded! ')); } Else { $this->show_tips (' operation failed! ')); } } Else { $this->show_tips (' illegal operation! ')); } } /** * Delete user*/ Public functionUser_del () {$id=$this->input->post (' id '); $bool=$this->user->userdel ($id); if($bool) { $this->show_tips (' Operation succeeded! ')); } Else { $this->show_tips (' operation failed! ')); } }}/**
* Some notes: * 1. Controller file in application/controller/users.php, support subdirectory * 2. The first letter of the controller name must be capitalized and must inherit Ci_controller * 3. Both front and back permissions control are in the application/core/my_controller.php file,
Define two controllers, respectively, for foreground and background, inherit Ci_controller, and the rest simply inherit the two custom controllers. * 4. Define the default controller, in application/config/route.php*/

/**
 
   php    $this->load->view (' header '); >
 
      $user _list$user _listas$v
 
  
          
  
   if ():?>            
  
    foreach (  ):?>            
  
   
    Endforeach;? >  endif;? > 
   
$v [' username '];? >
PHP $this->load->view (' header '); >/** *

There may be hand errors, the above code does not directly copy the use; For more practical uses of CI, you can check the CI manual at any time.

Link:http://www.cnblogs.com/farwish/p/3991419.html

@ Black-eyed poet

http://www.bkjia.com/PHPjc/946584.html www.bkjia.com true http://www.bkjia.com/PHPjc/946584.html techarticle [Ci]codeigniter Rapid Development Guide, Cicodeigniter----------------------------------------------------------------------- ----------------------------------the strongest since the use of CI ...

  • 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.