Skip to main content
Participant
October 11, 2007
Question

PHP inside CSS

  • October 11, 2007
  • 2 replies
  • 528 views
Hullo,

I'm trying to work out if I can add PHP script pulling dynamic images from database, inside CSS "background-image url(xx);".

Like this:

#someID {
background-image: url(../randomimage.php)
}

the PHP script works ok (done with ADDT) and shows image ok when run separately. I get no errors from CSS either, but no picture is shown. One or two sites claim that this should be ok... Maybe i'm doing it the wrong way then.

Any ideas, suggestions?

All help welcome.
This topic has been closed for replies.

2 replies

Inspiring
October 11, 2007
KK74 escreveu:
> Hullo,
>
> I'm trying to work out if I can add PHP script pulling dynamic images from
> database, inside CSS "background-image url(xx);".
......................
>
> #someID {
> background-image: url(../randomimage.php)
> }
......................
I'm using gd images as background for a lot of time without problems.
Se here a useful example, with sources:

http://www.educar.pro.br/public/all/d/
----
zerof
Inspiring
October 11, 2007
.oO(KK74)

> I'm trying to work out if I can add PHP script pulling dynamic images from
>database, inside CSS "background-image url(xx);".
>
> Like this:
>
> #someID {
> background-image: url(../randomimage.php)
> }

Sure. A URL is a URL. It doesn't matter if it points to a static
resource or a script. Just make sure that the script delivers the
correct HTTP headers.

> the PHP script works ok (done with ADDT) and shows image ok when run
>separately. I get no errors from CSS either, but no picture is shown. One or
>two sites claim that this should be ok... Maybe i'm doing it the wrong way
>then.

Can you upload a test case and post the URL?

Micha
Inspiring
October 11, 2007
.oO(KK74)

> that was very quick. I guess the problem is with the http headers. If you have
>quick guidelines to them, please do post.

Whenever a web server returns something to a browser, it sends along
some HTTP headers which describe the content, so the browser knows how
to handle it. If you use a script to deliver something, you have to take
care of these headers yourself.

The most important is the 'Content-Type' header, which should be
'image/jpeg' or 'image/png', dependent on your image type. Without that
the browser won't know what your script is sending.

There are some more like 'Content-Length' or 'Cache-Control', which can
be useful to optimize and speed things up, but they are optional.

To send such headers use the header() function. Be sure that you call
this function first in your script before sending any other output.

http://www.php.net/header

If this doesn't solve the problem, some code or a URL would be helpful.

Micha
KK74Author
Participant
October 15, 2007
Hi,

I must have something terribly wrong with the code. The bannerimg fecthing PHP follows at the end of this posting... (the problem is probably there, but I cannot see it with my very vague understanding of PHP, which relies 99,9% on DW generated code). The page where the img should appear has "Banner" DIV, and the CSS tries to fetch the Background-image using that PHP script. The script shows the test img nice and ok, when run on its own with IE, but actually with firefox it gets another result.... It's very frustrating since the problem is probably very very small :D, or maybe, the whole combination is faulty??

<?php require_once('Connections/connElektra3.php'); ?>
<?php
header('Content-type: image/jpeg');
// Load the tNG classes
require_once('includes/tng/tNG.inc.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_connElektra3, $connElektra3);
$query_rs_bannerbg = "SELECT img_name FROM bannerimages";
$rs_bannerbg = mysql_query($query_rs_bannerbg, $connElektra3) or die(mysql_error());
$row_rs_bannerbg = mysql_fetch_assoc($rs_bannerbg);
$totalRows_rs_bannerbg = mysql_num_rows($rs_bannerbg);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p><img src="<?php echo tNG_showDynamicImage("", "img/banners/", "{rs_bannerbg.img_name}");?>" />
</p>
</body>
</html>
<?php
mysql_free_result($rs_bannerbg);
?>