Adding Backgroundimage depending on PHP variable

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
new2PHP2008
Forum Newbie
Posts: 1
Joined: Tue Apr 22, 2008 2:26 pm

Adding Backgroundimage depending on PHP variable

Post by new2PHP2008 »

Hi,

I have two images image 1 and image 2. I want to insert image 1 as background when a variable test =1 otherwise the image 2 needs to be inserted. I wrote the following code for it.

Code: Select all

<?PHP
            if ($test==1)
            echo "<td width='50%' valign='top'  STYLE=' background-image: url('img/image1.jpg');' >";
            else
            echo "<td width='50%' valign='top'  STYLE= ' background-image: url('img/image2.jpg');' >";
            ?>
 
However none of the background image is displayed.

Please help me with this.
Thanks
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Adding Backgroundimage depending on PHP variable

Post by Zoxive »

You html syntax is wrong, and you can make it more simple.

Code: Select all

<td width="50%" valign="top" style="background-image:url('img/<?php echo $test==1? 'image1.jpg' : 'image2.jpg'; ?>');" >
You can keep it your way if you like though.. you just need to escape the the quotes.

Code: Select all

<?php
if ($test==1)
  echo "<td width='50%' valign='top'  STYLE=\"background-image: url('img/image1.jpg');\" >";
else
  echo "<td width='50%' valign='top'  STYLE=\" background-image: url('img/image2.jpg');\" >";
?>
Post Reply