The simplest CI framework Entry Example-Database Data Retrieval

Source: Internet
Author: User
Tags php foreach

This is the simplest example for beginners to call. Many examples on the Internet are actually not available for beginners.

1. Download the CI framework (find it by yourself)

 

2. Configuration

Database. php Configuration:
Set the connection parameter for the database server:

$db['default']['hostname'] = "your-db-host";$db['default']['username'] = "your-username";$db['default']['password'] = "your-password";$db['default']['database'] = "your-db-name";$db['default']['dbdriver'] = "mysql";

 

3. Create a table

CREATE TABLE IF NOT EXISTS `users` (  `id` INT(8) NOT NULL AUTO_INCREMENT,  `name` VARCHAR(30) CHARACTER SET utf8 DEFAULT NULL,  `age` VARCHAR(3) CHARACTER SET utf8 DEFAULT NULL,  `sex` VARCHAR(2) CHARACTER SET utf8 DEFAULT NULL,  PRIMARY KEY  (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_estonian_ci AUTO_INCREMENT=14 ;

Enter a few pieces of data by yourself

 

4. Implement MVC

1) Implement m -- get data

Create a file mtest. php under models of Ci

<?phpclass Mtest extends CI_Model{    function Mtest(){        parent::__construct();    }        function get_last_ten_entries()    {        $this->load->database();
Mysql_query ("set names GBK"); // prevents Chinese garbled characters $ query = $ this-> DB-> get ('users', 10 ); return $ query-> result () ;}}?>

Note:

Parent: :__ construct (); indispensable
$ This-> load-> database (); it must be rare. Otherwise, an error will be reported.

You can also enable the "Automatic Connection" function to automatically instantiate the database class when each page is loaded. To enable "Automatic Connection", you can add it to the library array in the following file:Database:

Application/config/autoload. php

Otherwise, it will be written on every page like this.

You can also use $ query = $ this-> DB-> query ('select * From users ');

Write your own SQL

 

 

2) Implement c -- determine the data to be retrieved

Create a file test. php under the controllers of Ci.

<? Phpclass test extends ci_controller {function test () {parent ::__ construct ();} function index () {$ this-> load-> helper ('form '); $ data ['title'] = "Homepage"; $ data ['headline'] = "entering user information "; // multi-dimensional array $ data ['todo _ list'] = array ('clean house', 'Call Ms', 'Run errands '); // $ this-> load-> vars ($ data); $ this-> load-> model ('mtest '); $ data ['query1'] = $ this-> mtest-> get_last_ten_entries (); $ this-> load-> View ('users', $ data ); // $ This-> load-> View ('newfile'); // $ this-> load-> View ('A/newfile'); }}?>

Call Model: $ This-> load-> model ('mtest ');

Load the model into the array: $ data ['query1'] = $ this-> mtest-> get_last_ten_entries ();

Reprint the array to the page: $ this-> load-> View ('users', $ data );

2) Implement V-page display

Create a file user. php under views of Ci

 

Note: You can use the for and foreach methods to find the data you want!

NOTE: For the entire pageGarbledThe web page header is like this.

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

If you do not use CI to connect to the database, add the following code to the database connection section.

Mysql_query ("set names GBK"); // prevents Chinese garbled characters
Mysql_query ("set names utf8;"); // After mysql_select_db.

// Prevent Chinese garbled characters from viewing your database Character Set

Database. php file under CI config

$ DB ['default'] ['Char _ set'] = 'utf8 '; // utf8. the database character set is also utf8 $ DB ['default'] ['dbcollat'] = 'utf8 _ general_ci ';

 

 

For more information, see:

Controller http://codeigniter.org.cn/user_guide/general/controllers.html

Model http://codeigniter.org.cn/user_guide/general/models.html

View http://codeigniter.org.cn/user_guide/general/views.html

 

 

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.