showing an image conditionally

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

showing an image conditionally

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the "if" flow control, coupled with empty() maybe...
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

Code: Select all

$image = $results["Image"];
     if (empty($image)) {
     echo "";
     }
	else {
	echo "<img src='image/image.gif' ></a><bR>"; 
     }
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post 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?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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>";
     }
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
}
Post Reply