作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个访问者 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/
我是一名优秀的程序员,十分优秀!