I purchased a script a while ago (PHP/MySQL) that contains a user registration form. 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).
I need to find a way for the code to allow for up to 15 animals instead of just 2 results. Here is the code. "profile_userinfo_gender1" and "gender2" are what call the "gender" or "animal". They should go up to "gender15". 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. The if else construct only allows 2 possibilities (even though you could use if elseif elseif.. And so on but that's not nice).
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. 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 .
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.
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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.