Topic: Linking PHP in a stylesheet to main page

This test code works well; it has variables to add up the widths of images and put the total width in a style in the head section style tags to create a width for a div.

If the style #pictures2d { width: <?php echo $totalwidth3 . "px"; ?>; height: 90px; } is put in a separate stylesheet the PHP won't connect with the variable in the main page. Is it possible to use a session to connect the stylesheet with the main page?

<?php 
$image1_width = "435";
$image2_width = "90";
$totalwidth3 = 30+$image1_width+30+$image2_width+30+$image1_width+30+$image2_width+30+$image1_width+30;
?>  
<style type="text/css">
img { margin-right: 30px; padding: 0; }
img.first { margin-left: 30px; }
.pictures1 { width: 500px; border: 2px solid green; overflow: auto; height: 100px; }
#pictures2d { width: <?php echo $totalwidth3 . "px"; ?>; height: 90px; }
</style>

       

<div class="pictures1">
<div id="pictures2d">
<img class="first" src="images/image1.jpg" alt="image 1"><img src="images/image2.jpg" 
alt="image 2"><img src="images/image1.jpg" alt="image 1"><img src="images/image2.jpg"
 alt="image 2"><img src="images/image1.jpg" alt="image 1">
</div>
</div>

I've tried putting all the php code with the variables in the stylesheet with the styles and renaming the stylesheet with a .php extension but it doesn't work.

EDIT:

I've answered my own question - I put all the php variables and styles in a separate file called style1.php (having added the style tags either side of the styles) so that the variables are in the same file as the style that uses one and used a PHP "include" in the main php file instead of a css file link.

It does get all the styles out of the main page, but in an include file instead of a css file.

Last edited by Wickham (2009-10-27 11:05:40)

Re: Linking PHP in a stylesheet to main page

I've seen another method which uses a link statement to add a css file with a 'php' file extension, and the php file includes a mime type suitable for the css contents.
If I get some free time tonight, I'll see if I can submit a demo.

Last edited by jlhaslip (2009-10-27 20:31:06)