PEAR::Pager と Smarty でページング
もはや愛読書となっている
Amazon.co.jp: PEAR入門 PHP標準ライブラリを極める!: 山田 祥寛: 本
Amazon.co.jp: Smarty入門‾PHP+テンプレート・エンジンでつくるMVCアプリケーション‾: 山田 祥寛: 本
とかマニュアルを読んでそれぞれの機能は何となーく理解できる様になってきましたが PEAR と Smarty を連携させる解説書やサイトがあんまりなかったりするのね。ふたつとも利用者は増えてるんだと思うんだけどなぁ。そんな訳で簡単ではありますが PEAR::Pager と Smarty でページング。
list.php
< ?php //ライブラリとSmartyの読み込み require_once("Pager/Pager.php"); require_once("Smarty.class.php"); $smarty=new Smarty; //例えばDBからデータを取得して$listに入れる //while($row=$rsList->fetchRow(DB_FETCHMODE_ASSOC)){ //$list[]=array("name"=>$row['name'],"tel"=>$row['tel']); } //とか。そこらへんの処理は略 //ページ表示 $perPage=10; $params=array("perPage"=>$perPage, "itemData"=>$list); $o_page=Pager::factory($params); foreach($o_page->getPageData() as $listData){ $listDataList[]=$listData; } //表示するデータをSmartyに渡す $smarty->assign("list",$listDataList); //ナビゲーションの作成 $navi=$o_page->getLinks(); $smarty->assign("pageNavi",$navi['all']); //Smarty設定 $smarty->template_dir = "./templates"; $smarty->compile_dir = "./templates_c"; $smarty->cache_dir = "./cache"; //フォーム表示 $smarty->display("list.tpl"); ?>
list.tpl
<html><body> <ul> {foreach from=$list item="list" name="listLoop"} <li>{$list.name}{$list.tel}</li> {/foreach} </ul> {$pageNavi} </body></html>
うむ。意外と簡素。