PDA

View Full Version : T_ELSE error


mdastous
1-14-05, 10:29 AM
OK, I have looked at this until I am cross-eyed, but I am obviously missing something very simple. What is wrong with the following code?

<?php
$imgname=$profile["imgname"];

if (!empty($imgname)){
$pic="$siteaddress/files/"."$defaultpic";
$dimensions = resize($pic);
}
echo "<img src=\"$pic\" width=\"$dimensions[0]\" height=\"$dimensions[1]\">";

else {
$pic = "$siteaddress/files/".$profile["imgname"];
$dimensions = resize($pic);
}
echo "<img src=\"$pic\" width=\"$dimensions[0]\" height=\"$dimensions[1]\">"; ?>
I am getting the T_ELSE error on line 10 of this code. Any help would be greatly appreciated.

Marc

ticoroman
1-14-05, 10:41 AM
echo "<img src=\"$pic\" width=\"$dimensions[0]\" height=\"$dimensions[1]\">";
The line above is obviously misplaced, as you can not have any output (or anything else) in the middle (outsite { and }) of an IF-ELSE statement!

mdastous
1-14-05, 10:50 AM
Thank you for your reply.

FYI, I'm teaching myself PHP so please be patient.

Essentially what I am trying to do is if condition 1 is TRUE I want to print the default image, else I want to print an image that is stored within the database. What do I need to do to correct this IF statement?

Marc

ticoroman
1-14-05, 10:58 AM
Essentially what I am trying to do is if condition 1 is TRUE I want to print the default image, else I want to print an image that is stored within the database. What do I need to do to correct this IF statement?You need to move the output (echo) inside { }.

Your code should be:<?php
$imgname=$profile["imgname"];

if (!empty($imgname))
{
$pic="$siteaddress/files/"."$defaultpic";
$dimensions = resize($pic);
echo "<img src=\"$pic\" width=\"$dimensions[0]\" height=\"$dimensions[1]\">";

}
else
{
$pic = "$siteaddress/files/".$profile["imgname"];
$dimensions = resize($pic);
echo "<img src=\"$pic\" width=\"$dimensions[0]\" height=\"$dimensions[1]\">";
}

?>

What I meant is that in PHP (or any other language I know about) it's not allowed to have any code in the middle of an IF-ELSE statement outsite { }.

# You can echo here
if($a)
{
# You can echo here
}
# You can NOT echo here, as you'll break the IF-ELSE statement.
else
{
# You can echo here
}
# You can echo here

mdastous
1-14-05, 11:09 AM
Thank you sooo much for that explanation. That makes a lot of sense.

Next question...

The image is not appearing. I have defined the variable $defaultpic as "smiley.jpg" in my config.php and use it as an include at the top of this document.

//Default image
$defaultpic="smiley.jpg";

When a file is uploaded to the dbase ["imgname"] using another script the image appears. What I am missing to get the image to appear from the default?

There is a file called smiley.jpg in my FILES directory.

Marc