Hello all. I am working with some code like this.

echo"<div style='background-image: url(boxshadow2small.jpg);text-align:center; height: 266px; width: 225px; '></div>";

I wanted to be able to control the size of this div with php conditions. I thaught i could do it like this, but its not working.

$someVariable = 200;

echo"<div style='background-image: url(boxshadow2small.jpg);text-align:center; height: $someVariable px; width: 225px; '></div>";

$someVariable is the kind of information i would alter with the use of php control statements. I really would like to get this kind of action working. Does anyone know of something like this being possable? thanks. I guess it would be called dynamic div creation. i duno ,thanks.

Recommended Answers

All 3 Replies

I don't think your code is wrong. It works here and it should work. Try these alternatives, just in case.

echo "<div style='background-image: url(boxshadow2small.jpg);text-align:center; height: {$someVariable}px; width: 225px; '></div>"; 
//OR
echo "<div style='background-image: url(boxshadow2small.jpg);border:1px solid #333; text-align:center; height: ".$someVariable."px; width: 225px; '></div>";

Your code is OK. I have been using this approach quite often (e.g. conditionally applying a background colour to a table). What makes you think the code does not work? Maybe the change just isn't visible. Try changing some other attributes to see whether there is any effect. Try:

$bgrPic1 = 'boxshadow1small.jpg';
$bgrPic2 = 'boxshadow2small.jpg';

echo"<div style='background-image: url($bgrPic1);text-align:center; height: 266px; width: 225px; '></div>";
echo"<div style='background-image: url($bgrPic2);text-align:center; height: 266px; width: 225px; '></div>";

thanks guys big relief

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.