0

これが更新されたコードです。

案内広告表

id | タイトル| 説明| state_id | city_id |

1フォード| 販売のためのフォード| 1 | 1

2フォード| フォードエクスプローラー| 2 | 1

状態テーブル

id | 名前|

1テキサス2アリゾナ

シティテーブル


id | 名前| state_id

1アーリントン12グローブ2

検索を実行して結果を出力するコードはすべてここにあります:

したがって、このファイルはsearch.phpと呼ばれます

enter  <?php
   $conn = mysql_connect('localhost', 'root','');
  $db = mysql_select_db('files',$conn);
  $input = $_GET['query'];
  $state = $_POST['state'];
 $city = $_POST['city'];
 if (isset($input) && $input != "" ) {
 $select = "SELECT * FROM classifieds";
 $where = " WHERE (title like '%" . $input . "%'  OR description like '%" . $input .   

 "%')";
if (!empty($city)) {
$select .= " LEFT JOIN city ON classifieds.city_id=city.id";
$where .= " AND city.name = '" . $city . "'";
}
if (!empty($state)) {        
$select .= " LEFT JOIN state ON classifieds.state_id=state.id";
$where .= " AND state.name = '" . $state . "'";
}    
$q = $select . $where . ' ORDER BY date DESC';
$r = mysql_query($q);
$rows = mysql_num_rows($r);
}
echo $rows; ?> Search Results For: <?PHP echo $input; 
if ($rows > 0) {
while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
$description = $row['description'];
echo "  <tr  >
<td ><a href='classified-" . $row['adid'] . ".htm' >" . $row['title'] . "</a><br />
 <span><em><a href='category-" . $cat . ".php'>" . $catname . "</a></em><br/>" .    
$description . "...</span></td>
 </tr>
  ";
  }
   }
  ?>
   <form action="search.php" method="get">
   <input type="text" name="query" />
  <input type="submit" name="Submit" value="Search" />
   <select name="state" >
<option value="null"></option>
<?php
//POPULATE DROP DOWN MENU WITH STATES FROM A GIVEN REGION, COUNTRY
$sql = "SELECT id, statename FROM state";
$states = mysql_query($sql,$conn);
while($row = mysql_fetch_array($states))
{
    echo ("<option value=".$row[id]." >$row[statename]</option>");
}
?>
</select>
<select name="city" >
<option value="null"></option>
<?php
$sql = "SELECT id, city FROM city  ";
$cities = mysql_query($sql,$conn);
while($row = mysql_fetch_array($cities))
{
    echo ("<option value=".$row[id].">$row[city]</option>");
}
?>  here
4

2 に答える 2

0

基本的に、設定されているかどうかstate、および/またはcity設定されているかどうかを確認する必要があります(ちなみに、GETフォームのメソッドとして設定しましたが、$_POST都市と州を確認します-それは正しいですか?)そしてそれに応じてSQLクエリに追加します:

$select = "SELECT * FROM classifieds";
$where = " WHERE (title like '%" . $input . "%'  OR description like '%" . $input . "%')";

if (!empty($city)) {
    $select .= " LEFT JOIN city ON classifieds.city_id=city.id";
    $where .= " AND city.name = '" . $city . "'";
}

if (!empty($state)) {        
    $select .= " LEFT JOIN state ON classifieds.state_id=state.id";
    $where .= " AND state.name = '" . $state . "'";
}    

$q = $select . $where . ' ORDER BY date DESC';

タイトルと説明の括弧に注意してください。括弧がないと、ORがANDよりも優先されます。

補足として、同じデータについてデータベースに2回クエリを実行する必要はありません。

于 2012-05-22T21:01:20.180 に答える
0

私はBjauyが私に与えた解決策を共有すると思いました:)

  $select = "SELECT * FROM classifieds";
      $where = " WHERE (title like '%" . $input . "%'   OR description like '%" .  

       $input . "%' )";
          if (!empty($state)) {        

        $where .= " AND state_id = '" . $state . "'";
        }
       if (!empty($city)) {

        $where .= " AND city_id = '" . $city . "'";
       } 
     $query = $select . $where . ' ORDER BY date DESC';
于 2012-05-23T16:49:47.113 に答える