0

我的网站的 header.php 文件中有以下代码:

<?php
mysql_select_db($database_ecsdb, $ecsdb);
$query_getCat = "SELECT DISTINCT category FROM food ORDER BY category DESC";
$getCat = mysql_query($query_getCat, $ecsdb) or die(mysql_error());
$row_getCat = mysql_fetch_assoc($getCat);
$totalRows_getCat = mysql_num_rows($getCat);

mysql_select_db($database_ecsdb, $ecsdb);
$query_getZip = "SELECT * FROM food ORDER BY zip DESC";
$getZip = mysql_query($query_getZip, $ecsdb) or die(mysql_error());
$row_getZip = mysql_fetch_assoc($getZip);
$totalRows_getZip = mysql_num_rows($getZip);
?>

<div id="header" >
<div id="search">
<h2> Search for Organizations</h2>
<br>

<form action="../spsucares2/search.php" method="get">

<label name="title">
    Category:
        <select name="category">      
          <?php
    do {  
    ?>
              <option value="<?php echo $row_getCat['category']?>"><?php echo $row_getCat['category']?></option>
              <?php
    } while ($row_getCat = mysql_fetch_assoc($getCat));
      $rows = mysql_num_rows($getCat);
      if($rows > 0) {
          mysql_data_seek($getCat, 0);
          $row_getCat = mysql_fetch_assoc($getCat);
      }
    ?>
    </select>
    </label>

  <label>Enter your ZIP Code:
     <input maxlength="5" name="zipcode" size="6" type="text" /></label> </br>

  <label>Select a distance:</label>
        <select name="distance">
          <option>5</option>
          <option>10</option>
          <option>25</option>
          <option>50</option>
          <option>100</option>
        </select>

  <input name="submit" type="submit" value="search" />


</form>

<?php
        if(isset($_POST['submit'])) {
            if(!preg_match('/^[0-9]{5}$/', $_POST['zipcode'])) {
                echo "<p><strong>You did not enter a properly formatted ZIP Code.</strong> Please try again.</p>\n";
            }
            elseif(!preg_match('/^[0-9]{1,3}$/', $_POST['distance'])) {
                echo "<p><strong>You did not enter a properly formatted distance.</strong> Please try again.</p>\n";
            }
            else {
                //connect to db server; select database
                $link = mysql_connect('localhost', 'root', 'fisher36') or die('Cannot connect to database server');
                mysql_select_db('cares') or die('Cannot select database');

                //query for coordinates of provided ZIP Code
                if(!$rs = mysql_query("SELECT * FROM zip WHERE zip_code = '$_POST[zipcode]'")) {
                    echo "<p><strong>There was a database error attempting to retrieve your ZIP Code.</strong> Please try again.</p>\n";
                }
                else {
                    if(mysql_num_rows($rs) == 0) {
                        echo "<p><strong>No database match for provided ZIP Code.</strong> Please enter a new ZIP Code.</p>\n"; 
                    }
                    else {
                        //if found, set variables
                        $row = mysql_fetch_array($rs);
                        $lat1 = $row['latitude'];
                        $lon1 = $row['longitude'];
                        $d = $_POST['distance'];
                        $r = 3959;

                        //compute max and min latitudes / longitudes for search square
                        $latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
                        $latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
                        $lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
                        $lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));

                        //display information about starting point
                        //provide max and min latitudes / longitudes
                        echo "<table class=\"bordered\" cellspacing=\"0\">\n";
                        //echo "<tr><th>City</th><th>State</th><th>Lat</th><th>Lon</th><th>Max Lat (N)</th><th>Min Lat (S)</th><th>Max Lon (E)</th><th>Min Lon (W)</th></tr>\n";
                        //echo "<tr><td>$row[city]</td><td>$row[state]</td><td>$lat1</td><td>$lon1</td><td>$latN</td><td>$latS</td><td>$lonE</td><td>$lonW</td></tr>\n";
                        echo "</table>\n<br />\n";

                        //find all coordinates within the search square's area
                        //exclude the starting point and any empty city values
                        $query = "SELECT * FROM zip WHERE (latitude <= $latN AND latitude >= $latS AND longitude <= $lonE AND longitude >= $lonW) AND (latitude != $lat1 AND longitude != $lon1) AND city != '' ORDER BY state, city, latitude, longitude";
                        if(!$rs = mysql_query($query)) {
                            echo "<p><strong>There was an error selecting nearby ZIP Codes from the database.</strong></p>\n";
                        }
                        elseif(mysql_num_rows($rs) == 0) {
                            echo "<p><strong>No nearby ZIP Codes located within the distance specified.</strong> Please try a different distance.</p>\n";                               
                        }
                        else {
                            //output all matches to screen
                            echo "<table class=\"bordered\" cellspacing=\"0\">\n";
                            echo "<tr><th>City</th><th>State</th><th>ZIP Code</th><th>Latitude</th><th>Longitude</th><th>Miles, Point A To B</th></tr>\n";
                            while($row = mysql_fetch_array($rs)) {
                                echo "<tr><td>$row[city]</td><td>$row[state]</td><td>$row[zip_code]</td><td>$row[latitude]</td><td>$row[longitude]</td><td>";
                                echo acos(sin(deg2rad($lat1)) * sin(deg2rad($row['latitude'])) + cos(deg2rad($lat1)) * cos(deg2rad($row['latitude'])) * cos(deg2rad($row['longitude']) - deg2rad($lon1))) * $r;
                                echo "</td>  </tr>\n";  
                            }
                            echo "</table>\n<br />\n";
                        }
                    }
                }
            }
        }
    ?>


</div>


</div>
<br clear="all" />

它允许用户在选定范围内搜索组织。但是,我的搜索功能无法正常工作,因为它不会过滤结果,而是返回所有结果。此外,如果选择了两个类别,则只能搜索一个类别,而不能同时搜索这两个类别。请问我该如何解决这个问题?

4

0 回答 0