0

这是更新的代码。

分类表

编号 | 标题 | 描述 | state_id | city_id |

1 福特 | 福特出售| 1 | 1

2 福特 | 福特探险家| 2 | 1

状态表

编号 | 姓名 |

1 德克萨斯州 2 亚利桑那州

城市表


编号 | 姓名 | state_id

1 阿灵顿 1 2 地球 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

我想我会分享 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 回答
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。

作为旁注,您不需要为相同的数据查询数据库两次。

于 2012-05-22T21:01:20.180 回答