2007. 03. 13, kedd keltezéssel 12.12-kor Bruce Gilbert ezt írta: > I stumbled upon this article http://www.chrisjdavis.org/2005/10/16/php-in-css/ > > and was trying out variables with PGP, but can't get it to work. I > wanted to have a variable image that changes on refresh, and also set > the body color with PHP/CSS and maybe get that to change on refresh > too. > > sp from the tutorial mentioned above I have: > > [php] > <?php > /** > * > * create our font class. > * > **/ > > class font { > > /** CONSTRUCTOR > * > * In PHP 4 your constructor has to have the same name as the class > * in PHP 5 it would be thus: > * > * function __construct($args=array()) { > * $this->fields = array'body','image_float'); > * foreach ($this->fields as $field) { > * $this->{"$field"} = $args["$field"]; > * } > * } > * } > * > **/ > > function font($args=array()) { > $this->fields = array('body','image_float'); > foreach ($this->fields as $field) { > $this->{"$field"} = $args["$field"]; > } > } > } > > /** > * > * create our color class. > * > **/ > > class color { > > /** > * > * In PHP 4 your constructor has to have the same name as the class, see above. > * > **/ > function color($args=array()) { > $this->fields = array('body','image_float'); > foreach ($this->fields as $field) { > $this->{"$field"} = $args["$field"]; > } > } > } > > /** > * > * create and setup our color object > * > **/ > > $color = new color(array( > > body => "#000", > > )); > > /** > * > * And now we write our rotation script > * > **/ > > function rotater() { > // we start off with the path to your images directory > $path='images'; > > // srand(time()) will generates the random number we need for our rotater > srand(time()); > for ($i=0; $i < 1; $i++) { > > // here we are assigning our random number to a variable name so we > can call it later. An important note here, you see we are dividing > rand() by 6, 6 is the number of images you have to work with, so if > you have 10, we would: rand()%10) > $random = (rand()%6); > > // so our files in our images folder are named side_image1.gif, > side_image2.gif, side_image3.gif etc. We construct the file name once > we have the random number: > $file = 'image_float'; > $image = $path . $file . $random . '.gif'; > } > > // echo $image here in the function, > } > ?> > [/php] > > In the XHTML I have: > > <div class="image_float"> </div> > and of course a <body> tag. > > in my css I have: > > div.image_float{ > background-image: url(<?php rotater(); ?>) no-repeat; > float:left; > width:75px; > height:75px; > padding:15px; > } > > and also > > body > { > > background-color: <?php echo $color->body; ?>; > } > > in my directory, I have a folder called images which is at the root > level with the index file and css, and the images are called > side_image1, side_image2, side_image3 etc. > > can anyone see whay this isn't working? Neither the background body > color or the rotating image works. does php interpret the file or not? you can decide it if you request the css file directly with a browser. if you see <?php and code in it, it is not interpreted. a possible solution for that: "Use .htaccess to force your .css files to *really* be PHP files: <Files ~.css> ForceType application/x-httpd-php </Files>" (quoted from Richard Lynch's earlier post) another problem may be if you do not send correct content-type header. the first line should be something like <?php header('Content-type: text/css'); ?> before the css stuff. hope that helps Zoltán Németh > > thanks > > -- > ::Bruce:: > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php