2011-03-09 94 views
0

在我們的網站上,我們有一個功能,人們可以輸入他們的英國郵政編碼,並給予他們十個最近治療者的名單。除了兩個或兩個以上的治療者共用同一個郵編,它只會返回其中的一個,這很有效。如何獲得此代碼以返回多個相同的英國郵政編碼?

我相信這是這個函數是造成問題:

function postcode_closest ($needle,$haystack) // $needle = user entered postcode. 
               // $haystack = list of postcodes. 
{ 
    if (!$needle||!$haystack) { return; } 
    if (!is_array($haystack)) { return; } 

    foreach ($haystack as $postcode) 
    { 
     $results[$postcode]=postcode_distance($needle,$postcode); 
    } 

    asort($results); 

    return $results; 
} 

編輯:這裏是這個函數被調用的主要部分代碼:

$query = mysql_query("SELECT latitude, longitude FROM postcodes WHERE outcode='$outcode'"); 
if (mysql_fetch_array($query)) 
{ 
    $closest=postcode_closest($outcode,$postcodes); // Call the postcode_closest function to get a list of $postcodes ordered by distance from $outcode 
    echo "<table>\n<thead>\n<tr><th scope='col'>Name</th><th scope='col'>Town</th><th scope='col'>Telephone</th><th scope='col'>Email</th><th scope='col'>Distance</th></tr>\n</thead>\n<tbody>\n"; 
    $i = 0; 
    foreach ($closest as $key => $val) 
    { 
     if ($i < 10) // Display this number of results from the list. 
     { 
      $query = mysql_query("SELECT member, postcode, name, town, telephone, email FROM healers WHERE postcode = '$key'"); 
      $one = mysql_fetch_array($query); 

      // Linkify email addresses, but only if they exist. 
      if ($one[5]) 
      { 
       $one[5] = "<a href='mailto:$one[5]'>$one[5]</a>"; 
      } 

      echo "<tr><td>$one[2]</td><td>$one[3]</td><td>$one[4]</td><td>$one[5]</td><td>$val Miles</td></tr>\n"; 
      $i++; 
     } 
    } 
    echo "</tbody>\n</table>\n"; 
} 

如果我正確理解,$ postcode被用作$ results數組的密鑰,這就是爲什麼每個郵政編碼只會出現一次。

這有道理嗎?我將如何改變代碼,讓共享郵編的治療師都出現在名單上?我繼承了這些代碼,並且我的編程知識很少。如有必要,我可以發佈完整的代碼。

謝謝。

+0

嗯,對我來說,這需要郵政編碼的列表,並返回距離的列表通過郵政編碼爲核心。這段代碼對治療者沒有任何意義。我想,你必須進一步搜索一下。 – ZeissS 2011-03-09 18:51:13

+0

這是使用這個需要改變的代碼。你能用它更新你的問題嗎? – 2011-03-09 18:54:48

+0

太好了,謝謝。我已經添加了調用該函數的代碼的主要部分。這有幫助嗎? – Richard 2011-03-09 19:15:32

回答

0

我認爲你是對的,你必須給每個鍵添加多個郵政編碼的可能性。 例子:

foreach ($haystack as $postcode) 
{ 
    // if we already have this code 
    if(isset($results[$postcode])) 
    $results[$postcode][]=postcode_distance($needle,$postcode); 
    else 
    $results[$postcode]=postcode_distance($needle,$postcode); 
} 

但之後,你必須改變你與陣列工作方式

相關問題