前提・実現したいこと
現在口コミサイトを作っています。
検索フォームからデータベースの名前を検索して表示できる様にはなったのですが、一つのフォームで店舗名(shop)と点数(point)も検索したいと思っています。
フォームを3つ作ればいいのだと思いましたが上手くいきませんでした。
色々検索してみてもいまいちわからなかったので教えていただきたいです。
宜しくお願い致します。
該当のソースコード
<?php function getHostData($params){ if(!isset($params['search'])){ return[]; } //DBの接続情報 $password = ""; $dbname = "mydb"; $host = "localhost"; $username = "root"; //DBコネクタを生成 $Mysqli = new mysqli($host, $username, $password, $dbname); $Mysqli->query('SET NAMES utf8'); if ($Mysqli->connect_error) { error_log($mysqli->connect_error); exit; } //入力された検索条件からSQl文を生成 $where = []; if(!empty($params['name'])){ $where[] = "name like '%{$params['name']}%'"; } if($where){ $whereSql = implode(' AND ', $where); $sql = 'select * from hosts where ' . $whereSql ; }else{ $sql = 'select * from hosts'; } //SQL文を実行する $HostDataSet = $Mysqli->query($sql); //扱いやすい形に変える $result = []; while($row = $HostDataSet->fetch_assoc()){ $result[] = $row; } return $result; } ?> <!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="utf-8" > </head> <body> <h1 class="col-xs-6 col-xs-offset-3">検索フォーム</h1> <div class="col-xs-6 col-xs-offset-3 well"> <form method="get"> <div class="form-group"> <label for="InputName">名前</label> <input name="name" class="form-control" id="InputName" value="<?php echo isset($_GET['name'])? htmlspecialchars($_GET['name']) : '' ?>"> </div> <button type="submit" class="btn btn-default" name="search">検索</button> </form> </div> <?php if(!empty($_GET)){ $hostData = getHostData($_GET);?> <div class="col-xs-6 col-xs-offset-3"> <?php //③取得データを表示する ?> <?php if(isset($hostData) && count($hostData)): ?> <p class="alert alert-success"><?php echo count($hostData) ?>件見つかりました。</p> <table class="table"> <thead> <tr> <th>名前</th> <th>店舗名</th> <th>点数</th> <th>コメント</th> </tr> </thead> <tbody> <?php foreach($hostData as $row): ?> <tr> <td><?php echo htmlspecialchars($row['name']) ?></td> <td><?php echo htmlspecialchars($row['shop']) ?></td> <td><?php echo htmlspecialchars($row['point']) ?></td> <td><a href ="show_comment.php?post_id=<?=$row['post_id'] ?>">コメント</a></td> </tr> <?php endforeach; ?> </tbody> </table> <?php else: ?> <p class="alert alert-danger">検索対象は見つかりませんでした。</p> <?php endif; ?> </div> <?php } ?> </body> </html>
回答1件
あなたの回答
tips
プレビュー