Forum Moderators: coopster

Message Too Old, No Replies

If else statement not working

Simple code, should work, but doesn't

         

scraptoft

6:02 pm on Dec 14, 2007 (gmt 0)

10+ Year Member



Going crazy, theres something wrong but I don't know what. It just seams so simple...please WW help!

$tablecolour = "red";
//
//Check to see what type of id we are handling
//
if ($dbtable == $tablecolour){
$id_type = red_id;}
else{
$id_type = blue_id;}

If I echo $dbtable the ouput is red.
If I echo $id_type the output is blue_id.
Anyone spot the problem?

PHP_Chimp

6:44 pm on Dec 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where are you getting $dbtable from?

$dbtable = 'red';
$tablecolour = "red";
//
//Check to see what type of id we are handling
//
if ($dbtable == $tablecolour){
$id_type = 'red_id';
}
else{
$id_type = 'blue_id';
}
echo $id_type;

You also need to put ' or "'s around any strings, like red_id and blue_id.
As with my example above the output is red_id. So im guessing that your problem lies with getting the value for $dbtable.

scraptoft

9:37 pm on Dec 14, 2007 (gmt 0)

10+ Year Member



Hi PHP Chimp,

I am using $dbtable = $_POST['dbtable']; (posting from a form) to get the value of $dbtable. If I echo $dbtable the output is 'red'.

I replaced my code with yours which works fine, I then replaced $dbtable= 'red'; with the original code $dbtable = $_POST['dbtable'];

Making this change somehow stops it from working.

PHP_Chimp

11:18 pm on Dec 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Humm....
Do you have any line breaks or other white space on the end of that $_POST variable? As this wouldnt show up when you echo the variable, but 'red' and "red\n" are different.

scraptoft

12:53 am on Dec 15, 2007 (gmt 0)

10+ Year Member



You hit the nail on the head.

<input name="dbtable" type="hidden" id="dbtable" value="<?PHP echo " $dbtable";?>" />

I had accidently added a space beore $dbtable when echoing it out in the hidden form.

Can't believe I have wasted so much time on that problem..I suppose that's programming though.

Thanks for your help Chimp.