Simple If else statments or so I thought {SOLVED}


#1

Ok with the array when an option is chosen i have created if else statements to choose batches because some ore type have the same batch and i plan on the options when i posted this to my site all i get are ERROR repeatedly and does not echo the batch #. Can someone explain what ive done wrong. Thanks in advance. or if you know of a better way to write this that would be greatly appreciated.

[php]
$type_ore = array(“Arkonor” => 1, “Bistot” => 2, “Crokite” => 3, “Dark Ochre” => 4);

if ($type_ore==“1”)
$batch = 250;
else
echo “ERROR”;
if ($type_ore==“2”)
$batch = 200;
else
echo “ERROR”;
if ($type_ore==“3”)
$batch = 250;
else
echo “ERROR”;
if ($type_ore==“4”)
$batch = 400;
else
echo “ERROR”;

echo $batch;
[/php]


#2

Changing your script around, try to use what’s called a switch statement. This is an easy way instead of using IF and ELSE continuously.

[php]
switch($type_ore){
case 1:
$batch = 250;
break;
case 2:
$batch = 200;
break;
case 3:
$batch = 250;
break;
case 4:
$batch = 400;
break;
default:
echo ‘ERROR’;
break;
}
[/php]

Try that and see if you get what you need. You might want to read more about the switch syntax at http://www.php.net/switch.


#3

I agree with OpzMaster on the Switch. If you have to do more than 2 (or maybe 3) options, the switch is the way to go (performancewise is better too).

Anyway, if you really want to do it with an If/Else then anything after the first else must be an elseif

As I looked further I see that you have NESTED your If/Else statements. You should always write your code with some sort of spacing/structure so that it’s clear what you are working with (and which Loop).

Anyway, should consider your OPEN and Close Brackets { } for your ifs and elses.


#4

thank you for the switch when i tested it, it always gives me the value of $batch = 250 in case 1, no matter what selection i make any ideas on why this is happening.

[php]
switch(true){
case ($type_ore=1):
$batch = 250;
break;
case ($type_ore=2):
$batch = 200;
break;
case ($type_ore=3):
$batch = 250;
break;
case ($type_ore=4):
$batch = 400;
break;
default:
echo ‘ERROR’;
break;
}
[/php]


#5

You did the switch wrong. The switch statment should be what I showed you above:

[php]
switch($type_ore){
case 1:
$batch = 250;
break;
case 2:
$batch = 200;
break;
case 3:
$batch = 250;
break;
case 4:
$batch = 400;
break;
default:
echo ‘ERROR’;
break;
}
[/php]

And second the = is an assign character, the == sees if the left is equal to the right.

[php]
$variable = 5; // $variable would turn into 5
$variable == 5; // Checks to see if $variable is equal to 5
[/php]


#6

Thank you for your help solved my issue it was not keeping the variable for the switch so i needed to post some code like this just in case if someone runs into the same problem as me.

[php]
$test = $_POST[‘type_ore’];

switch($test){
case 1:
$batch = 250;
break;
case 2:
$batch = 200;
break;
case 3:
$batch = 250;
break;
case 4:
$batch = 400;
break;
default:
echo ‘ERROR’;
break;
}
[/php]
{SOLVED}