它应该根据用户浏览Web站点的城市自动显示发布的内容。我正在尝试使用SmartIP。我尝试使用以下代码:
<?php
if ($_SESSION['smart_ip']['location']['country_code'] == 'IN'):
?>特定于印度内容
<?php
elseif ($_SESSION['smart_ip']['location']['country_code'] == 'UY'):
?>特定于乌拉圭HTML内容
<?php
....
else:
?>回退默认内容。
<?php
...
endif;
?>我试着用国家代码作为'IN‘。但是它没有显示内容。我正在尝试将代码更改为
<?php
if ($_SESSION['smart_ip']['location']['country_code']['state_code'][city_code] == 'BAN'):
?>我的疑问是:
我如何显示在班加罗尔张贴的内容自动,如果用户是从城市浏览,如班加罗尔或德里等…?我可以在哪里添加这些国家、州和城市代码?
发布于 2012-10-01 13:49:13
你可以尝试实现一个‘字典函数’:你用你提到的地理代码调用它,它会返回你想要嵌入到html模板中的本地化内容。在后台,它会查询一个“目录”,通常是这样的数组结构:
$LCat = array (
'India' => array (
'Assam' => array (
'Dispur' => "some Dispur specific content",
'Guwahati' => "some Guwahati specific content",
... some other cities in that state ...
),
'Orissa' => array (
'Bhubaneswar' => "some Bhubaneswar specific content",
... some other cities in that state ...
),
... some other states in that country ...
),
... some other countries ...
);该函数在目录中查找匹配条目(例如,通过使用is_set()函数):
if ( is_set($LCat[$_SESSION['smart_ip']['location']['country_code']]) ) {
if ( is_set($LCat[$_SESSION['smart_ip']['location']['country_code']['state_code']]) )
if ( is_set($LCat[$_SESSION['smart_ip']['location']['country_code']['state_code']['city_code']]) ) ) {
$Location=$LCat[$_SESSION['smart_ip']['location']['country_code']['state_code']]['city_code']];
} else {
$Location=$LCat[$_SESSION['smart_ip']['location']['country_code']['state_code']]
} else {
$Location=$LCat[$_SESSION['smart_ip']['location']['country_code']]
} else {
$Location="location specific content for 'Nirwana'";
}所以结构是:州>国家>城市,或者你需要的任何结构。这个想法是:尝试使用目录中最具体的匹配。如果它不存在,则使用不太具体的条目,依此类推。通过这种方式,您始终可以在结构中编写安全的回退代码,而不必为脚本可能遇到的每个位置编写回退代码。
显然,为匹配而存储的内容可以是任何内容,我只选择了简单的字符串来说明这一点。该结构也可以以不同的方式存储,例如,在运行时检查的文件系统层次结构中,每个国家的文件夹,每个州的文件夹,每个城市的文件等等。如果您想要提高性能并提供一种管理目录数据的简单方法,那么您应该将该目录存储在您在运行时查询的数据库中。不过,这个想法仍然是一样的。
请注意,我还没有测试过该代码,但它应该会为您提供一种可能的方法。
https://stackoverflow.com/questions/12667843
复制相似问题