header image

Ryuzeeの他サイト

mixi Twitter Twitter

Categories

携帯対応

QRコード

RING

人気ブログランキング



新着記事

インストール

簡単すぎる。rootアカウントで

pear install Pager

結果、以下のように表示されれば良い。

install ok: channel://pear.php.net/Pager-2.4.3

使い方

こちらも簡単だ。以下にサンプルソースを丸ごと掲載しておく。pager_sample.php

<?php
$lib = "/usr/share/pear";
set_include_path($lib . PATH_SEPARATOR . get_include_path());

require_once("DB.php");
require_once("Pager/Pager.php");
require_once("Smarty/Smarty.class.php");

//データベースからデータを取得する
$datasource = "pgsql://postgres:@localhost/bugtracker";
$db = DB::connect($datasource );
$sql = "select * from mantis_bug_text_table order by id asc;";
$res = $db->query($sql);

//データベースから取得したデータを配列に入れる
while ($row = $res->fetchRow( DB_FETCHMODE_ASSOC )) {
    $data_list[]array(
        "id"=>$row["id"],
        "description"=>$row["description"]
    );
}

//データベース後処理
$res->free();
$db->disconnect();

//Pagerへのデータ割り当て処理
$perPage=10;
$params=array("perPage"=>$perPage, "itemData"=>$data_list);
$o_page=Pager::factory($params);
foreach($o_page->getPageData() as $item){
    $data_list_for_page[]=$item;
}

//Smartyへの割付処理
$smarty=new Smarty;
$smarty->template_dir = "./templates";
$smarty->compile_dir = "./templates_c";
$smarty->cache_dir = "./cache";
$smarty->assign("list", $data_list_for_page);
$navi=$o_page->getLinks();
$smarty->assign("pageNavi", $navi['all']);
$smarty->display("pager_sample.tpl");

?>

簡単な流れであるが、

  1. データを配列にぶち込む
  2. 1ページに表示するアイテム数を決める
  3. pageIDによってテンプレートに割り付けるデータが変わるので、その割り当て処理
  4. あとはSmartyへ割付

Smartyのテンプレートも簡単だ。pager_sample.tpl

<html>
<head>
<title>Pear::Pager Sample</title>
</head>
<body>
{$pageNavi}
<br />
<br />
<table border="1" cellpadding="0" cellspacing="0">
{foreach from=$list item="list" name="listLoop"}
<tr>
<td>{$list.id}</td><td>{$list.description}</td>
</tr>
{/foreach}
</table>
<br />
{$pageNavi}
</body>
</html>

以上で完了だ。細かい処理は省いているが適宜追加すれば良い。

なお、今回のサンプルではデータベースにバグトラッキングシステムのmantisのテーブルを使った。

Ratings

1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.5 out of 5)
Loading ... Loading ...

“Pear::pagerでページングする”へ3件のコメントがあります。

  • ミルキーぱぱ 2007/09/13

    大変参考になりました。ありがとうございました。

  • Ryuzee 2007/09/14

    >ミルキーぱぱさん

    どういたしまして。
    昔のブログの移行で醜いままだったので、ソースなどハイライト表示に変えました。

  • さかい 2008/08/17

    大変参考にさせて頂きました。
    ありがとうございました!

コメントする

XHTML: 以下のタグが利用可能です: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback

 

add to hatena hatena.comment (0) add to del.icio.us (0) add to livedoor.clip (0) add to Yahoo!Bookmark (0) Total: 0