検索フォームサイトを作成しています。
セレクトボックスの選択状況によって、実行するSQL文を分けるという処理をしたいです。
php部分で、場合分けを行っているのですが、
($KEY1 == "0")と($KEY2 == "0")を条件に指定した部分のSQL文の結果が表示されません。
結果を表示するのは、html内のphp部分です。
原因は、どこがどこにあるか分からないので、教えていただきたいです。
php
1<?php 2error_reporting(E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED & ~ E_NOTICE); 3 4 5 $P_IP = "xxxx"; 6 $P_PORT = "xxxx"; 7 $P_DBNAME = "xxx"; 8 $P_DBUSER = "xxx"; 9 $P_DBPASS = "xxx"; 10 11header('Content-Type: text/html; charset=utf8'); 12 13 // 各種パラメータを指定して接続 14$connect = "host=$P_IP port=$P_PORT dbname=$P_DBNAME user=$P_DBUSER password=$P_DBPASS"; 15 16if (! $con = pg_connect( $connect ) ){ 17 die( 'Postgres接続失敗' ); 18 } 19 20 // フォームへの入力値を受けとる 21session_start(); 22$_SESSION['word1'] = $_GET['word1']; 23$_SESSION['word2'] = $_GET['word2']; 24$_SESSION['word3'] = $_GET['word3']; 25 26$KEY1 = $_SESSION['word1']; 27$KEY2 = $_SESSION['word2']; 28$KEY3 = $_SESSION['word3']; 29 30 31if (!isset($_GET['page'])) { 32 $page = 1; // 最初のアクセス 33} else { 34 $page = intval($_GET['page']); // ページ指定でのアクセス 35 if ($page == 0 ) { die('page no error');} 36} 37 38print $page; 39 40 41session_start(); 42 $_SESSION['page'] =$page; 43session_start(); 44 $_SESSION['page'] =$page; 45 46 47$pagehead = "<B>全$tcnt 件"; 48$offset = ($page-1) * 5; 49 50 51 52 53print $offset; 54 55if ($KEY1 == "0" && $KEY2 == "0"){ 56 print "ワードのみ指定の検索"; 57 $test ="test1"; 58 $sql = "SELECT * FROM theme WHERE(aaa LIKE '%{$KEY3}%') OR (bbb LIKE '%{$KEY3}%')limit 5 offset $offset"; 59}else if($KEY1 == "0"){ 60 print "A種別のみの指定"; 61 $test ="test2"; 62 $sql = "SELECT * FROM theme WHERE (ccc LIKE '%{$KEY1}%') and (aaa LIKE '%{$KEY3}%' or bbb LIKE '%{$KEY3}%') limit 5 offset $offset"; 63}else if($KEY2 == "0"){ 64 print "B種別のみの指定"; 65 $test ="test3"; 66 $sql = "SELECT * FROM theme WHERE (ddd LIKE '%{$KEY2}%') and (aaa LIKE '%{$KEY3}%' or bbb LIKE '%{$KEY3}%') limit 5 offset $offset"; 67}else{ 68 print "全て指定で検索"; 69 $test ="test4"; 70 $sql ="SELECT * FROM theme WHERE (ccc LIKE '%{$KEY1}%' and ddd LIKE '%{$KEY2}%') and (aaa LIKE '%{$KEY3}%' or bbb LIKE '%{$KEY3}%') limit 5 offset $offset"; 71} 72 73$result = pg_query($con,$sql); 74 75 76$total = pg_query($con, "SELECT * FROM theme order by den_num limit 10 offset $offset"); 77$totalPage = pg_num_rows($total); 78 79 80//登録されている全行数を取得 81 82$totalpage = pg_query($con, "SELECT * FROM theme"); 83$total_num = pg_num_rows($totalpage); 84 85 86//表示されている行数を取得 87 88$result_sum = pg_num_rows($result); 89$totalpage = ceil($result_sum/5) * $page; 90 91 92 93//各種エラーの設定 94 95if (!$result) { 96 die('クエリー1が失敗しました。'.pg_last_error()); 97} 98?>
html
1<html> 2<head> 3 <meta charset="UTF-8"> 4 <link rel="stylesheet" type="text/css" href="style.css"> 5<title>FAQ</title> 6</head> 7<body> 8<div class="page_title"> 9 10 <p class="title_p">FAQサイト</p> 11 <a href="xxx.php" class="top_retern">xxx</a> 12 <a href="xxx.php" class="top_retern">xxx</a> 13</div> 14 15 16 17<div class="siborikomi"> 18 <div class="title"> 19 <h3 class="title_s">検索</h3> 20 21 </div> 22<div class="search1"> 23 <form method="get" action='<?= $_SERVER["PHP_SELF"] ?>'> 24 <p>xxx</p> 25 <select class="soft_select" name="word1"> 26 <option value="0" name="word1" >0.xxx</option> 27 <option value="1" name="word1" >1.xxx</option> 28 <option value="2" name="word1" > 2.xxx</option> 29 <option value="3" name="word1" >3.xxx</option> 30 <option value="3" name="word1" >4.xxx</option> 31 32 </select> 33 <p>xxx</p> 34 <select class="type_select" name="word2"> 35 <option value="0" name="word2" >0.xxx</option> 36 <option value="1" name="word2" >1.xxx</option> 37 <option value="2" name="word2" > 2.xxx</option> 38 <option value="3" name="word2" >3.xxx</option> 39 <option value="4" name="word2" >4.xxx</option> 40 <option value="5" name="word2" >5.xxx</option> 41 </select> 42 <p>ワード</p> 43 44 45 46 <input type="text" name="word3" id="" value="<?php print $KEY3 ?>"> 47 <input type="submit" value="検索"> 48 49 <p>件/全<?php print $total_num ?>件</p> 50 <p><a class="botton" href="xxx.php?page=<?php echo ($page + 1); ?>&word1=<?php echo $KEY1;?>&word2=<?php echo $KEY2;?>&word3=<?php echo $KEY3;?>">next</a></p> 51 <p><a class="botton" href="userxxx.php?page=<?php echo ($page - 1); ?>&word1=<?php echo $KEY1;?>&word2=<?php echo $KEY2;?>&word3=<?php echo $KEY3;?>">back</a></p> 52 </form> 53 </div> 54 55 56 57<table> 58 <tr> 59 <th class="den_num"></th> 60 <th class="type_q">xxx</th> 61 <th class="contents">xxx</th> 62 <th class="question">xxx</th> 63 <th class="answer">xxx</th> 64 </tr> 65 66 67 68 <?php 69 if 70 ($KEY3==""){ 71 for ($i = 0 ; $i < pg_num_rows($total) ; $i++){ 72 $rows = pg_fetch_array($total, NULL, PGSQL_ASSOC); 73 $_GET['xxx'] = $rows['xxxx']; 74 $denden = $_GET['xxx']; 75 print '<tr>'; 76 print'<th class="xxx"><a href="userxxx.php?link='.($rows['xxx']).'&get='.$denden.'" class="m" >'.($rows['xxx']).'</a></th>'; 77 print'<th class="type_q">'.($rows['xxx']).'</th>'; 78 print'<th class="contents">'.($rows['xxx']).'</th>'; 79 print'<th class="question">'.($rows['xxx']).'</th>'; 80 print'<th>'.($rows['xxx']).'</th>'; 81 print '</tr>'; 82 } 83 }else{ 84 for ($i = 0 ; $i < pg_num_rows($result) ; $i++){ 85 $rows = pg_fetch_array($result, NULL, PGSQL_ASSOC); 86 print '<tr>'; 87 print'<th class="den_num"><a href="xxx.php?link='.($rows['xxx']).'&get='.$denden.'" class="m" >'.($rows['xxx']).'</a></th>'; 88 print'<th class="type_q">'.($rows['xxx']).'</th>'; 89 print'<th class="contents">'.($rows['xxx']).'</th>'; 90 print'<th class="question">'.($rows['xxx']).'</th>'; 91 print'<th>'.($rows['xxx']).'</th>'; 92 print '</tr>'; 93 }} 94 95?> 96 97 </table> 98 </div> 99 100 101</body> 102</html> 103 104