欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ThinkPHP快速入門實(shí)例教程之?dāng)?shù)據(jù)分頁

 更新時間:2014年07月01日 16:34:26   投稿:shichen2014  
這篇文章主要介紹了ThinkPHP快速入門實(shí)例教程的數(shù)據(jù)分頁實(shí)現(xiàn)過程,需要的朋友可以參考下

數(shù)據(jù)分頁可能是web編程里最常用到的功能之一。ThinkPHP實(shí)現(xiàn)分頁功能十分簡潔。只需要定義幾個參數(shù)就可以實(shí)現(xiàn)。并且擴(kuò)展也十分方便。

下面讓我們從零開始實(shí)現(xiàn)ThinkPHP的分頁程序吧。

1.首先,我們得創(chuàng)建一個用于分頁測試的數(shù)據(jù)庫 test.sql代碼如下。

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項(xiàng)目。新版tp已經(jīng)內(nèi)置了項(xiàng)目自動生成目錄功能。
在htdocs(也就是你的網(wǎng)站根目錄)下新建一個test文件夾,把THINKPHP核心文件夾放進(jìn)test根目錄,并在test根目錄新建文件index.php,加入如下代碼:

// 定義ThinkPHP框架路徑
define('THINK_PATH', './Thinkphp');
//定義項(xiàng)目名稱和路徑。這2句是重點(diǎn)。
define('APP_NAME', 'test');
define('APP_PATH', './test');
// 加載框架入口文件
require(THINK_PATH."/ThinkPHP.php");
//實(shí)例化一個網(wǎng)站應(yīng)用實(shí)例
$App = new App();
//應(yīng)用程序初始化
$App->run();

運(yùn)行“http://localhost/test/index.php”.會看到ThinkPHP的歡迎頁面。再打開你的test目錄看看,發(fā)現(xiàn)在根目錄下多了一個test文件夾,此時,你的項(xiàng)目目錄已經(jīng)生成了。
打開/test/test/conf/目錄,新建“config.php” ,配置好你的數(shù)據(jù)庫連接。

<?php
return array(
'DB_TYPE'=>'mysql',
'DB_HOST'=>'localhost',
'DB_NAME'=>'test', //新建的數(shù)據(jù)庫名test
'DB_USER'=>'root', //數(shù)據(jù)庫用戶名
'DB_PWD'=>'', //數(shù)據(jù)庫密碼
'DB_PORT'=>'3306',
);
?>

如果你想打開調(diào)試模式,請在數(shù)組中加入

"debug_mode"=>true

3.基本頁面輸入與輸出的實(shí)現(xiàn)。
(1)打開/test/test/lib/action/IndexAction.class.php,會發(fā)現(xiàn)以下代碼

<?php
// 本類由系統(tǒng)自動生成,僅供測試用途
class IndexAction extends Action{
public function index(){
header("Content-Type:text/html; charset=utf-8");
echo "<div style='font-weight:normal;color:blue;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;font-size:14px;font-family:Tahoma'>^_^ Hello,歡迎使用<span style='font-weight:bold;color:red'>ThinkPHP</span></div>";
}
}
?>

由系統(tǒng)自動生成的indexaction類中的index()函數(shù)是默認(rèn)的首頁調(diào)用函數(shù)。你可以使用http://localhost/test/index.php或者h(yuǎn)ttp://localhost/test/index.php/index來訪問他

(2)我們暫時不管他。首先我們需要一個表單提交的頁面。打開“/test/test/tpl/default/index/”,新建一個文件add.html.

<form method="post" action="__URL__/insert">
<p>姓名:<input name="name" type="text" ></p>
<p>內(nèi)容:<input name="content" type="text"></p>
<p>提交:<input type="submit" value="submit"></p>
</form>

保存后,輸入 http://localhost/test/index.php/index/add,你就能看到你新增的頁面了。其中,__URL__(url要大寫)被轉(zhuǎn)換為相應(yīng)地址/test/index.php/Index/.
這里簡單說一下模板和action之間的關(guān)系。每一個action,對應(yīng)的模板是與之名字相同的html文件。例如index類下的index(),對應(yīng)default/index/index.html,而add.html,則顯然對應(yīng)的是index類下的add()。
我們甚至可以在只有add.html而沒有相應(yīng)的add()動作情況下,用訪問add()的形式(http://localhost/test/index.php/index/add)來訪問add.html模板。add.html模板下的占位符會被替換成相應(yīng)的數(shù)據(jù)。效果如下。

(3)從form的“action=__URL__/insert”中可以看出,進(jìn)行表單處理的動作是/test/index.php/index/insert,所以我們得新增insert動作來處理表單提交數(shù)據(jù)。在此之前,我們還有一件重要的事情要做,那就是新增model文件。通過model文件的建立,我們將能在insert動作中使用便捷的方法來操作數(shù)據(jù)庫了
打開/test/test/lib/model/文件夾,新建文件TestModel.class.php.打開他,輸入并保存以下代碼

<?php
class TestModel extends Model {
}
?>

簡單的說,這是ActiveRecord實(shí)現(xiàn)的基本文件。命名規(guī)則是你數(shù)據(jù)庫中的表后面加Model.例如我們將要使用到的表是test,我的文件命名必須是TestModel.class.php,而文件下的類命名必須是TestModel.

接著,我們回到indexaction.class.php文件,刪除原來的代碼,加入如下代碼。

class IndexAction extends Action{
//表單數(shù)據(jù)添加到數(shù)據(jù)庫
public function insert() {
//實(shí)例化我們剛才新建的testmodel.
$test = D('Test');
if ($test->create()) {
//保存表單數(shù)據(jù)就這一步。thinkphp已經(jīng)全部做完了。
$test->add();
$this->redirect();
}else{
exit($test->getError()。'[ <A HREF="javascript:history.back()">返 回</A> ]');
}
}
}

(4)接下來,我們需要在IndexAction類中增加一個首頁默認(rèn)顯示動作index()來調(diào)用表單數(shù)據(jù)。

public function index() {
//依舊是實(shí)例化我們新建的對應(yīng)相應(yīng)表名的model.這是我們進(jìn)行快捷表操作的重要關(guān)鍵。
$test = D('Test');
//熟悉這段代碼么?計算所有的行數(shù)
$count = $test->count('','id');
//每頁顯示的行數(shù)
$listRows = '3';
//需要查詢哪些字段
$fields = 'id,name,content';
//導(dǎo)入分頁類 /ThinkPHP/lib/ORG/Util/Page.class.php
import("ORG.Util.Page");
//通過類的構(gòu)造函數(shù)來改變page的參數(shù)。$count為總數(shù),$listrows為每一頁的顯示條目。
$p = new Page($count,$listRows);
//設(shè)置查詢參數(shù)。具體見“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();
}

我們該設(shè)置一個模板了。在/test/test/tpl/default/index/下新建index.html(因?yàn)槟J(rèn)對應(yīng)了index()。所以程序中可以直接assign.而不用去指定模板文件。當(dāng)然,這是可以配置的。)

<hr><a href="__URL__/add">填寫</a>
//分頁顯示,這一行
<hr>{$page}<hr>
//數(shù)據(jù)顯示。下面的參數(shù)很快會再進(jìn)行詳解。它很好理解。
<volist name="list" id="vo">
<p>姓名:{$vo.name}</p>
<p>內(nèi)容:{$vo.content}</p>
<hr>
</volist>

保存他。接著輸入 http://localhost/test/
恭喜你。你已經(jīng)學(xué)會了如何利用thinkphp制作分頁了!

相關(guān)文章

最新評論