Detailed CodeIgniter framework basic additions and deletions change the sample code of the operation

Source: Internet
Author: User
This article mainly introduces the CodeIgniter framework of the basic additions and deletions to the operation, combined with specific examples to analyze the CodeIgniter framework for the database to create, increase the deletion of the operation of the relevant implementation skills, the need for friends can refer to the next

In this paper, the basic additions and deletions of codeigniter frame are described. Share to everyone for your reference, as follows:

For CodeIgniter and additions and deletions, here I use one of my own example to illustrate:

To create a database:

CREATE TABLE IF not EXISTS ' users ' (' id ' int (ten) NOT NULL auto_increment, ' username ' varchar () ' is not null, ' password ' var char () not NULL, ' email ' varchar (+) NOT NULL, ' fullname ' varchar (+) NOT NULL, PRIMARY key (' id '), UNIQUE key ' Userna Me ' (' username '), UNIQUE KEY ' email ' (' email ')) Engine=innodb DEFAULT Charset=utf8;

Model

mtest.php

<?php  class Mtest extends ci_model{public    function construct () {      parent::construct ();      $this->load->database ();    }    INSERT into Data public    function Insert_users ($arr) {      $this->db->insert (' users ', $arr);    }    Delete Data public    function Delete_users ($id) {      $this->db->where (' id ', $id);      $this->db->delete (' users ');    Alter data public    function Update_users ($username, $arr) {      $this->db->where (' username ', $username) ;      $this->db->update (' users ', $arr);    }    Select data public    function Select_users ($id) {      $this->db->where (' id ', $id);      $this->db->select (' * ');      return $this->db->get (' users ');      Note that if you return the following content, he is the result set of an object, so that when you return to the controller, you have to convert;/     return $query->result ();}  ? >

Controller

home.php

<?php/** * xxx.php * ============================================== * Copy Right 2012-2015 *------------------------ ----------------------* This is not a free software, without any authorization are not allowed to use and spread. * ============================================== * @Author: yexianming * @Email: LangWaiShiGe@hotmail.com * @Version:  Zend studio10.6.2 php5.4.38 apache2.2 */if (!defined (' BasePath ')) exit (' No Direct script access allowed ');    Class Home extends ci_controller{public function construct () {parent::construct ();      }//insert Data Public function Insert () {$this->load->model (' Mtest ', ", TRUE); $arr =array (' username ' = ' yexianming1 ', ' password ' = ' admin ', ' email ' = ' 11504160314qq.com ', ' fullname ' + =      ' luotianyexianming '); if (! (      $this->mtest->insert_users ($arr)) {echo "Insert data successfully";      }else{echo "Insert data failed";  }}//update data public Function Update () {$this->load->model (' Mtest ', ", TRUE);    $arr =array (' username ' = ' Helloworld22 ', ' password ' = ' root ', ' email ' = ' 895787704@qq.com ', ' fullname ' and ' = '      Luotianyecong '); if (! (      $this->mtest->update_users (' Helloworld ', $arr)) {echo "modified successfully";      }else{echo "failed to modify";      }}//delete data public function Delete () {$this->load->model (' Mtest ', ", TRUE);      $query = $this->mtest->delete_users (12);      if (! $query) {echo "Delete succeeded";      }else{echo "Delete failed";      }}//select data Public function Select () {$this->load->library (' table ');      $this->load->model (' mtest ', ', TRUE);      $arr = $this->mtest->select_users (3);      $userinformation = $this->table->generate ($arr);      $data [' userinfor ']= $userinformation;    $this->load->view (' template ', $data); }}?>

View

temlate.php

<?php echo $userinfor;? >

Output

ID username password email fullname3 Helloworld22 root 1111111@qq.com luotianyecong

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.