douyu1990 2012-08-23 09:10
浏览 15
已采纳

使用mysql显示特定国家/地区的Web内容的最佳方式

Need a big help from you. I need an advice from all you good people out there.

I have some contents that i need to display only to few countries.I am okay with getting the user's country with Geoip.i am storing my content in MySQL database with a country_code field.country code can be empty or any single code or group of comma separated values

What i want to do is,

  1. checking country_code and if it is empty, display that content for all users.
  2. if country_code is not empty and only a single value,display content only for the given country_code users.
  3. if country_code is group of comma separated values,display contents for all the comma separated country users.

i have two options for do this

  1. adding data columns for each country separately and display contents as for example three columns for three countries US,UK,AU and if user's location is US,

    $sql=mysql_fetch_array(mysql_query("SELECT * FROM content_table WHERE US=='$country_code'"));

  2. adding country data for single column as country_code and store US,UK,AU and if user's location is US,

    $sql=mysql_fetch_array(mysql_query("SELECT * FROM content_table WHERE country_code!=''")); $pieces = explode(",", $sql['country_code']); foreach($pieces as $val){ if($country_code=="US") //display content }

NOTE:amount of countries may differs according to my needs

what will be the best practice to overcome this,

  • using different columns to different country make fast search but adding more columns is not good i think
  • using single column and iterate comma separated values needs more computation

What should i do..?? please advice me on this.. OR do you have any idea better than this..? Thanks

  • 写回答

3条回答 默认 最新

  • dsc6517 2012-08-23 09:27
    关注
    $sql=mysql_fetch_array(mysql_query("SELECT * 
        FROM content_table 
        WHERE country_code = '' 
        OR country_code REGEXP ',?". $user_country_code .",?'"));
    

    If I understood you well, you want to show elements to the user only if the user country code is in the country_code field or if the country_code is not specified ?

    This should do it, even if a database solution would be better.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