简体   繁体   English

注册脚本代码上的PHP“else”和“if”

[英]PHP “else” and “if” on code for registration script

I purchased a script a while ago (PHP/MySQL) that contains a user registration form. 我刚刚购买了一个包含用户注册表单的脚本(PHP / MySQL)。 The script has an area for selection of Gender (Male or Female). 该脚本有一个选择性别(男性或女性)的区域。 Two options. 两种选择。

I want to modify this code to allow for selection of an Animal instead of a sex. 我想修改此代码以允许选择动物而不是性别。 I changed the code in all applicable places, and it works great. 我在所有适用的地方更改了代码,效果很好。 The problem is, it only displays animals 1 and 2, since the initial code was setup for a "male/female" selection (2). 问题是,它只显示动物1和2,因为初始代码是为“男/女”选择设置的(2)。

I need to find a way for the code to allow for up to 15 animals instead of just 2 results. 我需要为代码找到一种方法,允许最多15只动物而不是仅仅2种结果。 Here is the code. 这是代码。 "profile_userinfo_gender1" and "gender2" are what call the "gender" or "animal". “profile_userinfo_gender1”和“gender2”是所谓的“性别”或“动物”。 They should go up to "gender15". 他们应该达到“性别15”。 Please help! 请帮忙! Thank you! 谢谢!

    if ($D->u->gender == 0) $D->gender = $this->lang('profile_userinfo_withoutinfo');
    else {
        if ($D->u->gender == 1) $D->gender = $this->lang('profile_userinfo_gender1');
        else $D->gender = $this->lang('profile_userinfo_gender2');
    }

You need to change from a if else to a for. 您需要从if else更改为for。 The if else construct only allows 2 possibilities (even though you could use if elseif elseif.. And so on but that's not nice). if else构造只允许2种可能性(即使你可以使用if elseif elseif ..依此类推,但这并不好)。

Use a for loop to loop over your 15 possibilities and store some kind of array which gives you the according animals for each value of the gender variable. 使用for循环遍历15种可能性并存储某种数组,该数组为性别变量的每个值提供相应的动物。 Example: 例:

for ($i=0; $i < $animals.length $i++)  {
    if ($D->u->gender == $i){
        $this->lang($animals[i]);
        break;
    } 
} 

As Norbert van Nobelen mentioned, I'd also strongly recommend w3schools . 正如Norbert van Nobelen所说,我也强烈推荐w3schools

 if ($D->u->gender == 0) $D->gender = $this->lang('profile_userinfo_withoutinfo');
else {
    if ($D->u->gender == 1) $D->gender = $this->lang('profile_userinfo_gender1');
    else if($D->u->gender == 2) $D->gender =  $this->lang('profile_userinfo_gender2');
    else if($D->u->gender == 3) $D->gender =  $this->lang('profile_userinfo_gender3');
etc...
}

There are smarter ways to do this by using a good drop down definition with key and value. 有一些更聪明的方法可以通过使用具有键和值的良好下拉定义来实现此目的。 See w3schools.org for that. 请参阅w3schools.org。

if ($D->u->gender == 0) $D->gender = $this->lang('profile_userinfo_withoutinfo');
else {
    $total_gender = 15; //define total gender
    for($i = 1;$i <= $total_gender;$i++){
    if ($D->u->gender == $i) $D->gender = $this->lang("profile_userinfo_gender$i");
     }
    #Add an gender generic default. 
    if ($D->u->gender == $i) $D->gender = $this->lang("profile_userinfo_gender_default");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM