測試

http://ellislab.com/codeigniter

http://www.codeigniter.org.tw/
回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

測試

文章 yehlu »

1.資料庫設定
/config/database.php

代碼: 選擇全部

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test';
$db['default']['dbdriver'] = 'mysql';

2.自動載入db 物件
/config/autoload.php

代碼: 選擇全部

$autoload['libraries'] = array('database');
3.建立 Control
/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
      }
}
?>
4.建立Model
/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;
      }
}
?>
6.建立 view
/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>

回覆文章

回到「CodeIgniter」