I simulated what you wanted to do and it's working, I used only if's instead switch-cases (not to mix if's and switch-cases since they both do the same):
PHP Code:
function items_genderf($item, $adopt){
$mysidia = Registry::get("mysidia");
//Let's check if the adoptable is already female.
if($adopt->gender == 'f')
{ //The adoptable is already female
$note = "Your adoptable is already female.";
}
else{
//The adoptable is male
$mysidia->db->update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner ='{$item->owner}'");
$delitem = $item->remove();
$note = "Your adoptable {$adopt->name} is now female.";
}
return $note;
}
Just switch:
PHP Code:
$mysidia = Registry:: get("mysidia");
//Let's check if the adoptable is already female.
switch($adopt->gender)
{
case "f":
$note = "Your adoptable is already female.";
break;
case "m":
$mysidia->db->update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner='{$item->owner}'");
$note = "Your adoptable is now female.";
$delitem = $item->remove();
break;
default:
$note = "It appears your adoptable doesn't have a gender.";
}
return $note;
You can also use your own code, you one error only. In the switch-case, it should be case 'm'. Case 'f' means "if it is female, change to female.", so case 'm' means "If it is male, change to female.".
Also, the item deletes anyway at the end because it's not inside the the case. You have to delete the item if it succeeds, so it should be inside the case.
Also each case must end with break; so it knows when to stop.
Since your adopt was male, it was reaching the switch-case (the else). But it would only activate if it was female, so it ignored it and proceeded to item deletion so that's why it was decreasing and not changing the gender. To avoid this kind of confusion, you only delete the item if the switch-case is sucessful.
With your own code:
PHP Code:
$mysidia = Registry:: get("mysidia");
//Let's check if the adoptable is already female.
$gender = $mysidia -> db -> select ("owned_adoptables", array("gender"), "aid='{$adopt->aid}' and owner ='{$item->owner}'") -> fetchColumn();
if($gender == "f") {
//The adoptable is already female
$note = "Your adoptable is already female.";
}
else{
//The adoptable is male. It's gender can be switched to female.
switch($adopt->gender){
case "m":
$mysidia -> db -> update("owned_adoptables", array("gender" => 'f'),"aid='{$adopt->aid}' and owner ='{$item->owner}'");
$note = "Your adoptable {$adopt->name} is now female.";
//Update item quantity...
$delitem = $item->remove();
break;
}
}
return $note;
One thing, you really don't need to get the gender from a select, it's ok to just do $adopt->gender