Php 从阵列中提取国家,然后使用;如果/否则“;确定是否需要重定向
我们网站上的一个类在尝试订购产品时会删除要选择的国家列表,其中有许多项目。3个国家-俄罗斯、白俄罗斯和哈萨克斯坦-由于注册冲突等原因需要从列表中分离出来,并将用户重定向到俄罗斯网站。在进入订单页面时,它通常只在URL中包含一个国家代码,然后根据一些国家的情况切换语言。在这种情况下,语言是完全不相关的。我找到了负责列表的类,并找到了进入订单页面的函数,其中指明了某个国家/地区。我想在其中包含一个“if/else”语句,其中“如果选择了RU、BY或KZ=重定向到另一个网站”或“else=返回国家代码” else看起来不错,但Dreamweaver一直告诉我“if”语句行有问题,我尝试用不同的方法编写语法,但运气不佳Php 从阵列中提取国家,然后使用;如果/否则“;确定是否需要重定向,php,symfony,redirect,if-statement,Php,Symfony,Redirect,If Statement,我们网站上的一个类在尝试订购产品时会删除要选择的国家列表,其中有许多项目。3个国家-俄罗斯、白俄罗斯和哈萨克斯坦-由于注册冲突等原因需要从列表中分离出来,并将用户重定向到俄罗斯网站。在进入订单页面时,它通常只在URL中包含一个国家代码,然后根据一些国家的情况切换语言。在这种情况下,语言是完全不相关的。我找到了负责列表的类,并找到了进入订单页面的函数,其中指明了某个国家/地区。我想在其中包含一个“if/else”语句,其中“如果选择了RU、BY或KZ=重定向到另一个网站”或“else=返回国家代码
<?php
class CountryUtil {
public static $country_list =
array(
'AF' => 'Afghanistan',
'AX' => 'Aland Islands',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',
'AD' => 'Andorra',
'AO' => 'Angola',
'AI' => 'Anguilla',
'AQ' => 'Antarctica',
'AG' => 'Antigua and Barbuda',
'AR' => 'Argentina',
'AM' => 'Armenia',
'AW' => 'Aruba',
'AU' => 'Australia',
'AT' => 'Austria',
'AZ' => 'Azerbaijan',
'BS' => 'Bahamas',
'BH' => 'Bahrain',
'BD' => 'Bangladesh',
'BB' => 'Barbados',
'BY' => 'Belarus',
'BE' => 'Belgium',
'BZ' => 'Belize',
'BJ' => 'Benin',
'BM' => 'Bermuda',
'BT' => 'Bhutan',
'BO' => 'Bolivia',
'BA' => 'Bosnia and Herzegovina',
'BW' => 'Botswana',
'BV' => 'Bouvet Island',
'BR' => 'Brazil',
'IO' => 'British Indian Ocean Territory',
'BN' => 'Brunei Darussalam',
'BG' => 'Bulgaria',
'BF' => 'Burkina Faso',
'BI' => 'Burundi',
'KH' => 'Cambodia',
'CM' => 'Cameroon',
'CA' => 'Canada',
'CV' => 'Cape Verde',
'KY' => 'Cayman Islands',
'CF' => 'Central African Republic',
'TD' => 'Chad',
'CL' => 'Chile',
'CN' => 'China',
'CO' => 'Colombia',
'KM' => 'Comoros',
'CG' => 'Congo',
'CK' => 'Cook Islands',
'CR' => 'Costa Rica',
'CI' => 'Cote D\'Ivoire',
'HR' => 'Croatia',
'CU' => 'Cuba',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'DK' => 'Denmark',
'DJ' => 'Djibouti',
'DM' => 'Dominica',
'DO' => 'Dominican Republic',
'EC' => 'Ecuador',
'EG' => 'Egypt',
'SV' => 'El Salvador',
'GQ' => 'Equatorial Guinea',
'ER' => 'Eritrea',
'EE' => 'Estonia',
'ET' => 'Ethiopia',
'FK' => 'Falkland Islands (Malvinas)',
'FO' => 'Faroe Islands',
'FJ' => 'Fiji',
'FI' => 'Finland',
'FR' => 'France',
'GF' => 'French Guiana',
'PF' => 'French Polynesia',
'GA' => 'Gabon',
'GM' => 'Gambia',
'GE' => 'Georgia',
'DE' => 'Germany',
'GH' => 'Ghana',
'GI' => 'Gibraltar',
'GR' => 'Greece',
'GL' => 'Greenland',
'GD' => 'Grenada',
'GP' => 'Guadeloupe',
'GU' => 'Guam',
'GT' => 'Guatemala',
'GN' => 'Guinea',
'GW' => 'Guinea-Bissau',
'GY' => 'Guyana',
'HT' => 'Haiti',
'HM' => 'Heard Island and McDonald Islands',
'VA' => 'Holy See (Vatican City State)',
'HN' => 'Honduras',
'HK' => 'Hong Kong',
'HU' => 'Hungary',
'IS' => 'Iceland',
'IN' => 'India',
'ID' => 'Indonesia',
'IR' => 'Iran, Islamic Republic of',
'IQ' => 'Iraq',
'IE' => 'Ireland',
'IL' => 'Israel',
'IT' => 'Italy',
'JM' => 'Jamaica',
'JP' => 'Japan',
'JO' => 'Jordan',
'KZ' => 'Kazakhstan',
'KE' => 'Kenya',
'KI' => 'Kiribati',
'KR' => 'Korea, Republic of',
'KW' => 'Kuwait',
'KG' => 'Kyrgyzstan',
'LA' => 'Lao People\'s Democratic Republic',
'LV' => 'Latvia',
'LB' => 'Lebanon',
'LS' => 'Lesotho',
'LR' => 'Liberia',
'LY' => 'Libyan Arab Jamahiriya',
'LI' => 'Liechtenstein',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'MO' => 'Macao',
'MK' => 'Macedonia, the Former Yugoslav Republic of',
'MG' => 'Madagascar',
'MW' => 'Malawi',
'MY' => 'Malaysia',
'MV' => 'Maldives',
'ML' => 'Mali',
'MT' => 'Malta',
'MH' => 'Marshall Islands',
'MQ' => 'Martinique',
'MR' => 'Mauritania',
'MU' => 'Mauritius',
'YT' => 'Mayotte',
'MX' => 'Mexico',
'FM' => 'Micronesia, Federated States of',
'MD' => 'Moldova, Republic of',
'MC' => 'Monaco',
'MN' => 'Mongolia',
'ME' => 'Montenegro',
'MS' => 'Montserrat',
'MA' => 'Morocco',
'MZ' => 'Mozambique',
'MM' => 'Myanmar',
'NA' => 'Namibia',
'NR' => 'Nauru',
'NP' => 'Nepal',
'NL' => 'Netherlands',
'AN' => 'Netherlands Antilles',
'NC' => 'New Caledonia',
'NZ' => 'New Zealand',
'NI' => 'Nicaragua',
'NE' => 'Niger',
'NG' => 'Nigeria',
'NU' => 'Niue',
'NF' => 'Norfolk Island',
'MP' => 'Northern Mariana Islands',
'NO' => 'Norway',
'OM' => 'Oman',
'PK' => 'Pakistan',
'PW' => 'Palau',
'PS' => 'Palestinian Territory',
'PA' => 'Panama',
'PG' => 'Papua New Guinea',
'PY' => 'Paraguay',
'PE' => 'Peru',
'PH' => 'Philippines',
'PL' => 'Poland',
'PT' => 'Portugal',
'PR' => 'Puerto Rico',
'QA' => 'Qatar',
'RE' => 'Reunion',
'RO' => 'Romania',
'RU' => 'Russian Federation',
'RW' => 'Rwanda',
'SH' => 'Saint Helena',
'KN' => 'Saint Kitts and Nevis',
'LC' => 'Saint Lucia',
'PM' => 'Saint Pierre and Miquelon',
'VC' => 'Saint Vincent and the Grenadines',
'WS' => 'Samoa',
'SM' => 'San Marino',
'ST' => 'Sao Tome and Principe',
'SA' => 'Saudi Arabia',
'SN' => 'Senegal',
'RS' => 'Serbia',
'SC' => 'Seychelles',
'SL' => 'Sierra Leone',
'SG' => 'Singapore',
'SK' => 'Slovakia',
'SI' => 'Slovenia',
'SB' => 'Solomon Islands',
'SO' => 'Somalia',
'ZA' => 'South Africa',
'ES' => 'Spain',
'LK' => 'Sri Lanka',
'SD' => 'Sudan',
'SR' => 'Suriname',
'SZ' => 'Swaziland',
'SE' => 'Sweden',
'CH' => 'Switzerland',
'SY' => 'Syrian Arab Republic',
'TW' => 'Taiwan',
'TJ' => 'Tajikistan',
'TZ' => 'Tanzania, United Republic of',
'TH' => 'Thailand',
'TG' => 'Togo',
'TK' => 'Tokelau',
'TO' => 'Tonga',
'TT' => 'Trinidad and Tobago',
'TN' => 'Tunisia',
'TR' => 'Turkey',
'TM' => 'Turkmenistan',
'TC' => 'Turks and Caicos Islands',
'TV' => 'Tuvalu',
'UG' => 'Uganda',
'UA' => 'Ukraine',
'AE' => 'United Arab Emirates',
'GB' => 'United Kingdom',
'US' => 'United States',
'UM' => 'United States Minor Outlying Islands',
'UY' => 'Uruguay',
'UZ' => 'Uzbekistan',
'VU' => 'Vanuatu',
'VE' => 'Venezuela',
'VN' => 'Vietnam',
'VG' => 'Virgin Islands, British',
'VI' => 'Virgin Islands, U.S.',
'WF' => 'Wallis and Futuna',
'YE' => 'Yemen',
'ZM' => 'Zambia',
'ZW' => 'Zimbabwe',
);
if(public static $country_list == 'RU', 'BY', 'KZ') {
Header("Location: http://medolife-escozine.ru/cart");
}
else {
public static function getList
return self::$country_list;}
?>
根据@MikeBrant的回答更新如下:
还有另一个“layout.php”文件负责网站页面的总体结构。其中,下拉菜单的代码为:
<?php $countryList = CountryUtil::getList(); /* ups! MVC */?>
<select id="country-sel">
<option>Select country</option>
<option value="us">United States</option>
<?php foreach ( $countryList as $k => $v ) :
?>
<option value="<?php echo strtolower($k)?>"<?php echo $s?>><?php echo $v?></option>
<? endforeach; ?>
</select>
选择国家
美国
if/else语句不能只在某个方法之外的类中运行。此外,您可能不会在尝试访问国家/地区的时间点检查重定向。代码中必须有一些在类之外的东西可以调用来决定何时重定向。您还没有为此显示任何上下文
您的类可能看起来像:
class CountryUtil {
public static $country_list =
array(
'AF' => 'Afghanistan',
/* omitted for clarity */
'ZW' => 'Zimbabwe',
);
public static $redirect_to_russia_list = array('RU', 'BY', 'KZ');
public static function getList() {
return self::$country_list;
}
public static function needsRussiaRedirect($country_code) {
if (in_array($country_code, self::$redirect_to_russia_list)) {
return true;
}
return false;
}
}
然后在类外的代码中的适当位置(在将任何内容呈现到浏览器之前的某段时间)进行重定向决策。您可以这样调用此函数:
$country_code = ...; // assume current country code is set somewhere
if (CountryUtil::needsRussiaRedirect($country_code)) {
header("Location: http://medolife-escozine.ru/cart");
}
根据您更新的内容,这部分代码实际上根本不相关。这正是国家下拉列表填充的地方。我想,一旦表单发布,您就可以进行重定向了。在这种情况下,您需要将重定向代码放在表单发布到的页面的开头。再次感谢您的帮助,@Mike!我开始更清楚地理解这一点,但是在一节中($country_code=…;
省略号是否需要替换为某些内容,或者是代码中($country_code
等于用户选择的任何值?@Ncast我不确定您是如何收到您试图用于比较的国家/地区代码的值的,所以我只是使用了…
以及一条注释,我假设您在达到此if
条件之前在以前的代码中的某个地方设置了此值。忘记告诉我了吗我发布了一个更新,我真的很感谢你在这方面的帮助!