カテゴリー
PHP

pegenation.php

<?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

上の図で、比較的に長いくないので、どうやれば表示部分を変えていけるでしょうかね。ページネイションの本質はこれで行くとして。

inserted by FC2 system