カテゴリー
PHP

photolib弄ってます。

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

inserted by FC2 system