あーる学習帳

自分が勉強したことや気になることなど、99%自分用です。コードを書いてるのでPCから閲覧を推奨。

PHP開発日記:GETで検索(全件表示)

今夜は昨日作ったaddressテーブルからGETでデータを取得する部分を作成しました。

index.phpからコントローラであるsearch.phpに飛ばし、そこからフォームや結果を表示するためのsearch_view.phpに飛ぶ構成です。

 

search.php

<?php
 //設定&関数読み込み
 require_once('config.php');
 require_once('./helper/db_helper.php');
 require_once('./helper/extra_helper.php');

 if (isset($_GET['addno']) or isset($_GET['add1']) or isset($_GET['add2']) or isset($_GET['add3'])) {
  $addno=$_GET['addno'];
  $addno=html_escape($addno);
  $add1=$_GET['add1'];
  $add1=html_escape($add1);
  $add2=$_GET['add2'];
  $add2=html_escape($add2);
  $add3=$_GET['add3'];
  $add3=html_escape($add3);
  $result=;
  $dbh=get_db_connect();
  $result=search_address($dbh,$addno,$add1,$add2,$add3);
}

 //都道府県一覧を取得して配列にセット
 $ken=;
 $dbh=get_db_connect();
 $ken=get_ken($dbh);

 //検索・表示用PHPファイル呼び出し
 include_once('./view/search_view.php');

viewファイルからGETでここにデータを飛ばし、$_GET['addno']、$_GET['add1']、$_GET['add2']、$_GET['add3']のいずれかが存在していたら検索する。

また、viewファイルで使う都道府県一覧もここで取得する。

html_escapeはextra_helper.phpから呼び出した関数である。

 

search_view.php

<!DOCTYPE html>
<head>
 <meta charset="utf-8">
 <title>住所検索</title>
</head>
<body>
 <!-- 検索条件入力 -->
 <h3>検索条件指定</h3>
 <form action="" method="GET">
  <p>郵便番号:<input type="text" name="addno"> ※「0000000」の形で入力してください</p>
  <p>都道府県:
   <select name="add1">
    <option value="指定しない">指定しない</option>
    <?php foreach($ken as $data): ?>
     <option value="<?php echo $data['add1'];?>"><?php echo $data['add1'];?></option>
    <?php endforeach; ?>
   </select>
  </p>
  <p>市区町村:<input type="text" name="add2"></p>
  <p>町域:<input type="text" name="add3"></p>
  <input type="submit" value="検索!">
 </form>
 <br>
 <!-- 表示 -->
 <?php if(isset($addno) or isset($add1) or isset($add2) or isset($add3)): ?>
  <table border="1">
   <tr>
    <th>郵便番号</th>
    <th>都道府県</th>
    <th>市区町村</th>
    <th>町域</th>
   </tr>
   <?php foreach ($result as $row): ?>
    <tr>
     <td><?php echo $row['addno']; ?></td>
     <td><?php echo $row['add1']; ?></td>
     <td><?php echo $row['add2']; ?></td>
     <td><?php echo $row['add3']; ?></td>

    </tr>
   <?php endforeach; ?>
  </table>
 <?php endif; ?>
</body> 

検索条件を入力する部分と検索結果を表示する部分を1ページで表示する。検索結果についてはGETでこのページを呼び出したときのみ表示。

都道府県名については別途関数で取得してコンボボックスへ。

 

db_helper.php(追加部分)

function get_ken($dbh){
 $ken=;
 $sql="select max(x.kenid) as id ,max(x.add1) as add1 from address as x group by x.add1 order by x.kenid asc";
 $stmt=$dbh->prepare($sql);
 $stmt->execute();
 while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
  $ken
=$row;
 }
 return $ken;
}

function search_address($dbh,$addno,$add1,$add2,$add3){
 $result=;
 $sql="select addno,add1,add2,add3 from address where 1=1";
 if ($addno!=='') {
  $sql=$sql." and addno like :addno";
 }
 if ($add1!=='指定しない') {
  $sql=$sql." and add1 like :add1";
 }
 if ($add2!=='') {
  $sql=$sql." and add2 like :add2";
 }
 if ($add3!=='') {
  $sql=$sql.=" and add3 like :add3";
 }
 $sql=$sql." order by addno";
 //プリペア、置換
 $stmt=$dbh->prepare($sql);
 if ($addno!=='') {
  $stmt->bindvalue(':addno','%'.$addno.'%',PDO::PARAM_STR);
 }
 if ($add1!=='指定しない') {
  $stmt->bindvalue(':add1','%'.$add1.'%',PDO::PARAM_STR);
 }
 if ($add2!=='') {
  $stmt->bindvalue(':add2','%'.$add2.'%',PDO::PARAM_STR);
 }
 if ($add3!=='') {
  $stmt->bindvalue(':add3','%'.$add3.'%',PDO::PARAM_STR);
 }
 $stmt->execute();
 while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
  $result
=$row;
 }
 return $result;
}

どちらもaddressテーブルを検索して結果を配列として取得している。なお、addressテーブルについてはフィールド「kenid(データ型:int)」を追加して都道府県別に番号を振った。

これが検索条件を入れる前の状態。

f:id:R_de_aru:20180712225210p:plain

検索条件を入力するとこうなる。

f:id:R_de_aru:20180712225245p:plain

明日からはこの状態をベースに、「1ページに検索結果を10件表示し、GoogleやYahooの検索のように2ページ目や3ページ目を表すボタンを押したら11~20件目、21~30件目が表示される」という仕様を作っていく。