Essentials of CodeIgniter framework learning

Source: Internet
Author: User
Tags php foreach codeigniter
CodeIgniter framework learning points the following content is excerpted from the brothers' CI teaching video: codeigniter.org. cntutorials & nbsp; strong & nbsp CodeIgniter framework learning points

The following content is excerpted from the brothers' CI teaching video:

Http://codeigniter.org.cn/tutorials/

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

Codeigniter framework

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

Instructor: Yan Yiliang

Weibo: weibo.com/it266

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

Main content

CI introduction

In-depth MVC design model

Controller and view in CI

Super object in CI

Database access

AR model

How to extend the CI controller


Url-related functions


Set route


Paging


File Upload


Session


Verification code


Form verification

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

What is CI?

CodeIgniter is a lightweight but powerful PHP framework.

Based on the MVC design pattern, a rich set of class libraries are provided.

Easy to Learn, efficient and practical

Official website

Www.codeigniter.com

Chinese website

Http://codeigniter.org.cn

Download the latest version

CodeIgniter_2.1.4.zip

(As of January, the latest version is 3.0.0-note by the author)

What are the features?

You want a small framework

You need outstanding performance

You need to be widely compatible with various PHP versions and configurations on standard hosts

For CI 2.1.4, PHP5.1.6 is required.

You want a framework with almost zero configuration

You want a framework without any command operators

You want a framework that does not need to stick to restrictive coding rules.

You do not want to be forced to learn a template language (although you can select your preferred template parser)

You don't like complexity. you love simplicity.

You need clear and complete documentation

Directory Structure Description

License Agreement

User_guide User Manual

Javase framework core file

Application Directory

Index. php entry file

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

MVC

1. entry file

The only script file directly requested by the browser

2. Controller

Coordinate models and views

3. model

Provide data and save data

4. View

Only responsible for displaying

Form...

5. action

Is the method in the controller, used for browser requests

MVC in CI

The access url uses pathinfo.

Entry file. php/controller/Action

In the application directory:

Controllers controller

Models model

Views

The default controller is welcome.

The default action is index.

Controller

1. no suffix is required

2. all file names are in lowercase, for example, user. php.

3. all controllers directly or indirectly inherit from the CI_Controller class

4. action (method) requirements in the controller:

Public

It cannot start _

View

1. load the view in the controller

// Write the View name directly without the extension. if there is a subdirectory, write the directory name.

2. directly use the native PHP code in the view

3. recommended

Super object

Current Controller object

Provides many attributes:

$ This-> load

Loader class instance system/core/loader. php

Methods provided by the loader class:

View () load view

Vars () allocates variables to the view

Database () load database operation objects

Model () load model object

Helper ()

$ This-> uri

Is an instance of the CI_URI class system/core/URI. php

The CI_URI class provides the following methods:

Segment () is used to obtain parameters in the uri

Traditional: entry file. php/controller/action/parameter 1/value 1/parameter 2/value 2

Entry file. php/controller/action/value 1/value 2

Echo $ this-> segment (3); // value 1

Echo $ this-> segment (4); // value 2

// Index. php/controller/index/6

Public function index ($ p = 0) {echo $ p; // output 6

}

$ This-> input

Input class

Is an instance of the CI_URI class system/core/input. php

The CI_URI class provides the following methods:

$ This-> input-> post ('Username'); // equivalent to $ _ POST ['username'];

$ This-> input-> server ('document _ root'); // equivalent to $ _ SERVER ['document _ root'];

$ This-> input-> server ('remote _ ADDR ');

In the view, you can directly use $ this to access attributes in the Super object.

Database access

Modify configuration file

Application/config/database. php

Load the database access object to the properties of the super object $ this-> db

$ This-> load-> query ($ SQL); // return object

$ Res = $ this-> db-> query ($ SQL); // return object

$ Res-> result (); // returns an array, which contains objects one by one.

$ Res-> result_array (); // returns a two-dimensional array with an associated array

$ Res-> row () // returns the first data, directly an object


Parameter binding
$ SQL = "select * from blog_user where name =? ";
$ This-> db-> query ($ SQL, $ name); // if there are multiple question marks, you need to input an index array

Table prefix
$ Db ['default'] ['dbprefix'] = 'new _';
$ Db ['default'] ['swap _ pre'] = 'swap _';


The configuration is the same. in the code, you can hardcode the table prefix. if the project database table prefix changes in the future,
You only need to modify $ db ['default'] ['dbprefix'] = 'new _ '. swap _ in the code is automatically replaced with new _

Automatic db loading
Application \ config \ autoload. php
$ Autoload ['libraries'] = array (database );
Not required: $ this-> load-> database ();

Taken from the incremental id

$ This-> db-> insert_id ();

Number of affected rows

$ This-> db-> affected_rows ();

Active record

1. application/config/database. php

Make sure $ active_record = TRUE;

2. application/config/autoload. php

$ Autoload ['libraries'] = array (database );

3. in the configuration file, after the table prefix is correctly configured

$ Res-> $ this-> db-> get ('Table name'); // return result set object

$ Res-> result ();

$ Bool = $ this-> db-> insert ('Table name', associated array );

$ Bool = $ this-> db-> update ('Table name', associated array, WHERE condition );

$ Bool = $ this-> db-> delete ('Table name', WHERE condition );

// Select uid, username from user where uid> = 3 order by uid desc limit 2 and 3

$ Res = $ this-> db-> select ('uid, username ')

-> From ('user ')

-> Where ('uid> = ', 1)

-> Limit (3, 2) // skip 2 and retrieve 3 data records

-> Order_by ('uid desc ')

-> Get ();

// Display the last SQL statement

Echo $ this-> db-> last_query ();

// Where

// $ Res = $ this-> db-> where ('username', 'marry')-> get ('user ');

// $ Res = $ this-> db-> where ('username! = ', 'Marry')-> get ('user ');

// $ Res = $ this-> db-> where ('username', 'marry')-> get ('user ');

$ Res = $ this-> db-> where (array ('username' => 'hanyile ', 'uid <' => 3 )) -> get ('user ');

Echo $ this-> db-> last_query ();

For complex queries, use $ this-> db-> query ($ SQL, $ data); // bind a query with a question mark.

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.