954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Setting a Background Image problem

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 tag.
When i am trying to display it a tabel cell like it works, but not in 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.

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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>
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

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 '<'

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

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

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Nope not working this code too..

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

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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.

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

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

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 
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 ???

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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>
buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 
<? 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 ..
:confused:

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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.

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

Hi,
Image got displayed whn used .
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();?>
Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

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

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

Thanks a ton buddylee17! Its working now :)

Kavitha Butchi
Junior Poster in Training
69 posts since May 2008
Reputation Points: 10
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You