
今回、一番下の前へ戻るを追加してみました。下は前の時です。


ブレークポイントの部分を追加しました。一番最初のメニューへ戻るときは、上から二行目のHOMEをクリックです。

今回、一番下の前へ戻るを追加してみました。下は前の時です。


ブレークポイントの部分を追加しました。一番最初のメニューへ戻るときは、上から二行目のHOMEをクリックです。
|
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 |
<?php require_once("photolibini.php"); session_start(); $tcnt = $_SESSION['tcnt']; $PAGESIZE=1; $photoid = $_GET['photoid']; //print $photoid; $where = " WHERE tblphoto.photoid = $photoid"; //データベース接続 $dsn = "mysql:host=".$DBSERVER.";dbname=".$DBNAME; $dbh = new PDO($dsn, $DBUSER, $DBPASSWORD); //1ページ分だけ抽出するSQL文を組立てる $sql = "SELECT tblphoto.*, categoryname FROM tblphoto RIGHT JOIN tblcategory ON tblphoto.categoryid = tblcategory.categoryid" . $where ; $posts = $dbh->prepare($sql); $posts->execute(); //ページ本文を組み立てます $body = "<BR>"; $body .= "現在表示中のphotid:$photoid"; $showtype = 1; if (!isset($showtype) or $showtype == 1) { //一覧表示の場合 $body .= "<TABLE class='photolist'> <TR> <TH>写真<SPAN class='smallfont'> (クリックで拡大)</SPAN></TH> <TH>カテゴリ</TH> <TH>コメント</TH> <TH>登録日</TH> </TR>"; //結果セットからデータをループで読み込みます while($col = $posts->fetch( PDO::FETCH_ASSOC )) { //各レコード内容を表示する表を組み立てます $body .= "<TR> <TD width='130' align='center'> <A href='$PHOTODIR$col[photofilename]' target='_blank'> <IMG src='$PHOTODIR$col[photofilename]' width='120' height='90'></A> </TD> <TD width='130'>$col[categoryname]</TD>"; //キーワードが指定されているときはコメント内のキーワードを太字に置換します $tmpcomment = $col['comment']; /* if ($searchtype == "kw") { for ($i = 0; $i < sizeof($arykey); $i++) { //$tmpcomment = preg_replace("/" . preg_quote($arykey[$i]) . "/", "", $tmpcomment); } } */ //改行コードをBRタグに置換します $tmpcomment = nl2br($tmpcomment); $body .= "<TD width='410'>$tmpcomment</TD> <TD width='90' align='center'>$col[regdate]</TD> </TR>"; } $body .= "</TABLE>"; } //ページのナビゲーションを追加します $body .= "<DIV class='pagenavi'>"; if ($photoid > 1) { //2ページ以降の場合は[前]を表示します $body .= "<A href =$_SERVER[PHP_SELF]?photoid=" . ($photoid - 1) . "&tcnt=$tcnt'>" . "&lt;前の $PAGESIZE 件</A>&nbsp;&nbsp;&nbsp;"; } //if ($photoid = 1 and $tcnt > 1) { // //2ページ以降の場合は[前]を表示します // $body .= "<A href =$_SERVER[PHP_SELF]?photoid=" . ($photoid + 1) . "&tcnt=$tcnt'>" . // "&lt;次の $PAGESIZE 件</A>&nbsp;&nbsp;&nbsp;"; //} if (($photoid > 1 or $photoid == 1) and $photoid < $tcnt) { //全部で2ページ以上あってかつ現在が最終ページより //前のときは[次]を表示します $body .= "<A href =$_SERVER[PHP_SELF]?photoid=" . ($photoid + 1) . "&tcnt=$tcnt'>" . "次の $PAGESIZE 件&gt;</A>"; } $body .= "</DIV>"; //ページヘッダを出力します print htmlheader("検索結果"); //ページ本文を出力します print $body; //ページフッタを出力します print htmlfooter(); ?> |
比較的簡単に出来ました。search.phpをそのままコピー出来ました。



