Click to See Complete Forum and Search --> : function question


Genixdeae
01-25-2006, 02:31 PM
Ok, I have this function(see below) which is supposed to grab the correct things to display depending on what 'race' is being input. the code to get the function is like this:echo race_commodities(1, 1); //should output Metal, and it does
echo race_commodities(1, 2); //should output Oil, but it doesn't, it puts Metal i'm not sure what's not working, logically to me, it should be working//this first function will helps us grab the names of the commodities depending on the race
function race_commodities($race, $which_comm){
if($race == 1) {//They have chosen humans
$commodity1 = "Metal";
$commodity2 = "Oil";
$commodity3 = "Gas";
$power = "Electricity";
if($which_comm = 1) {//They want to see the first commodity
return $commodity1;
}elseif($which_comm = 2) {//They want to see the second commodity
return $commodity2;
}elseif($which_comm = 3) {//They want to see the third commodity
return $commodity3;
}elseif($which_comm = 4) {//They want to see the power
return $power;
}else{//They want to return a commodity that doesn't exsist
return "Error: Unknown commodity.";
}
}elseif($race == 2) {//They have chosen Chronthos
$commodity1 = "Comm1";
$commodity2 = "Comm2";
$commodity3 = "Comm3";
$power = "Power";
if($which_comm = 1) {//They want to see the first commodity
return $commodity1;
}elseif($which_comm = 2) {//They want to see the second commodity
return $commodity2;
}elseif($which_comm = 3) {//They want to see the third commodity
return $commodity3;
}elseif($which_comm = 4) {//They want to see the power
return $power;
}else{//They want to return a commodity that doesn't exsist
return "Error: Unknown commodity.";
}
}elseif($race == 3) {//They have chosen Prostiegans
$commodity1 = "Protons";
$commodity2 = "Electrons";
$commodity3 = "Neutrons";
$power = "Particle Conductor";
if($which_comm = 1) {//They want to see the first commodity
return $commodity1;
}elseif($which_comm = 2) {//They want to see the second commodity
return $commodity2;
}elseif($which_comm = 3) {//They want to see the third commodity
return $commodity3;
}elseif($which_comm = 4) {//They want to see the power
return $power;
}else{//They want to return a commodity that doesn't exsist
return "Error: Unknown commodity.";
}
}else{//They have some how chosen a race that doesn't exsist
$commodity1 = "Error";
$commodity2 = "Error";
$commodity3 = "Error";
$power = "Error";
if($which_comm = 1) {//They want to see the first commodity
return $commodity1;
}elseif($which_comm = 2) {//They want to see the second commodity
return $commodity2;
}elseif($which_comm = 3) {//They want to see the third commodity
return $commodity3;
}elseif($which_comm = 4) {//They want to see the power
return $power;
}else{//They want to return a commodity that doesn't exsist
return "Error: Unknown commodity.";
}
}
}
I am completly confused by this and odds are it's something really simple, need more info jus let me know.

Wisest Guy
01-25-2006, 02:39 PM
I wouuld use an array:

function race_commodities($race,$which_comm)
{return Array(Array("comm1","comm2","comm3",...),Array(...)...)[$race - 1][$which_comm - 1];}

Genixdeae
01-25-2006, 02:56 PM
I havn't done much w/ arrays, I understand what you're saying it would do but it's not workin and i dont know enough about arrays to fix it....it doesn't like the[$race - 1][$which_comm - 1];

NogDog
01-25-2006, 06:43 PM
You want to compare, not assign:

if($which_comm == 1)

Wisest Guy
01-25-2006, 06:51 PM
This should work:

function race_commodities($race,$comm)
{$Arr = Array(Array("comm1","comm2","comm3",...),Array(...)...);
$Comms = $Arr[$race - 1];
$Comm = $Comms[$comm - 1];
if ($Comm == ""){$Comm = "Error: Unknown commodity.";}
return $Comm;}

NogDog
01-25-2006, 07:04 PM
How I might do it with the array approach:

function race_commodities($race, $which_comm)
{
$raceData = array(1 => array(1 => "Metal",
2 => "Oil",
3 => "Gas",
4 => "Electricity"),
2 => array(1 => "Comm1",
2 => "Comm2",
3 => "Comm3",
4 => "Power"),
3 => array(1 => "Protons",
2 => "Neutrons",
3 => "Electrons",
4 => "Particle Conductor"));
if(array_key_exists($race, $raceData))
{
if(array_key_exists($which_comm, $raceData[$race]))
{
return($raceData[$race][$which_comm]);
}
else
{
return("ERROR: Unknown commodity");
}
}
else
{
return("ERROR: Unknown race");
}
}

Genixdeae
01-26-2006, 12:39 AM
lol, love you guys, never woulda guessed any of that :P, thanks bundles