douwen4178 2012-10-10 03:07
浏览 35
已采纳

我可以为MySql表值指定字符串吗?

I have a MySql table that stores the users state and city from a list of states and cities. I specifically coded each state as their two letter shortened version (like WA for Washington and CA for California) and every city has the two letter abbreviation and the city name formated like this: Boulder Colorado would be CO-boulder and Salt Lake City, Utah would be UT-salt-lake-city as to avoid different states with same city name. The PHP inserts the value (UT-salt-lake-city) under the column City, but when I call the variable through PHP, it displays like this: Your location is: UT-salt-lake-city, Utah. To solve this, I've been making this list of variables

    if ($city == "AL-auburn") { $city = "Auburn"; }
    else if ($city == "AL-birmingham") { $city = "Birmingham"; }
    else if ($city == "GA-columbus") { $city = "Columbus"; $state = "Georgia"; }
    else if ($city == "AL-dothan") { $city = "Dothan"; }
    else if ($city == "AL-florence") { $city = "Forence"; }
    else if ($city == "AL-muscle-shoals") { $city = "Muscle Shoals"; }
    else if ($city == "AL-gadsden-anniston") { $city = "Gadsden Anniston"; }
    else if ($city == "AL-huntsville") { $city = "Huntsville"; }
    else if ($city == "AL-decatur") { $city = "Decatur"; }
    else if ($city == "AL-mobile") { $city = "Mobile"; }
    else if ($city == "AL-montgomery") { $city = "Montgomery"; }
    else if ($city == "AL-tuscaloosa") { $city = "Tuscaloosa"; }

Is there a way I can shorten this process or at least call it from a separate file so I don't have to copy/paste every time I want to call the location?

  • 写回答

3条回答 默认 最新

  • downloadTemp2014 2012-10-10 03:18
    关注

    What about something like

    $name = 'UL-salt-lake-city';
    ucwords(str_replace("-"," ",substr($name, 3)));
    

    Converts to "Salt Lake City"

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度