gpt4 book ai didi

PHP:计算重复项并删除重复行

转载 作者:行者123 更新时间:2023-11-29 14:00:48 24 4
gpt4 key购买 nike

我有一个访问者 IP 的 mysql 数据库,我正在尝试创建一个 HTML 表来显示其中有多少 IP 来自 X 国家。我可以让脚本显示访问者来自哪个国家/地区,但我似乎找不到一种方法来分组而不是逐行显示它。

例如,目前我得到:

Country | Visitors
------------------
US | Array
UK | Array
UK | Array
UK | Array
US | Array
MX | Array
MX | Array

我想要的是:

Country | Visitors
------------------
US | 2 Visitors
UK | 3 Visitors
MX | 2 Visitors

我尝试过array_count_values,但它仍然按行列出每个访问者,并为每一行分配一个“Array”值。

IP 位于数据库中,脚本提取 IP,然后根据单独文件中的数据为 IP 分配国家/地区代码值。

这是我用我的业余技能破解的内容。如果有更好的方法来实现这一点,欢迎提出建议!

require_once("geoip.inc");
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);


include_once($_SERVER['DOCUMENT_ROOT'] . 'connect.php');

/* Performing SQL query */
$data = mysql_query("SELECT * FROM the_ips LIMIT 100")
or die(mysql_error());

echo "<table>";
echo "<td><strong>Country</strong></td><td><strong>Visitors</strong></td></tr>";

while($info = mysql_fetch_array( $data ))
{
$ip = $info['ip_address'];
$country_code = geoip_country_code_by_addr($gi, $ip);
$countrylist = array($country_code);
$frequency = array_count_values($countrylist);

echo "<tr><td style='width: 100px;'>".$country_code."</td><td style='width: 100px;'>".$frequency."</td></tr>";
}
echo "</table>";
geoip_close($gi);

最佳答案

SELECT
Count(Distinct country) As country_number
, country
FROM the_ips
GROUP BY country
LIMIT 100

尝试一下这个开始

关于PHP:计算重复项并删除重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15099907/

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