thinkphp執行個體教程之資料分頁

來源:互聯網
上載者:User
  1. create table `test` (
  2. `id` int(10) unsigned not null auto_increment,
  3. `name` char(100) not null,
  4. `content` varchar(300) not null,
  5. primary key (`id`)
  6. ) engine=myisam default charset=utf8 auto_increment=27 ;
  7. insert into `test` (`id`, `name`, `content`) values
  8. (19, '123', '123'),
  9. (20, '1231', '123123123'),
  10. (21, '123123', '123123123'),
  11. (26, '24', '123123'),
  12. (25, '321123', '321123'),
  13. (24, 'age', 'age'),
  14. (23, '123123', '123123'),
  15. (22, '213', '123');
複製代碼

2,建立一個thinkphp項目。新版tp已經內建了項目自動組建目錄功能。在htdocs(也就是你的網站根目錄)下建立一個test檔案夾,把thinkphp核心檔案夾放進test根目錄,並在test根目錄建立檔案index.php,加入如下代碼:

  1. // 定義thinkphp架構路徑
  2. define('think_path', './thinkphp');
  3. //定義項目名稱和路徑。這2句是重點。
  4. define('app_name', 'test');
  5. define('app_path', './test');
  6. // 載入架構入口檔案
  7. require(think_path."/thinkphp.php");
  8. //執行個體化一個網站應用程式執行個體
  9. $app = new app();
  10. //應用程式初始化
  11. $app->run();
複製代碼

運行“http://localhost/test/index.php”.會看到thinkphp的歡迎頁面。再開啟你的test目錄看看,發現在根目錄下多了一個test檔案夾,此時,你的項目目錄已經產生了。

開啟/test/test/conf/目錄,建立“config.php” ,配置好資料庫連接。

  1. return array(
  2. 'db_type'=>'mysql',
  3. 'db_host'=>'localhost',
  4. 'db_name'=>'test', //建立的資料庫名test
  5. 'db_user'=>'root', //資料庫使用者名稱
  6. 'db_pwd'=>'', //資料庫密碼
  7. 'db_port'=>'3306',
  8. );
  9. ?>
複製代碼

如果想開啟偵錯模式,請在數組中加入:"debug_mode"=>true

3,基本頁面輸入與輸出的實現。1)開啟/test/test/lib/action/indexaction.class.php,找到:

  1. // 本類由系統自動產生,僅供測試用途
  2. class indexaction extends action{
  3. public function index(){
  4. header("content-type:text/html; charset=utf-8");
  5. echo "^_^ hello,歡迎使用thinkphp";
  6. }
  7. }
  8. ?>
複製代碼

由系統自動產生的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.開啟他,輸入並儲存以下代碼

  1. class testmodel extends model {
  2. }
  3. ?>
複製代碼

這是activerecord實現的基本檔案。命名規則是你資料庫中的表後面加model。例如將要使用到的表是test,檔案命名必須是testmodel.class.php,而檔案下的類命名必須是testmodel.接著,回到indexaction.class.php檔案,刪除原來的代碼,加入:

  1. class indexaction extends action{
  2. //表單資料添加到資料庫
  3. public function insert() {
  4. //執行個體化我們剛才建立的testmodel.
  5. $test = d('test');
  6. if ($test->create()) {
  7. //儲存表單資料就這一步。thinkphp已經全部做完了。
  8. $test->add();
  9. $this->redirect();
  10. }else{
  11. exit($test->geterror()。'[ 返 回 ]');
  12. }
  13. }
  14. }
複製代碼

4)接下來,需要在indexaction類中增加一個首頁預設顯示動作index()來調用表單資料。

  1. public function index() {
  2. //依舊是執行個體化我們建立的對應相應表名的model.這是我們進行快捷表操作的重要關鍵。
  3. $test = d('test');
  4. //熟悉這段代碼嗎?計算所有的行數
  5. $count = $test->count('','id');
  6. //每頁顯示的行數
  7. $listrows = '3';
  8. //需要查詢哪些欄位
  9. $fields = 'id,name,content';
  10. //匯入分頁類 /thinkphp/lib/org/util/page.class.php
  11. import("org.util.page");
  12. //通過類的建構函式來改變page的參數。$count為總數,$listrows為每一頁的顯示條目。
  13. $p = new page($count,$listrows);
  14. //設定查詢參數。具體見“thinkphp/lib/think/core/model.class.php”1731行。
  15. $list = $test->findall('',$fields,'id desc',$p->firstrow.','.$p->listrows);
  16. //分頁類做好了。
  17. $page = $p->show();
  18. //模板輸出
  19. $this->assign('list',$list);
  20. $this->assign('page',$page);
  21. $this->display();
  22. }
複製代碼

設定一個模板,在/test/test/tpl/default/index/下建立index.html(因為預設對應了index()。程式中可以直接assign.而不用去指定模板檔案。當然,這是可以配置的。)

  1. 填寫
  2. //分頁顯示,這一行
  3. {$page}
  4. //資料顯示。下面的參數很快會再進行詳解。它很好理解。
  5. 姓名:{$vo.name}

  6. 內容:{$vo.content}

複製代碼

儲存,輸入 http://localhost/test/

以上就是thinkphp製作分頁的方法與執行個體,希望對大家有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.