2件目なので前のデータもありますし、次もあります。ナビの部分です。

1件目なので、前はありません。

125件目をクリックします。

125件目は前も、次もあります。次をクリックします。

最後の126件目なので、前はありますが、次はありません。大体、良いように思います。(^o^)参考のWPブログにコードを張ってあります。

photolibのメニュー。

前は<TABLE>使ってましたが、クリック選択できないので、上の図のように変えました。

onelinedisp.phpでphotoidを受け取りました。10と表示があります。参考のWPブログへリンクしておきます。
|
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 114 115 116 117 118 119 120 |
<?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 件の写真が見つかりました。"; //表示したいphotoidをクリックして下さい。 //<BR><BR> //<UL type='circle'>"; $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 .= "<BR>表示したいphotoidをクリックして下さい。 <BR> <UL type='circle'>"; //$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 .= "<LI><A href='onelinedisp.php?photoid=$result[photoid]'"; $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"; $body .= "</UL>"; // 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 |
以前のものは、<TABLE>使って、表形式で表示してましたが、どうもクリック選択できないので、<TABLE>をしないように、コメントアウトしてます。それ以外は変わってないと思います。
PHPの練習とMySQLの練習で、「PHP5.0+MySQL5.0で構築する最速Webシステム」が大分動いてきたので、多少の変更を加えて、練習してます。サンプルには登録順の表示は、簡単なためか無かったので、自分にはちょうどいい練習問題です。本を読まないで、いきなりコードに入ったので分からないことが山積です。前回でページネイションが大体完成でしたので、クリックでそのデータを表示するをやって見たいと思います。今はonelinedisp.phpでクリックした一行を表示しようと持ってます。photoidを渡すとこ迄出来てますので、search.phpから一行分データを組立てればOKでしょう。データベースからphotoidに一致するデータを見つけるsqlも作る必要だありますね。
ここからは多分の話ですが、前は<TABLE>を使って、表示してましたが、これだとクリック出来ないようです。一行のデータをベタ出力(というかどうか?)していくと、クリックできるので、変えました。
|
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 |

メニューの下2つは、自分が追加したものです。今回は下から二番目をやって見てます。

全体では写真が126件登録になってます。その内1番目が上です。下は2番目。件数が$tcntで表示されるのですが、最初にセットしたら、次回以降はセットした数値を使いたかったのですが、自分自身をよんで、($pageは出来ている)$tcntを受けたかったのですが、上手く行かないので$tcntはスーパーグローバル変数$_SESSIONを使いました。


ブレークポイントのところに注目。session_start();を入れました。




上の図で&tcnt=$tcntはエラーにはなりません。下の3行で二三日悩んでました。(*_*;参考にならないかもしれませんが、SSだとコピー出来ないので、WPブログにアップしてます。
素人が趣味でやっているプログラミング、面白いのですが、問題の発生が多すぎませんか?もう少し次の問題が出ない方が気持ち楽ですけど。今回の謎は、例えば、index.htm(index.phpでも良いようですし、index.htmlでも良いと思います。)のメニューの一部にhrefという文があります。これはこういうものだと覚えるしかない。一行の分と、上の三行(ブログでは表示できない)を同じ目で見れますか?id=2に相当する、&tcnt=$tcntは何処に入れればいいのか??です。?page=は例の通りあります。
ブログにhtmlで使う記号を使うと、文章が乱れます。出来ません。
結論は、自分を呼ぶ場合の変数の渡し方なんです。$pageは出来ていたのですが、$tcntが何故かできない。で仕方ないので$_SESSIONというスーパーグローバル変数を使って一応の解決したように見えます。次回の書き込みで。
|
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 |
上の図で、比較的に長いくないので、どうやれば表示部分を変えていけるでしょうかね。ページネイションの本質はこれで行くとして。