pegenation.php
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 |
<?php require_once("photolibini.php"); //データベース接続 $dsn = "mysql:host=".$DBSERVER.";dbname=".$DBNAME; $db = new PDO($dsn, $DBUSER, $DBPASSWORD); // GETで現在のページ数を取得する(未入力の場合は1を挿入) if (isset($_GET['page'])) { $page = (int)$_GET['page']; } else { $page = 1; } // スタートのポジションを計算する if ($page > 1) { // 例:2ページ目の場合は、『(2 × 10) - 10 = 10』 $start = ($page * 10) - 10; } else { $start = 0; } // postsテーブルから10件のデータを取得する $posts = $db->prepare(" SELECT categoryid, comment, photofilename, photoid, regdate FROM tblphoto LIMIT {$start}, 10 "); $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 / 10); //ページヘッダを出力します print htmlheader("PHPテスト"); //ページ本文を出力します print $body; //ページフッタを出力します print htmlfooter(); ?> <?php for ($x=1; $x <= $pagination ; $x++) { ?> <a href="?page=<?php echo $x ?>"><?php echo $x; ?></a> <?php } // End of for |
上の図で、比較的に長いくないので、どうやれば表示部分を変えていけるでしょうかね。ページネイションの本質はこれで行くとして。