"Go" The simplest example of a CI framework getting started--database fetching data

Source: Internet
Author: User
Tags vars codeigniter

1. Download the CI framework (find it yourself)

2. Configuration

database.php configuration:
To set the connection parameter for the database server:

SOURCE print?
    1. $db [ ' default '] [ ' hostname '] =  "Your-db-host";   
    2. $db [ ' default '][ ' username ']  =  "Your-username";   
    3. $db [ ' default '][ ' password '] =   "Your-password";   
    4. $db [ ' default '][ ' database '] =  "Your-db-name";   
    5. $db [ ' default '][ ' Dbdriver '] =   "MySQL";   

3. Build a table

SOURCE print?
  1. <strong>CREATE TABLE IF not EXISTS ' users ' (
  2. ' ID ' INT (8) not NULL auto_increment,
  3. 'name ' VARCHAR (CHARACTER) SET UTF8 DEFAULT NULL,
  4. ' Age ' VARCHAR (3) CHARACTER SET UTF8 DEFAULT NULL,
  5. ' Sex ' VARCHAR (2) CHARACTER SET UTF8 DEFAULT NULL,
  6. PRIMARY KEY (' id ')
  7. ) Engine=myisam DEFAULT Charset=utf8 collate=utf8_estonian_ci auto_increment=14;
  8. </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?
    1. <?php
    2. Class Mtest extends ci_model{
    3. function Mtest () {
    4. Parent::__construct ();
    5. }
    6. function get_last_ten_entries ()
    7. {
    8. $this->load->database ();
SOURCE print?
    1. mysql_query ("SET NAMES GBK"); //Prevent Chinese garbled characters
    2. $query = $this->db->get (' users ', 10);
    3. return $query->result ();
    4. }
    5. }
    6. ?>

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?
  1. <?php
  2. Class Test extends Ci_controller {
  3. function Test () {
  4. Parent::__construct ();
  5. }
  6. function Index () {
  7. $this->load->helper (' form ');
  8. $data [' title '] = "Home";
  9. $data [' headline '] = "input user Information";
  10. //Multidimensional Arrays
  11. $data [' todo_list '] = Array (' Clean House ', ' call Mom ', ' Run errands ');
  12. //$this->load->vars ($data);
  13. $this->load->model (' mtest ');
  14. $data [' query1 '] = $this->mtest->get_last_ten_entries ();
  15. $this->load->view (' users ',$data);
  16. //$this->load->view (' NewFile ');
  17. //$this->load->view (' a/newfile ');
  18. }
  19. }
  20. ?>

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?
  1. <title><? echo $title;? ></title>
  2. <body>
  3. <ul>
  4. <?php foreach ($todo _list as $item):?>
  5. <li><?php echo $item;? ></li>
  6. <?php Endforeach;? >
  7. </ul>
  8. <ul>
  9. <?  Echo count ($query 1);
  10. foreach ($query 1 as $v 1) {
  11. foreach ($v 1 as $v 2) {
  12. echo "$v 2\n";
  13. }
  14. }
  15. For ($row =0; $row <count ($query 1); $row + +) {
  16. echo $query 1[$row]->name."  </br> ";
  17. }
  18. ?>
  19. <?php foreach ($query 1 as $v):?>
  20. <li><?php echo $v->name;? ></li>
  21. <?php Endforeach;? >
  22. </ul>
  23. echo $headline;?>
  24. </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?
    1. <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?
    1. $db [' Default '] [' char_set '] = ' UTF8 ';  //utf8. The database character set is also UTF8
    2. $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

Related Article

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.