Page 1 of 1

Else If statement not workin

Posted: Sun Aug 26, 2007 6:38 pm
by blade_922
Hey


Here's part of my code where the else if starts

Code: Select all

$theplatform=$row[platform];
$platform1='0';

if ($theplatform=ps3)  { $platform1="images/consoles/old/ps3.gif"; }
elseif ($theplatform=psp) { $platform1="images/consoles/old/psp.gif"; }
elseif ($theplatform=x360) { $platform1="images/consoles/old/x360.gif"; }
elseif ($theplatform=ds) { $platform1="images/consoles/old/ds.gif"; }
elseif ($theplatform=wii) { $platform1="images/consoles/old/wii.gif"; }
else { echo "nothing."; }

  //fill the array with your data 
  $mydata[] = "<table class=\"myown\" cellpadding=\"0\" border=\"0\" align=\"center\"><tr><td colspan=\"2\" $tdreg><center><img border=\"0\" src=\"thumbnailer.php?image=" .$row['url'] ."&type=Media\" border=\"0\"></center></td></tr><tr><td colspan=\"2\" $tdreg2>$row[title]&nbsp;<img src=\"$platform1\"><td></tr><tr><td $tdreg>Updated:</td><td $tdreg>$row[date]</td></tr></table>"; 

}
the console image is displayed ($platform1)
Okay so for some reason it keeps showing the image images/consoles/old/ps3.gif next to each screenshot instead of the correct image depending on what the database has it as.

Ive tried everything.

Any ideas?

Posted: Sun Aug 26, 2007 6:55 pm
by VladSun
1. "=" means "set value to", while "==" means "equal to"
2. use single or double quotes arround your strings. I.e. instead of ps3 you should use "ps3" or 'ps3'

Posted: Sun Aug 26, 2007 7:21 pm
by blade_922
Hey cheers.

Im not new to php but im no expert lol. Im still picking up on little things and i guess you learn from your mistakes. I wont be making this mistake again.

Cheers bud

Posted: Sun Aug 26, 2007 8:15 pm
by Benjamin
I would do something like this..

Code: Select all

$platimages = array('ps3'  => 'ps3.gif',
                    'psp'  => 'psp.gif',
                    'x360' => 'x360.gif',
                    'ds'   => 'ds.gif',
                    'wii'  => 'wii.gif');

$platform1 = 'images/consoles/old/' . (isset($platimages[$theplatform]) ? $platimages[$theplatform] : 'default.gif');
Also, be sure to encapsulate your strings in quotes or PHP is thinking that your looking for the value of a constant. It works because if the engine can't find a constant with that name it will interpret it as a string.