从数组中拉出国家/地区,然后使用“if / else”确定是否需要重定向

时间:2014-11-03 18:28:13

标签: php symfony redirect if-statement

我们网站上的某个类别会删除在尝试订购产品时要选择的国家/地区列表,其中包含许多项目。 3个国家 - 俄罗斯,白俄罗斯和哈萨克斯坦 - 需要与名单分开,并由于注册冲突等原因将用户重定向到俄罗斯网站。它过去只是在输入订单页面时在URL中包含国家/地区代码,并从那里根据某些国家/地区切换语言。在这种情况下,语言是完全不敬的。我找到了负责列表的类,以及进入订单页面的功能,并指明了某个国家/地区。我想要包含一个" if / else"在那里的声明,其中"如果该人选择RU,BY或KZ =重定向到另一个网站"或"否则=返回国家代码" 其他似乎没问题,但Dreamweaver一直告诉我," if"语句行和我试着用不同的方式编写语法而没有运气。

    <?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更新每个答案:

还有另一个&#34; layout.php&#34;文件,负责网站页面的一般结构。在那里,下拉菜单的代码是:

<?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>

更新11/05/2014 2:27 pm AST:

根据我之后分享的下拉菜单的代码判断$countryList = CountryUtil::getList(),所以我尝试使用它,以及$countryList = array('RU', 'BY', 'KZ');没有运气。就像我说的那样,这个网站的构建超出了许多试图解决它的开发人员的理解,而仅仅是我。每个页面至少使用4个不同的PHP文件,它们组合在一起创建一个完整的页面。它是页面内容文件,一般网站布局的layout.php,每页上nabigation栏中某些图像更改的_menu.php,以及只有页面标题回显的_title.php。我花了好几个小时试图插入提供的代码&#34;如果 / country是RU,BY,KZ /然后重定向&#34;,那么你很友好地提供,进入上述文件的不同部分没有任何变化。它们都没有包含,所以我当时正在寻找负责组合所有4个或更多文件来创建网页的文件/代码,但没有运气。我会去寻找一位自由职业者,看看他或她是否可以解决这个问题。尽管如此,还是会非常感谢任何其他建议!

1 个答案:

答案 0 :(得分:0)

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");
}

根据您更新的内容,这部分代码根本不相关。这就是填写国家/地区下拉列表的地方。我想,一旦发布表单就是你想要重定向的时候。在这种情况下,您需要在表单发布到的页面的开头放置重定向代码。