[PHP] 認識 CodeIgniter 的初次練習
第一次安裝codeigniter,我把他安裝在localhost/底下的codeigniter資料夾內
網址輸入http://localhost/codeigniter/,應該可以看到這種畫面
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.
*目前看到的頁面在 application/views/welcome_message.php
*控制的流程在application/controllers/welcome.php中
——————————————————————————————————————–
1.先到application/views/welcome_message.php 會看到許多的html與css
身為PHP 程式設計師應該非常的熟悉吧,
至於讓welcome_message.php出現的控制器則在 welcome.php 中
2.所以我們來看application/controllers/welcome.php制訂的格式
[php]
<!–?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);<br ?——>
//Welcome對應檔案名稱welcome.php,但規則是字首要大寫喔
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
*/
//取名index,要做的事情就是讀取view裡的welcome_message.php
//.php可省略,view()裡面可以是路徑喔,可以試試看
public function index()
{
$this->load->view(‘welcome_message’);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
[/php]
因此,網址的長相應該是:http://localhost/codeigniter/index.php/welcome/index/
我這裡分節說明一下:
– 安裝在http://localhost/codeigniter/ 底下
– welcome 是你的class名稱(Welcome),也是你的控制器網頁名稱(welcome.php)
– index 是你 class Welcome中的一個function 叫做index,
裡面的程式碼動作是呼叫view/welcome_message.php 的網頁。
——————————————————————————————————————–
我們自己舉一個例子好了!
假設我們想用資料夾(或linux的路徑)包起來,而且又夾帶參數……
——————————————————————————————————————–
1.新增 application/controllers/user.php
[php]
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
class User extends CI_Controller {
public $str = "HEellow";
public function datalist()
{
/* — 範例1 (夾帶參數請使用陣列方式) — */
//<title>標籤文字
$ary[‘page’][‘title’] = "把參數傳遞到view的範例";
//假設有多筆資料來供view做迴圈的動作
$ary[‘user’][0][‘name’] = "張先生";
$ary[‘user’][0][‘address’] = "高雄市XXXXXXXXXX";
$ary[‘user’][1][‘name’] = "王小姐";
$ary[‘user’][1][‘address’] = "台中市XXXXXXXXXX";
$ary[‘user’][2][‘name’] = "王小姐";
$ary[‘user’][2][‘address’] = "台北市XXXXXXXXXX";
//最後參數放進view()吧
$this->load->view(‘user/datalist.php’, $ary);
}
}
[/php]
2.新增 views/user/datalist.php
[php]
<?
/*
* 控制器中傳遞的參數形式為 $ary[‘page’][‘title’] ,但系統已經做了對應手續 $XXXX = $ary[‘XXXX’]; $YYYY = $ary[‘YYYY’];
* 所以我們輸出時,只要打$page[‘title’]就好了喔
*/
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?=$page[‘title’] // 直接用了吧 ?></title>
<style>
.container .user { background:#85BEA6; border-radius: 5px; padding:10px; margin:5px; color:white; font-weight:600}
</style>
</head>
<body>
<div class="container">
<?
//還有這裡的$user在控制器的時候,原本是$ary[‘user’]喔
if (is_array($user)) {
foreach ($user as $val) {
?>
<div class="user">
<div class="name"><?=$val[‘name’]?></div>
<div class="address"><?=$val[‘address’]?></div>
</div>
<?
}
}
else {
?><div>沒有任何資料喔!</div><?
}
?>
</div>
</body>
</html>
[/php]
3.去網址打上http://localhost/codeigniter/index.php/user/datalist 應該會看到: