[PHP-users 34576] Re: 検索結果の 次へ(Next)、前へ(Previous)ができず困っています。

高橋政利 takahashi @ hoshino-dk.co.jp
2009年 5月 15日 (金) 21:05:30 JST


高橋です。

丸投げっぽい気もしますが、私もこのメーリングリストにお世話になってますの
で、ご参考になれば・・・・・。
ページングはPHPとMySQLの組み合わせですので、MySQLとPHPの両スキルが必要で
す。頑張って下さいね。本を5冊もお持ちであれば、何とかなりますよ。詳しい
ことは、お手持ちの本を熟読して下さい。

データベースですが、詳しいデータベースの構造がよく分かりませんので、推測
が入っています。ご了承下さい。一応、

CREATE TABLE `dive_free_db_`.`dive_free_tbl_` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`time_stamp` TIMESTAMP NOT NULL ,
`card_level` TEXT NOT NULL ,
`tank_number` TEXT NOT NULL ,
`diving_point` TEXT NOT NULL ,
`diver_age` INT NOT NULL ,
`diver_sex` TEXT NOT NULL
) ENGINE = MYISAM;

と仮定しています。以下コードです。


<?
/*
 * ページングサンプル
 */

//変数取得
$card_level = $_REQUEST['card_level'];
//接続情報
$DBSERVER   = "localhost";
$DBUSER     = "root";
$DBPASSWORD = "";
$DBNAME     = "dive_free_db_";
//ページ情報
$current = $_SERVER["PHP_SELF"]; //基本アドレス
$maxRow  = 10; //表示行数
$page    =  0; //ページ番号(初期)
if(isset($_GET['page'])) //ページ番号がURIに含まれていれば
{
  $page = $_GET['page']; //ページ番号の取得(初期設定を上書き)
}
$startRow = $page * $maxRow; //データベースから取得する最初の行
//接続
$con = mysql_pconnect($DBSERVER, $DBUSER, $DBPASSWORD) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("set names sjis"); //文字セット
$selectdb = mysql_select_db($DBNAME, $con); //データベース選択
//データ取得
$query      = sprintf("SELECT * from dive_free_tbl_ where card_level = '%s' ORDER BY id ASC", $card_level); //基本クエリ
$queryLimit = sprintf("%s LIMIT %d, %d", $query, $startRow, $maxRow); //オフセット
$setAll     = mysql_query($query, $con) or die(mysql_error()); //全件抽出
$rowTotal   = mysql_num_rows($setAll); //総レコード数を取得
$set        = mysql_query($queryLimit, $con) or die(mysql_error()); //条件抽出
$row        = mysql_fetch_assoc($set); //結果セットの最初の1行を連想配列で取得
$maxPage    = floor($rowTotal/$maxRow); //最終ページ番号
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
<title>ページングテスト</title>
</head>
<body>
<?php do { ?>
<br />
<table width="580" border="0" cellpadding="2" cellspacing="0" bordercolor="#CCCCCC" bgcolor="#f9f3ff">
  <tr height="25">
    <th width="17%" align="center" bgcolor="#E6E6E6" class="style2">登録日</td>
    <th width="18%" align="center" bgcolor="#E6E6E6" class="style2">Card Level</td>
    <th width="16%" align="center" bgcolor="#E6E6E6" class="style2">タンク本数</td>
    <th width="16%" align="center" bgcolor="#E6E6E6" class="style2">主な潜水地</td>
    <th width="15%" align="center" bgcolor="#E6E6E6" class="style2">年齢</td>
    <th width="15%" align="center" bgcolor="#E6E6E6" class="style2">性別</td>
  </tr>
  <tr>
    <td><font color="#000082"><?php echo $row['time_stamp']; ?></font></td>
    <td><font color="#000082"><?php echo $row['card_level']; ?></font></td>
    <td><font color="#000082"><?php echo $row['tank_number']; ?></font></td>
    <td><font color="#000082"><?php echo $row['diving_point']; ?></font></td>
    <td><font color="#000082"><?php echo $row['diver_age']; ?></font></td>
    <td><font color="#000082"><?php echo $row['diver_sex']; ?></font></td>
  </tr>
</table>
<?php } while ($row = mysql_fetch_assoc($set)); //結果セットの次行を連想配列で取得 ?>
<a href="<?php printf("%s?page=%d", $current, 0); ?>">先頭</a>
<a href="<?php printf("%s?page=%d", $current, max(0, $page-1)); ?>">戻る</a>
<a href="<?php printf("%s?page=%d", $current, min($maxPage, $page+1)); ?>">次へ</a>
<a href="<?php printf("%s?page=%d", $current, $maxPage); ?>">最終</a>
</body>
</html>
<?php mysql_free_result($set); ?>


ちなみに

$card_level = $_REQUEST['card_level'];

は、ページングには何の関係もありません。ただの抽出条件ですよね?
フォームか何かで渡しているのであれば、$_POSTでも取得できます。
今回は、URIにページ番号を仕込んであります。ページ番号をURI($_GET)で取
得して、オフセット値を計算しデータベースより抽出して表示をすれば良いと思
います。そしてページ番号を使用して、新たなリンクを貼ればページングの完成
です。


と。これで宜しかったでしょうか?
勘違いしていたら、スルーして下さい。

ではでは。



PHP-users メーリングリストの案内