2013-04-04 85 views
0

我已將搜索功能併入我的網站,該搜索功能搜索特定郵政編碼和選定半徑內的所有組織。這部分運作良好。我現在正在嘗試整合一個類別搜索,因此它不是隻搜索一個壓縮文件的選定距離內的所有組織,而是在一個壓縮文件的選定距離內搜索選定類別中的所有組織。不過,我收到以下錯誤:PHP和mysql - 將另一個類別添加到搜索

致命錯誤:調用在C未定義功能preg__match():\ XAMPP \ htdocs中\ caretest \包括上線104

\ header.php中我敢肯定這可能是由於在我的if語句中某種濫用preg_match函數的原因,但是我不確定如何正確使用它以使其正常工作,或者如果有更好的方法來做到這一點,它不涉及preg_match也會很好。謝謝。

下面是代碼:

<?php include('./Connections/ecsdb.php'); ?> 
<script type="text/JavaScript"> 

function MM_goToURL() { //v3.0 
    var i, args=MM_goToURL.arguments; document.MM_returnValue = false; 
    for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'"); 
} 

</script> 
<?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); 

?> 

<a name="top" id="top"></a> 


    <div style="float:right;"> 

    <?php 

    $my_loginbutton = "<input name=\"toLogin\" type=\"button\" onclick=\"MM_goToURL('parent','./login/');return document.MM_returnValue\" value=\"Login\" />"; 
    $my_logoutbutton = "<input name=\"Logout\" type=\"button\" onclick=\"MM_goToURL('parent','./login/logout.php');return document.MM_returnValue\" value=\"Logout\" />"; 

    if (isset($_SESSION['MM_Username'])) 
     echo $my_logoutbutton; 
    else 
     echo $my_loginbutton; 
    ?> 

</div> 


    <div id="header" > 
<div style="float:left;"><img name="Alpha Accounting logo" src="./images/banner.png" width="800" height="140" border="0" alt="Alpha Accounting"></div> 
<div style="margin: 10px 0 0 5px; float:left;"></div> 
<div id="search"> 
<h2> Search for Organizations</h2> 
<br> 


<form action="../caretest/search.php" method="post"> 

<label name="category"> 
    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('/w', $_POST['category'])){ 
      // echo "<p><strong>You must select a category. </strong> Please try again.</p>\n"; 
      //} 
      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($hostname_ecsdb, $username_ecsdb, $password_ecsdb) or die('Cannot connect to database server'); 
       mysql_select_db($database_ecsdb) 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)))); 

         //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"<br />"; 
          echo"<br />"; 
          //echo "<h2> Organization Search Results</h2>"; 

          echo "<table class=\"bordered\" cellspacing=\"0\">\n"; 
          echo"<tr><th></th></tr>\n"; 
          echo "<h2> Organization Search Results</h2>"; 
          echo"<tr><th></th></tr>\n"; 

          echo "<tr><th>Agency</th><th>Telephone</th><th>Hours</th><th>Street Address</th><th>City</th><th>State</th><th>Zip</th><th>Website</th><th>Directions</th></tr>\n"; 
          while($row = mysql_fetch_array($rs)) { 
           //search for matching zip codes in the organization table 
           if(!$rt = mysql_query("SELECT * FROM food WHERE zip = '$row[zip_code]'")){ 
            echo "<p>Didn't work, try again!</p>"; 
            } 
           else{ 
             while($rowt = mysql_fetch_array($rt)) { 
              //output results 
              echo "<tr><td>$rowt[Agency]</td><td>$rowt[Telephone]</td><td>$rowt[Hours]</td><td>$rowt[street_address]</td><td>$rowt[city]</td><td>$rowt[state]</td><td>$rowt[zip]</td><td>$rowt[website]</td><td>"; 
              echo "</td></tr>\n"; 
             } 
            } 
          } 
          echo "</table>\n<br />\n"; 
         } 
        } 
       } 
      } 
     } 
    ?> 


</div> 
+0

'preg_match',而不是'preg__match'。單下劃線應該很好。 – 2013-04-04 16:52:09

+0

這工作,擺脫了preg_match錯誤...但是現在我收到一條消息,我必須選擇一個類別,所以由於某種原因,它不讀取類別下拉框中的選擇。 – 2013-04-04 16:57:00

回答

0

的函數被調用preg_match,不preg__match

0

我確實猜測你的XAMPP設置有問題。 Preg_Match應該不會被定義,除非你拼錯了它或者其他東西,但是你的代碼看起來很乾淨。

看看您是否可以暫時將代碼複製到網絡服務器,並確定您是否也有該錯誤。我猜你不會。

[編輯]你打電話給preg__match使用兩個下劃線,但它被註釋掉。這是否會導致您的問題?

+0

嗨塞斯,謝謝你的回答,那是導致問題的線路。我糾正了它,我不再遇到preg_match錯誤,但它似乎並未檢測到所選類別。 – 2013-04-04 17:01:38

相關問題