kawama.jp

PEARのPagerクラスを使う

Filed under: PHP — 2006年11月7日 11:30 — Comments (0)hatebuhatebu count

「前へ 1 2 3 4 5 6 7 8 9 10 次へ」

↑こういう感じのページナビゲーション、いわゆるページング処理を手軽に実装できるのがPEAR::Pagerです。実際使ってみたんですが、かなり便利です。

基本的な使い方は「pear pager」などでググると有用なページがいくつも見つかるのでそちらを参考にするといいと思います。というよりもむしろ実際に試して体で覚えるのがいいと思います。すごく簡単なので。

ちなみにこのページング処理、検索エンジン対策としてPATH_INFOにも対応できるといいな、、、って思っていたら、やってる方がいました。

http://project-p.jp/halt/anubis/blog_show/469

こちらを参考に実装してみました。

<?php



/*** pager.php ***/



//PATH_INFOから現在のページを取得

$tpath = explode(“/”, $_SERVER['PATH_INFO']);

array_shift($tpath);

if($tpath[0]){ $current_page = $tpath[0]; }else{ $current_page = 1; }





require(‘Pager/Pager.php’);

require(‘Smarty/Smarty.class.php’);



$smarty = new Smarty;

$smarty->template_dir = ‘./smarty/template’;

$smarty->compile_dir  = ‘./smarty/template_c’;





//ページング処理

$params = array(

    ”perPage” => 5,

    ”totalItems” => 100,

    ’mode’ => ‘Sliding’,

    ’delta’ => 2,

    ’path’ => ‘http://localhost/pager/’,

    ’currentPage’ => $current_page,

    ’fileName’ => ‘%d’,

    ’append’ => false,

    ’altPrev’ => ‘前のページ’,

    ’altNext’ => ‘次のページ’,

    ’altPage’ => ‘ページ’,

    ’firstPageText’ => ‘最初’,

    ’altFirst’ => ‘最初’,

    ’lastPageText’ => ‘最後’,

    ’altLast’ => ‘最後’,

    ’extraVars’ => array(“a” => 1),

    ’showAllText’ => ‘show all’,

    ’importQuery’ => true

    );

$pager = Pager::factory($params);

$navi = $pager->getLinks();

$smarty->assign(“pageNavi”,$navi['all']);



$smarty->display(“pager.tpl”);



?>

{** pager.tpl **}



<html>

<body>



{$pageNavi}



</body>

</html>

実は extraVars とか、いまいち動作がよく分からない変数があるんですが、その辺はあまり深く追求せずに使ってます。こういうのは自作しないでライブラリ使ったほうが絶対いいので、一度試してみる価値はあると思います。

コメントはまだありません »

コメントはまだありません。

この投稿へのコメントの RSS フィード。 TrackBack URL

コメントする

Copyright (C) 2002 - 2010 kawama.jp. All Rights Reserved. — Powered by WordPress