_php example of the complete implementation method of database fetching data for the introduction of CI framework

Source: Internet
Author: User
Tags php foreach
In this paper, a complete implementation method of database fetching data for the introduction of CI framework is described. is for beginners to see, this is the simplest can be adjusted to the example. Share to everyone for your reference. The implementation method is as follows:

1. Download the CI framework

2. Configuration

database.php configuration:

To set the connection parameter for the database server:
Copy the code as follows: $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

Copy the Code code as follows: 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;
Fill in a few data yourself

4. Implementing MVC
1) Implement m--data collection
Create a new file under CI models mtest.php
Copy the Code code as follows: <?php
Class Mtest extends ci_model{
function Mtest () {
Parent::__construct ();
}
function Get_last_ten_entries ()
{
$this->load->database ();
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 auto Connect, add database to the library array in the following file:
application/config/autoload.php
Otherwise write on each page as it is here.
can also be usedCopy CodeThe code is as follows: $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
Copy CodeThe code is as follows: <?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:Copy CodeThe code is as follows: $this->load->model (' mtest ');
Load the model into the array:Copy CodeThe code is as follows: $data [' query1 '] = $this->mtest->get_last_ten_entries ();
Reprint the array to the page:Copy CodeThe code is as follows: $this->load->view (' users ', $data);

2) Implement v--page display
CI Views under Create a new file user.php

Copy the Code code as follows:
<? echo $title;? >



      <?php foreach ($todo _list as $item):?>
    • <?php echo $item;? >

    • <?php Endforeach;? >


      <? echo Count ($query 1);
      foreach ($query 1 as $v 1) {
      foreach ($v 1 as $v 2) {
      echo "$v 2\n";
      }
      }
      for ($row =0; $row
      echo $query 1[$row]->name. "
      ";
      }
      ?>

      <?php foreach ($query 1 as $v):?>
    • <?php echo $v->name;? >

    • <?php Endforeach;? >

<?php echo $headline;?>


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.
Copy CodeThe code is as follows:
If you are not using CI to connect to the database, add the following code to the database connection section.
Copy CodeThe code is as follows: 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
Copy CodeThe code is as follows: $db [' Default '] [' char_set '] = ' UTF8 '; UTF8. The database character set is also UTF8
$db [' Default '] [' dbcollat '] = ' utf8_general_ci ';

We hope that this article is helpful to the study of CI Framework program design.

  • 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.