gpt4 book ai didi

php - 输出中的重复记录

转载 作者:行者123 更新时间:2023-11-30 22:26:28 25 4
gpt4 key购买 nike

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

<?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;

?>

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

75425,74743 Tall Tale Trailers Inc
75486,74743 Tall Tale Trailers Inc
75421,74743 Tall Tale Trailers Inc
75473,74743 Tall Tale Trailers Inc
75460,74743 Tall Tale Trailers Inc
75461,74743 Tall Tale Trailers Inc
75470,74743 Tall Tale Trailers Inc
75462,74743 Tall Tale Trailers Inc
75462,74743 Tall Tale Trailers Inc
75446,74743 Tall Tale Trailers Inc

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

最佳答案

我认为您将其包含在数组中。因此,对于每次迭代,它都会在 $categories 数组中相加。您可以使用 in_arrayarray_unique

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

关于php - 输出中的重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35094307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com