Page 1 of 1

showing an image conditionally

Posted: Sun Oct 23, 2005 4:43 pm
by kristie380
I am trying to get my script to show an image if a field has an entry and to not show the image if the field does not have an entry. How could I do this?

Posted: Sun Oct 23, 2005 4:45 pm
by feyd
the "if" flow control, coupled with empty() maybe...

Posted: Mon Oct 24, 2005 1:44 pm
by $var

Code: Select all

$image = $results["Image"];
     if (empty($image)) {
     echo "";
     }
	else {
	echo "<img src='image/image.gif' ></a><bR>"; 
     }

Posted: Mon Oct 24, 2005 3:35 pm
by kristie380
Ok here is my code that I tried:

Code: Select all

$email = $result["email"]; 
     if (empty($email)) { 
     echo ""; 
     } 
    else { 
    echo "<a href=\"mailto:$email\"><img src=\"images/mail.gif\" border=\"0\"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; 
     }
I want to show an image with a link to the person's email address if they entered an e-mail address. So the field name I am checking for a entry in is called "email". The code above did not work. Did I miss something?

Posted: Mon Oct 24, 2005 4:16 pm
by Zoxive

Code: Select all

$email = $result["email"];
     if (empty($email)) {
     echo "";
     }
    else {
    echo "<a href=\"mailto:" . $email . "\"><img src=\"images/mail.gif\" border=\"0\"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
     }
-NSF

Posted: Mon Oct 24, 2005 4:31 pm
by $var
what you want is this:

Code: Select all

$image = $results["Image"];
     $email = $results["'Email"];
     if (empty($image)) {
     echo "";
     }
    else {
    echo "<a href="mailto:$email?subject=Subject Line"><img src='image/image.gif' ></a><bR>";
     }

Posted: Tue Oct 25, 2005 12:26 am
by Jenk

Code: Select all

if (!empty($image)) {
    echo "<img src=" //etc
}
However, if you are basing this upon the click of a button on a form, which will be referred to via the $_POST array, then you should use:

Code: Select all

if (isset($_POST['image'])) {
    echo "<img src=" //etc
}