/config/database.php
代碼: 選擇全部
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test';
$db['default']['dbdriver'] = 'mysql';
/config/autoload.php
代碼: 選擇全部
$autoload['libraries'] = array('database');
/controllers/student.php
代碼: 選擇全部
<?php
class student extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data['title'] = "頁面標題";
$data['heading'] = "CI2測試中";
//$data['query'] = $this->db->get('student'); // A
$this->load->model('student_model'); // B
$data['query'] = $this->student_model->getdata(); // C
$this->load->view('student_view', $data); // D
}
}
?>
/models/student_model.php
代碼: 選擇全部
<?php
class student_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getdata()
{
//$query = $this->db->get('student');
$query = $this->db->query('SELECT * FROM student LIMIT 1,10');
return $query;
}
}
?>
/views/student_view.php
代碼: 選擇全部
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php
//輸出從Controller傳來的heading變數
echo '<h1>'.$heading.'</h1>';
//輸出透過Controller及Model的處理,從DB取出的資料
foreach($query->result() as $row)
{
echo '<h3>'.$row->name.'</h3>'; // 讀出title欄位的資料
echo $row->name; // 讀出body欄位的資料
echo '<hr />';
}
?>
</body>
</html>