0

私は訪問者のIPのmysqlデータベースを持っており、それらのIPのいくつがX国からのものであるかを示すHTMLテーブルを作成しようとしています。訪問者がどの国から来たのかを示すスクリプトを取得することはできますが、行ごとではなくグループで表示する方法が見つからないようです。

たとえば、現在私は次のようになっています。

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たが、それでもすべての訪問者が行ごとに一覧表示され、すべての行に「配列」の値が割り当てられます。

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);
4

2 に答える 2

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

これを試してみてください

于 2013-02-26T21:49:27.237 に答える