Page 1 of 1

Adding Backgroundimage depending on PHP variable

Posted: Tue Apr 22, 2008 2:30 pm
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

Re: Adding Backgroundimage depending on PHP variable

Posted: Tue Apr 22, 2008 3:46 pm
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');\" >";
?>