[CI] CodeIgniter quick development guide, cicodeigniter_PHP tutorial

Source: Internet
Author: User
Tags php foreach
[CI] CodeIgniter quick development guide, cicodeigniter. [CI] CodeIgniter quick Development Guide. cicodeigniter provides the strongest [CI] CodeIgniter quick development guide since CI is used. cicodeigniter

Bytes ---------------------------------------------------------------------------------------------------------

The most intense feeling since CI is its thorough MVC design. for example, in the application/modesl Directory, write our model operations to inherit the CI_Model.

In the controller, only the logic is written, and the database cannot be operated directly. the model needs to be called directly for data, and finally the template is called.

The following describes the collaboration between models, controllers, and views.

/*** User model, complete CURD example * @ Chenwei */class User_model extends CI_model
{Public function _ construct () {parent ::__ constrcut () ;}/ *** query user information. We do not recommend that you use the single id parameter as a condition, to facilitate the controller to assemble conditions and reuse this model method * @ param array format, for example, $ where = array ('id' => 1 ); * @ return array */public function userInfo ($ 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 in the form of a two-dimensional array} else {
$ Res = $ this-> db-> select ('Id, username, age')-> get ('users ');
Return $ res-> result_array () ;}/ *** add user * @ param array format, for example, $ data = array ('username' => 'chenwei ', 'age' => '18'); * @ reteurn bool */public function userAdd ($ 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 function userDel ($ id) {if ($ id) {$ where = array ('id' => $ id); $ bool = $ this-> db-> where ($ where)-> delete ('users '); return $ bool;} else {return false ;}} /*** modify user * @ param array $ where condition * @ param array $ data new data * @ reteurn bool */public function userEdit ($ where, $ data) {if ($ where & is_array ($ where) {$ bool = $ this-> db-> where ($ where)-> update ('users ', $ data); return $ bool;} else {return false ;}}/ *** Notes: * 1. the model class name User_model has upper-case letters and lower-case letters. it inherits the basic model class CI_Model * 2. class file name application/models/user_model.php * 3. how to load this model in the controller:
$ This-> load-> model ('User _ model', 'User'); this is introduced with the User as the object name;
$ This-> load-> model ('User _ Model'); this is introduced by default using User_model as the object name. sub-directories are supported for model files;
If the class file is in application/models/blog/user_model.php, you can introduce: $ this-> load-> model ('blog/User_model '); * 4. if necessary, you can set automatic loading in application/config/autoload. php file.
* 5. if no automatic database connection is set, you can set the connection when adding the model, such as $ this-> load-> model ('User _ model', '', TRUE );*/

Ps:
Here is an example of a joint query. you can try it if necessary:
$ 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 */class Users extends CI_Controller {public function _ construct () {parent ::__ construct (); $ this-> load-> model ('User _ model', 'User');}/*** User list */public function index () {$ data ['User _ list'] = $ this-> user-> userInfo (); $ this-> load-> view ('User _ list ', $ data); // call the template and output the data to the foreground}/*** add user */public function user_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 successful! ');} Else {$ this-> show_tips (' Operation failed! ') ;}}/*** Modify user */public function user_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 successful! ');} Else {$ this-> show_tips (' Operation failed! ') ;}} Else {$ this-> show_tips ('invalid operation! ') ;}}/*** Delete user */public function user_del () {$ id = $ this-> input-> post ('id '); $ bool = $ this-> user-> userDel ($ id); if ($ bool) {$ this-> show_tips ('Operation successful! ');} Else {$ this-> show_tips (' Operation failed! ');}}}/**
* Notes: * 1. the controller file is in application/controller/users. php, supports subdirectories * 2. the first letter of the controller name must be capitalized and must inherit from CI_Controller * 3. the front and back-end permission control is in the application/core/MY_Controller.php file,
Define two controllers for front-end and back-end, inherit CI_Controller, and the rest only need to inherit these two custom controllers. * 4. define the default controller in application/config/route. php */

/*** View layer example ** @ Chenwei */
 Load-> view ('header');?>
 

Load-> view ('header');?> /*** Notes: * 1. in the template, you can directly use the variables allocated in the controller and use all the functions and methods of the CI system. * 2. after the CI short label support is enabled, even if php does not enable the support, CI will automatically resolve the issue and can be safely used. */

There may be a hand error and the above Code should not be copied and used directly. for more useful CI usage, refer to the CI manual at any time.

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

@ Blackeye poet

Zookeeper is the strongest since CI is used...

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.