输出中的重复记录

时间:2016-01-29 21:24:48

标签: php mysql mysqli

我一直在将我的脚本从mysql转换为mysqli,但是我遇到了这个脚本的输出问题。此脚本根据用户输入从数据库中提取邮政编码,然后在用户定义的半径范围内查找已注册的邮政编码(在本例中为25英里)。然后脚本获取这些邮政编码并将它们与该区域中的商店的活动数据库进行比较,并根据用户邮政编码提供关闭商店。所有这一切都很好。问题在于我的输出(正如你所看到的)它为每个邮政编码返回重复和循环,即使附近只有一个商店。

            var user = new Object();
            user.remove = function () {
                console.log("remove called");
            }
            user.remove.all = function (){
                console.log("remove all");
            }

            user.remove();
            user.remove.all();

我对上面的代码做了一些更改,现在输出如下:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// select statment
$zipcodes = 'SELECT zip_code, latitude, longitude, statename, city,
    ROUND((ACOS((SIN(33.776/57.2958) * SIN(latitude/57.2958)) +
    (COS(33.776/57.2958) * COS(latitude/57.2958) *
    COS(longitude/57.2958 - -95.7128/57.2958))))
    * 3963) AS distance
    from tblzipcode
    WHERE (latitude >= 33.776 - (25/111))
    And (latitude <= 33.776 + (25/111))
    AND (longitude >= -95.7128 - (25/111))
    AND (longitude <= -95.7125 + (25/111))
    ORDER BY distance';
$result = $conn->query($zipcodes);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    $optioncodesOut = $row['zip_code'] . ",";
    echo $optioncodesOut ;

$nextplace ="select id, attn, addr1, addr2, addr3, city, state, country, zip, phone_number, fax, web_site_url, delaerwebid, ARCUSTO_ID, CUSTNO as dealer from DEALERLOCATOR where zip in (75425,75486,75421,75473,75460,75461,75470,75462,75462,75411,75477,74738,74759,75488,75446,74727,75416,74743,75450,75492,74723,74723,75443,75468,75438,75469,74721,74756,75434,75435,75441,75447,75432,75415,75436,75449,74726,75418,74760,75476,74523,74735,74735,75475,74720,75412,75448,74741,74741,74761,74542,74542,75413,75439)";
$result2 = $conn->query($nextplace);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
    $pickthedealer = $row2['zip'] . '&nbsp;' . $row2['attn'] . $row2['ARCUSTO_ID'];
    //echo $pickthedealer . '<br />';

$showplace = "select distinct zip, ARCUSTO_ID, attn from DEALERLOCATOR where ARCUSTO_ID in (25739)";
$result3 = $conn->query($showplace);
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
    echo $row3['zip'] .'&nbsp;'. $row3['attn'] . '<br />';
      }
     }
    } 
   }
  }
 }
//echo $nextplace;

?>

如您所见,25英里附近有10个邮政编码,但这10个邮政编码中只有一个商店。我需要帮助让它只显示一个商店而不是同一个商店,因为它找到了10个邮政编码。这不是一个独特的问题,因为邮政编码已经是唯一的。问题在于它实际上有9个不相关的列表,它为每个邮政编码提供相同的经销商。

2 个答案:

答案 0 :(得分:0)

我认为你将它包含在数组中。所以对于每次迭代,它都在$ categories数组中加起来。您可以使用if in_arrayarray_unique

的条件
while($row2 = $result2->fetch_array()) {
    $categories[$row2['zip']] = $row2;
}

答案 1 :(得分:0)

实际上它实际上是一个嵌套问题。