photolib弄ってます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
<?php session_start(); require_once("photolibini.php"); //1ページ当りの表示件数を設定します $PAGESIZE = 10; //データベース接続 $dsn = "mysql:host=".$DBSERVER.";dbname=".$DBNAME; $db = new PDO($dsn, $DBUSER, $DBPASSWORD); // GETで現在のページ数を取得する(未入力の場合は1を挿入) if (!isset($_GET['page'])) { //初めて呼ばれたときは、総件数を取得 $sql = "SELECT Count(*) AS cnt FROM tblphoto"; $posts = $db->prepare($sql); $posts->execute(); $tcnt = $posts->fetchColumn(); //件数を知る $totalpage = ceil($tcnt / $PAGESIZE); if ($totalpage == 0) { $body = "該当する写真はみつかりませんでした! <INPUT type='button' value='ホームへ戻る' onclick='window.location=\"index.php\"'>"; print htmlheader("検索結果") . $body . htmlfooter(); exit(); } $page = 1; //ページ上部の表示組立て $_SESSION['tcnt'] =$tcnt; $body = "$tcnt 件の写真が見つかりました。"; $body .= "[" . ($PAGESIZE * ($page - 1) + 1) . "-"; if($page < $totalpage) { //最終ページより前のページの時 $body .=($PAGESIZE * $page) . "]を表示"; } else{ //最終ページの時 $body .= "$tcnt]を表示"; } }else{ $page =(int)$_GET['page']; //$tcnt =(int)$_GET['tcnt']; $tcnt =(int)$_SESSION['tcnt']; $totalpage = ceil($tcnt / $PAGESIZE); $body = "$tcnt 件の写真が見つかりました。"; $body .= "[" . ($PAGESIZE * ($page - 1) + 1) . "-"; if($page < $totalpage) { //最終ページより前のページの時 $body .=($PAGESIZE * $page) . "]を表示"; } else{ //最終ページの時 $body .= "$tcnt]を表示"; } } // スタートのポジションを計算する if ($page > 1) { // 例:2ページ目の場合は、『(2 × 10) - 10 = 10』 $start = ($page * $PAGESIZE) - $PAGESIZE; } else { $start = 0; } // postsテーブルから10件のデータを取得する $posts = $db->prepare(" SELECT categoryid, comment, photofilename, photoid, regdate FROM tblphoto LIMIT {$start}, $PAGESIZE "); $posts->execute(); $body .= "<table>\n"; $body .= "\t<tr><th>photoid</th><th>photofilename</th><th>categoryid</th> <th>comment</th><th>regdate</th><tr>"; while($result = $posts->fetch(PDO::FETCH_ASSOC)){ $body .= "\t<tr>\n"; $body .= "\t\t<td>{$result['photoid']}</td>\n"; $body .= "\t\t<td>{$result['photofilename']}</td>\n"; $body .= "\t\t<td>{$result['categoryid']}</td>\n"; $body .= "\t\t<td>{$result['comment']}</td>\n"; $body .= "\t\t<td>{$result['regdate']}</td>\n"; $body .= "\t<tr>\n"; } $body .= "</table>\n"; // postsテーブルのデータ件数を取得する $page_num = $db->prepare(" SELECT COUNT(*) categoryid FROM tblphoto "); $page_num->execute(); $page_num = $page_num->fetchColumn(); // ページネーションの数を取得する $pagination = ceil($page_num / $PAGESIZE); // $pagination = $totalpage; //ページヘッダを出力します print htmlheader("PHPテスト"); //ページ本文を出力します print $body; //ページフッタを出力します print htmlfooter(); ?> <?php for ($x=1; $x <= $pagination ; $x++) { ?> <a href="?page=<?php echo $x ?>&tcnt=$tcnt"><?php echo $x; ?></a> <?php } // End of for |