- create table `test` (
- `id` int(10) unsigned not null auto_increment,
- `name` char(100) not null,
- `content` varchar(300) not null,
- primary key (`id`)
- ) engine=myisam default charset=utf8 auto_increment=27 ;
- insert into `test` (`id`, `name`, `content`) values
- (19, '123', '123'),
- (20, '1231', '123123123'),
- (21, '123123', '123123123'),
- (26, '24', '123123'),
- (25, '321123', '321123'),
- (24, 'age', 'age'),
- (23, '123123', '123123'),
- (22, '213', '123');
複製代碼
2,建立一個thinkphp項目。新版tp已經內建了項目自動組建目錄功能。在htdocs(也就是你的網站根目錄)下建立一個test檔案夾,把thinkphp核心檔案夾放進test根目錄,並在test根目錄建立檔案index.php,加入如下代碼:
- // 定義thinkphp架構路徑
- define('think_path', './thinkphp');
- //定義項目名稱和路徑。這2句是重點。
- define('app_name', 'test');
- define('app_path', './test');
- // 載入架構入口檔案
- require(think_path."/thinkphp.php");
- //執行個體化一個網站應用程式執行個體
- $app = new app();
- //應用程式初始化
- $app->run();
複製代碼
運行“http://localhost/test/index.php”.會看到thinkphp的歡迎頁面。再開啟你的test目錄看看,發現在根目錄下多了一個test檔案夾,此時,你的項目目錄已經產生了。
開啟/test/test/conf/目錄,建立“config.php” ,配置好資料庫連接。
- return array(
- 'db_type'=>'mysql',
- 'db_host'=>'localhost',
- 'db_name'=>'test', //建立的資料庫名test
- 'db_user'=>'root', //資料庫使用者名稱
- 'db_pwd'=>'', //資料庫密碼
- 'db_port'=>'3306',
- );
- ?>
複製代碼
如果想開啟偵錯模式,請在數組中加入:"debug_mode"=>true
3,基本頁面輸入與輸出的實現。1)開啟/test/test/lib/action/indexaction.class.php,找到:
- // 本類由系統自動產生,僅供測試用途
- class indexaction extends action{
- public function index(){
- header("content-type:text/html; charset=utf-8");
- echo "^_^ hello,歡迎使用thinkphp";
- }
- }
- ?>
複製代碼
由系統自動產生的indexaction類中的index()函數是預設的首頁調用函數。你可以使用http://localhost/test/index.php或者http://localhost/test/index.php/index來訪問他
2)暫時不管他。首先,需要一個表單提交的頁面。開啟“/test/test/tpl/default/index/”,建立一個檔案add.html.
複製代碼
儲存後,輸入 http://localhost/test/index.php/index/add,你就能看到你新增的頁面了。其中,__url__(url要大寫)被轉換為相應地址/test/index.php/index/.這裡簡單說一下模板和action之間的關係。每一個action,對應的模板是與之名字相同的html檔案。例如index類下的index(),對應default/index/index.html,而add.html,則顯然對應的是index類下的add()。
可以在只有add.html而沒有相應的add()動作情況下,用訪問add()的形式(http://localhost/test/index.php/index/add)來訪問add.html模板。add.html模板下的預留位置會被替換成相應的資料。(指令碼學堂 編輯整理 bbs.it-home.org)
3)從form的“action=__url__/insert”中可以看出,進行表單處理的動作是/test/index.php/index/insert,所以我們得新增insert動作來處理表單提交資料。在此之前,我們還有一件重要的事情要做,那就是新增model檔案。通過model檔案的建立,我們將能在insert動作中使用便捷的方法來操作資料庫了開啟/test/test/lib/model/檔案夾,建立檔案testmodel.class.php.開啟他,輸入並儲存以下代碼
- class testmodel extends model {
- }
- ?>
複製代碼
這是activerecord實現的基本檔案。命名規則是你資料庫中的表後面加model。例如將要使用到的表是test,檔案命名必須是testmodel.class.php,而檔案下的類命名必須是testmodel.接著,回到indexaction.class.php檔案,刪除原來的代碼,加入:
- class indexaction extends action{
- //表單資料添加到資料庫
- public function insert() {
- //執行個體化我們剛才建立的testmodel.
- $test = d('test');
- if ($test->create()) {
- //儲存表單資料就這一步。thinkphp已經全部做完了。
- $test->add();
- $this->redirect();
- }else{
- exit($test->geterror()。'[ 返 回 ]');
- }
- }
- }
複製代碼
4)接下來,需要在indexaction類中增加一個首頁預設顯示動作index()來調用表單資料。
- public function index() {
- //依舊是執行個體化我們建立的對應相應表名的model.這是我們進行快捷表操作的重要關鍵。
- $test = d('test');
- //熟悉這段代碼嗎?計算所有的行數
- $count = $test->count('','id');
- //每頁顯示的行數
- $listrows = '3';
- //需要查詢哪些欄位
- $fields = 'id,name,content';
- //匯入分頁類 /thinkphp/lib/org/util/page.class.php
- import("org.util.page");
- //通過類的建構函式來改變page的參數。$count為總數,$listrows為每一頁的顯示條目。
- $p = new page($count,$listrows);
- //設定查詢參數。具體見“thinkphp/lib/think/core/model.class.php”1731行。
- $list = $test->findall('',$fields,'id desc',$p->firstrow.','.$p->listrows);
- //分頁類做好了。
- $page = $p->show();
- //模板輸出
- $this->assign('list',$list);
- $this->assign('page',$page);
- $this->display();
- }
複製代碼
設定一個模板,在/test/test/tpl/default/index/下建立index.html(因為預設對應了index()。程式中可以直接assign.而不用去指定模板檔案。當然,這是可以配置的。)
- 填寫
- //分頁顯示,這一行
- {$page}
- //資料顯示。下面的參數很快會再進行詳解。它很好理解。
-
姓名:{$vo.name}
-
內容:{$vo.content}
複製代碼
儲存,輸入 http://localhost/test/
以上就是thinkphp製作分頁的方法與執行個體,希望對大家有所協助。 |