Hi All,
I am trying to set background image. I retreived it from the database into $xyzimage variable and then trying it to display in the <body > tag.
When i am trying to display it a tabel cell like <td> it works, but not in <body> tag.
Is there a specific way to do this other than :

$xyzimage;
//retreived it from database
echo '<body BACKGROUND='.$xyzimage.'>';

The same $image works while put in a table cell.

Thankyou all in advance.

Recommended Answers

All 17 Replies

Well it could be because the "BACKGROUND" attribute hasn't been used in about 10 years.

<style type="text/css">
  background-image:url('<?php echo $xyzimage ?>');
</style>

Well it could be because the "BACKGROUND" attribute hasn't been used in about 10 years.

<style type="text/css">
  background-image:url('<?php echo $xyzimage ?>');
</style>

Thanks fr the quick reply,
Getting error when i use it..

<style type="text/css">
             background-image:url('<?php echo $profpic ;?>');
       </style>

Parse error: syntax error, unexpected '<'

heh, the style tags can't show up in PHP code.
For future reference echoing out HTML in PHP can create some serious spaghetti code so you should avoid it if possible.
So instead of echoing you would close your PHP tag, then put in those style tags, then re-open your PHP tags

// some code
?>
<style type="text/css">
 ...
</style>
<?php
 // rest of your code

But how is it possible when my image which is retrieved from database is in a php variable:(

If you're inside a PHP script you can open and close your PHP tags as much as you want. Anything not inside the tags will be rendered to the browser as text just as if it were in an HTML file.
The code <?php echo $xyzimage ?> in the CSS is what puts the background image in the CSS

Nope not working this code too..

I tried passing it to switch and echo <body> tag from switch.. but seems no luck here too..

Just do this if you want to assign it to the body:

<style type="text/css">
body{
background-image:url('<?php echo $profpic ;?>');
}
</style>

Note: This should go in the head of the document.

Can someone tell me Whats wrong with the below part of the code :((

I keep getting parse error
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'

//part of the code
echo '
</head>  

';
//$home variable is retrieved from a db 

switch ($homelay){
	case 1:
                echo "<body BACKGROUND="bckgnd\bg1.jpg">";
		break;
	case 2:
                echo "<body BACKGROUND="bckgnd\bg2.jpg">";
		break;	
	case 3:
                echo "<body BACKGROUND="bckgnd\bg3.jpg">";
		break;	
	
       
       default: echo "<body BACKGROUND="bckgnd\bg2.jpg">";
		break;
}

echo ' <table>';
// rest of the code

the above code is the part which keep giving me Parse error as mentioned above.
Any help is appreciated.
Thankyou

Just do this if you want to assign it to the body:

<style type="text/css">
body{
background-image:url('<?php echo $profpic ;?>');
}
</style>

Note: This should go in the head of the document.

Hi thank you fr the reply

i keep getting same parse error ..
when i tried this one

The code assumes that php has been closed with a ?> delimiter prior to the style tag

The code assumes that php has been closed with a ?> delimiter prior to the style tag

I am sorry but i didnt get what you are saying!:(
are you talking about switch statement code ???

No, throw out that completely overkill switch statement and get rid of any attributes and values in the body. We don't do it like that anymore. We use CSS. Here's the basic idea:

<?php 
$profpic="whateverimage.jpg";
?>
<html>
<head>
<style type="text/css">
body{
background-image:url('<?php echo $profpic ;?>');
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<? ob_start(); ?>
<?php
if ( isset($_SESSION['username']))
{

mysql_connect(" ") ;
mysql_select_db(" ");

::::


:::::::

$profpic = $row['profpic'];


?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      
<style type="text/css"> 

body {
        background-image:url('<?php echo $profpic ;?>');   
	margin-left: 10px;
	margin-top: 4px;
	margin-right: 5px;
}
}

</style>

</head>  

<body>

I get no pic displayed this way... hence i thought of using Switch..
I am retreiving $profpic before displaying as background image.. but it wont display :X .
the same $profpic gets displayed when tried in a table <td>..
:confused:

Create a blank php page and put the following in to see how it should work:

<?php 
$profpic="http://www.daniweb.com/alphaimages/logo/logo.gif";
?>
<html>
<head>
<style type="text/css"> 
body {
    background-image:url('<?php echo $profpic ;?>');   
	margin-left: 10px;
	margin-top: 4px;
	margin-right: 5px;
	}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>

After you see how it works, try replacing the $profpic value with your image path.

commented: u r simply gr8!! +1

Hi,
Image got displayed whn used <daniwebimage>.
But when using session variables there seems to be some problem and i get this errormessage..

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent ..on line 5

<? ob_start(); ?>
<?php
session_start();
 
mysql_connect(" ") ;
mysql_select_db(" ");

$displayname = $_SESSION['username'];
$sql = "SELECT profilepic FROM registrations WHERE displayname = '$displayname'";
$result = mysql_query("$sql") ;
$row = mysql_fetch_array( $result );
$profpic = $row['profilepic'];
 echo $displayname; //its displaying
echo $profpic;//image is displaying here
 
?>
      <html>
<head>
    
      <style type="text/css">
    
      body {
   
      background-image:url('<?php echo $profpic ;?>');
   
      margin-left: 10px;
  
      margin-top: 4px;
  
      margin-right: 5px;
  
      }
        </style>
  
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  
    <title>Untitled Document</title>
  
      </head>
  
      <body>
  
      </body>
  
      </html>

     <?ob_flush();?>
      <head>
    
      <style type="text/css">
    
      body {
   
      background-image:url('<?php echo $profpic ;?>');
   
      margin-left: 10px;
  
      margin-top: 4px;
  
      margin-right: 5px;
  
      }
        </style>
  
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  
    <title>Untitled Document</title>
  
      </head>
  
      <body>
  
      </body>
  
      </html>


<?ob_flush();?>

OMG ..
seems like it is working..
I shall code it in the main code and update here..

Thanks a ton buddylee17! Its working now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.