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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题