CodeIgniter 新手配置用户指南-数据库配置篇
前沿:公司使用的CI框架,以前上学的时候没有接触过,对国外的优秀框架,几乎都没了解过,(英语是硬伤)。最近在接触学习CI框架,把学习成果,分享下:
1.下载最新的CI框架:
http://codeigniter.org.cn/download
2.解压到web根目录,
/Users/jason/project/ci/
3.目录文件列表
Ray-2:ci ray$ ll
total 24
drwxr-xr-x@ 7 ray staff 238 10 8 2012 .
drwxr-xr-x 7 root admin 238 6 14 09:45 ..
drwxr-xr-x@ 17 ray staff 578 10 8 2012 application
-rw-r--r--@ 1 ray staff 6357 10 8 2012 index.php
-rw-r--r--@ 1 ray staff 2496 10 8 2012 license.txt
drwxr-xr-x@ 10 ray staff 340 10 8 2012 system
drwxr-xr-x@ 17 ray staff 578 10 8 2012 user_guide
4.配置虚拟主机、域名映射本机IP
5.通过浏览器访问域名:
Welcome to CodeIgniter!
The page you are looking at is being generated dynamically by CodeIgniter.
If you would like to edit this page you'll find it located at:
application/views/welcome_message.php
The corresponding controller for this page is found at:
application/controllers/welcome.php
If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '123456';
$db['default']['database'] = 'myci';
$db['default']['dbdriver'] = 'mysql';
+++++++++++ 前提 数据库的地址、用户名、密码、数据库都是已存在且正确的,真是环境不要使用管理员链接数据库即可 ++++++++++++++++++++
8.创建model层的文件
/Users/jason/project/ci/application/models
vim testModel.php
<?php
class testModel extends CI_Model{
public $db;
public function __construct(){
$this->db = $this->load->database('default', TRUE);
// parent::__construct();
}
public function Insertdata($name){
return $this->db->insert('test',array('name'=>$name));
}
public function getdata(){
return $this->db->query('select * from test limit 20')->result_array();
}
}
?>
注意:model文件的名字要和类名相同。
9.修改controller层的文件
Ray-2:controllers ray$ ls
index.html welcome.php
Ray-2:controllers ray$ pwd
/Users/jason/project/ci/application/controllers
Ray-2:controllers ray$vim welcome.php
-------------------------------------
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function __construct(){
parent::__construct();
$this->load->model('testModel');
}
public function index()
{
// $this->load->model('testmodel');
$data=$this->testModel->getdata();
$this->load->view('welcome_message',array('data'=>$data));
}
public function insert($name){
// $this->load->model('testmodel');
$this->testModel->Insertdata($name);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
10.修改view层的文件
Ray-2:views ray$ ls
index.html welcome_message.php
Ray-2:views ray$ pwd
/Users/jason/project/ci/application/views
在默认的试图文件里面添加一下内容
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
<div>
<?php echo '<pre>';var_dump($data);?>
</div>
</body>
</html>
11.数据库结构
test 表
字段:
id int 主键,自增
name varchar 64
12.添加测试数据
在浏览器输入 http://www.myci.com/index.php/welcome/insert/jason 回车即可
13 查看数据是否插入成功?
在浏览器输入 http://www.myci.com/ 回车。
结果已成功插入。 OK