Introduction to CI Framework example of the complete implementation of database data _php example

Source: Internet
Author: User
Tags php foreach

This article illustrates the complete implementation of database data for the introduction of CI Framework sample. is for beginners to see, this is the easiest to pass the example. Share to everyone for your reference. The implementation methods are as follows:

1. Download CI framework

2. Configure

database.php configuration:

To set the connection parameter for the database server:

Copy Code 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. Table Building

Copy 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 out a few of your own data

4. Implementing MVC
1) realize m--fetch data
CI under Models a new file mtest.php

Copy 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
$query = $this->db->get (' users ', 10);
return $query->result ();
}
}
?>

Description

Parent::__construct (): Not less
$this->load->database (); must not be less or it will be an error.
You can also implement the automatic connection feature, which will automatically instantiate the database class when each page is loaded. To enable automatic connection, you can add a database to the library array in the following file:
application/config/autoload.php
Otherwise it's going to be written on every page like here.
can also be used

Copy Code code as follows:
$query = $this->db->query (' SELECT * from users ');

This writes to your own SQL

2 implement c--decision to take those data
CI under Controllers a new file test.php
Copy Code code as follows:
<?php
Class Test extends Ci_controller {
function Test () {
Parent::__construct ();
}
function index () {
$this->load->helper (' form ');
$data [' title '] = "Home page";
$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 Code code as follows:
$this->load->model (' mtest ');

Load the model into an array:
Copy Code code as follows:
$data [' query1 '] = $this->mtest->get_last_ten_entries ();

to reprint the array to the page:
Copy Code code as follows:
$this->load->view (' users ', $data);

2 Implement v--page display
CI under the views of a new file user.php

Copy Code code as follows:
<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>
</body>

Description: You can use for and foreach a variety of ways to find the data you want!
Description: If the whole page is garbled, the page head is probably like this.
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

If you are not using the CI connection database, add the following code to the database connection section.
Copy Code code as follows:
mysql_query ("SET NAMES GBK"); Prevent Chinese garbled
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 Code code as follows:
$db [' Default '] [' char_set '] = ' UTF8 ';  UTF8. The database character set is also UTF8
$db [' Default '] [' dbcollat '] = ' utf8_general_ci ';

I hope this article will help you with the study of CI framework programming.

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.