1. Download the CI framework (find it yourself)
2. Configuration
database.php configuration:
To set the connection parameter for the database server:
SOURCE print?
- $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. Build a table
SOURCE print?
- <strong>CREATE TABLE IF not EXISTS ' users ' (
- ' ID ' INT (8) not NULL auto_increment,
- 'name ' VARCHAR (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;
- </strong>
Fill in a few data yourself
4. Implementing MVC
1) Implement m--data collection
Create a new file under CI models mtest.php
SOURCE print?
- <?php
- Class Mtest extends ci_model{
- function Mtest () {
- Parent::__construct ();
- }
- function get_last_ten_entries ()
- {
- $this->load->database ();
SOURCE print?
- mysql_query ("SET NAMES GBK"); //Prevent Chinese garbled characters
- $query = $this->db->get (' users ', 10);
- return $query->result ();
- }
- }
- ?>
Description
Parent::__construct (); no less
$this->load->database (); must not be less or will be an error
You can also implement the Auto Connect feature, which will automatically instantiate the database class each time a page is loaded. To enable automatic connection, you can add it in the library array in the following file database :
application/config/autoload.php
不然就要像这里一样写在每个页面上。
You can also use $query = $this->db->query (' SELECT * from users ');
Write your own SQL in this way
2) Implement c--decision to take those data
Create a new file under CI controllers test.php
SOURCE print?
- <?php
- Class Test extends Ci_controller {
- function Test () {
- Parent::__construct ();
- }
- function Index () {
- $this->load->helper (' form ');
- $data [' title '] = "Home";
- $data [' headline '] = "input user Information";
- //Multidimensional Arrays
- $data [' todo_list '] = Array (' Clean House ', ' call Mom ', ' 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 ();
The array is reproduced on the page: $this->load->view (' users ', $data);
2) Implement v--page display
CI Views under Create a new file user.php
SOURCE print?
- <title><? echo $title;? ></title>
- <body>
- <ul>
- <?php foreach ($todo _list as $item):?>
- <li><?php echo $item;? ></li>
- <?php Endforeach;? >
- </ul>
- <ul>
- <? Echo count ($query 1);
- foreach ($query 1 as $v 1) {
- foreach ($v 1 as $v 2) {
- echo "$v 2\n";
- }
- }
- For ($row =0; $row <count ($query 1); $row + +) {
- echo $query 1[$row]->name." </br> ";
- }
- ?>
- <?php foreach ($query 1 as $v):?>
- <li><?php echo $v->name;? ></li>
- <?php Endforeach;? >
- </ul>
- echo $headline;?>
- </body>
Description: You can use for and foreach multiple methods to find the data you want!
Description: If the whole page is garbled , the page header is probably the case.
SOURCE print?
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
If you are not using CI to connect to the database, add the following code to the database connection section.
mysql_query ("SET NAMES GBK"); Prevent Chinese garbled characters
mysql_query ("set names UTF8;"); //In mysql_select_db ("");
Prevent Chinese garbled to see your database character set
database.php file under CI Config
SOURCE print?
- $db [' Default '] [' char_set '] = ' UTF8 '; //utf8. The database character set is also UTF8
- $db [' Default '] [' dbcollat '] = ' utf8_general_ci ';
More do not understand, please refer to:
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
Original link: http://blog.csdn.net/21aspnet/article/details/6599780
"Go" The simplest example of a CI framework getting started--database fetching data