現在、ECサイトの検索機能の強化を行っています。
タイトル通り検索結果の並び替え機能を実装したいです。
PHP
1<!-- search_result.php --> 2 3<form method = "GET" action = "search_result.php"> 4<!-- ジャンル選択ボックス --> 5<select name = "genre_select" name = "genre_select" style = "height: 30px"> 6 7 <option value = "すべて">すべて</option> 8 <option value = "ビタミン">ビタミン</option> 9 <option value = "ミネラル">ミネラル</option> 10 <option value = "コスメ">コスメ</option> 11 <option value = "健康食品">健康食品</option> 12 13</select> 14 15<!-- 検索ボックス --> 16<input type = "text" name = "search_box" placeholder = "何かお探しですか?"> 17 18<!-- 検索ボタン --> 19<a href = "search_result.php" class = "search_button"> 20 <input type = "submit" name = "search_button" value = "検索"> 21</a> 22 23</form> 24 25<form method = "POST" action = "search_result.php"> 26 27<h2>検索結果</h2> 28<!-- 並び替え方法選択 --> 29<select name = "sort"> 30 <option value = "cost_up">値段の安い順</option> 31 <option value = "cost_down">値段の高い順</option> 32 <option value = "name">名前順</option> 33</select> 34 35<!-- 並び替えボタン --> 36<input type = "submit" name = "sort_button" value = "並び替える"> 37 38</form> 39 40<form method = "POST" action = "search_result.php"> 41<!-- 検索結果表示テーブル --> 42<table> 43<!-- テーブルヘッダー --> 44<thead><tr> 45 <th></th> 46 <th>商品名</th> 47 <th>値段</th> 48 <th>売り文句</th> 49</tr></thead> 50 51<?php 52 53require_once 'datebase_connect.php'; //データベース接続 54 55if(isset($_POST['sort_button'])){ 56 $_SESSION['sort'] = 'sort'; 57 if($_POST['sort'] == 'cost_up'){ 58 $sql = "SELECT * FROM goods WHERE name LIKE '%$search_box%' and genre LIKE '%$genre_select%' ORDER BY price ASC"; 59 }elseif($_POST['sort'] == 'cost_down'){ 60 $sql = "SELECT * FROM goods WHERE name LIKE '%$search_box%' and genre LIKE '%$genre_select%' ORDER BY price DESC"; 61 }else{ 62 $sql = "SELECT * FROM goods WHERE name LIKE '%$search_box%' and genre LIKE '%$genre_select%' ORDER BY name ASC"; 63 } 64} 65 66if(!isset($_SESSION['sort'])){ 67 $sql = "SELECT * FROM goods WHERE name LIKE '%$search_box%' and genre LIKE '%$genre_select%' "; 68} 69 70$result = $mysqli -> query($sql); 71 72 73if(!$result) { 74echo $mysqli->error; 75 exit(); 76 } 77 78 //連想配列で取得 79 while($row = $result->fetch_array(MYSQLI_ASSOC)){ 80 $rows[] = $row; 81 } 82 83 //結果セットを解放 84 $result->free(); 85 86 // データベース切断 87 $mysqli->close(); 88 $i = 0; 89 foreach($rows as $row){ 90 91 // 画像ファイル読み込み 92 $image_disp = '<img src = "./image/'.$row['file'].'" width="100px" height="100px">'; 93 94 $i ++ ; 95 ?> 96 97 98 99 <tbody> 100 <tr align = "center"> 101 <td style = "width: 10%"><?php echo $image_disp; ?></td> 102 <td style = "width: 20%"><a href="goods_detail.php?code=<?php echo(htmlspecialchars( $row['id'] ) ); ?>"><?php echo htmlspecialchars($row['name'],ENT_QUOTES,'UTF-8'); ?></a></td> 103 <td style = "width: 8%"><?php echo htmlspecialchars($row['price'],ENT_QUOTES,'UTF-8'); echo '円';?></td> 104 <td style = "width: 50%"><?php echo htmlspecialchars($row['catchphrase'],ENT_QUOTES,'UTF-8'); ?></td> 105 106 <input name = "goods_code" type = "hidden" value = "<?php echo $i; ?>"> 107 108 </tr> 109 </tbody> 110 111 <?php 112 if(isset($_SESSION['sort'])){ 113 session_destroy(); 114 unset($_SESSION['sort']);} 115 } ?> 116 117 </table></center> 118 119 </form></div>
このようなコードを使ったとき、検索結果のみを並び替えしたいのですが、
データベースに登録されている商品全体が並び変わってしまいます。
読みにくいソースで申し訳ございませんが、
修正案のアドバイスよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/04/17 07:36