MySQLのデータ検索結果をページング処理で複数のページで表示させたいのですが、2ページ目以降表示されません。
ページャーリンク部分に問題があるのでしょうか?
お手数ですがご指導願います。
<?php session_start(); ?> <!DOCTYPE html> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html;charset=utf-8"> </HEAD> <BODY> <?php require_once("MYDB.php"); $pdo=db_connect(); try{ // GETで現在のページ数を取得 if (isset($_GET['page'])) { $page = (int)$_GET['page']; } else { $page = 1; } // スタートのポジションを計算 if ($page > 1) { $start = ($page * 10) - 10; } else { $start = 0; } //POSTデータを取得 $search_key = "{$_POST['search_key']}"; $searchSelect ="{$_POST['searchSelect']}"; $search_key = preg_replace("/[ ]+/", " ", $search_key); $search_key = trim($search_key); //検索条件 and or $array = explode(" ", $search_key); $cnt = count($array); $where = "WHERE "; for($i=0; $i < $cnt; $i++){ $where.= "type LIKE '%".$array[$i]."%'"; if($i < $cnt-1){ $where .=" ".$searchSelect." "; } } $sql = "SELECT DISTINCT `goods` FROM `list`".$where; $sql.= "ORDER BY `goods` LIMIT {$start}, 10"; $sth = $pdo->prepare($sql); $sth ->execute(); // ページ数を取得 $sql2 = "SELECT DISTINCT `goods` FROM `list`".$where; $stmh = $pdo->prepare($sql2); $stmh ->execute(); $count = $stmh->rowCount(); $totalpage = ceil($count / 10); }catch(PDOException $Exception){ print"error:".$Exception->getMessage(); } if($count <1){ print"<BR>検索結果はありません"; }else{ ?> <TABLE> <TBODY> <?php while($row = $sth ->fetch(PDO::FETCH_ASSOC)){ ?> <TR> <TD> <?=htmlspecialchars($row['goods'])?> </TD> </TR> <?php } ?> </TBODY> </TABLE> <?php } ?> <?php for ($i=1; $x <= $totalpage ; $i++) { ?> <a href="?page=<?php echo $i ?>"><?php echo $i; ?></a> <?php } // End of for ?> </BODY> </HTML>
補足情報
MySQL5.5
PHP5.6.22
回答2件
あなたの回答
tips
プレビュー