Php Include in css

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dddmx3
Forum Newbie
Posts: 16
Joined: Mon Sep 20, 2010 9:05 pm

Php Include in css

Post by dddmx3 »

This code generates a product image:

Code: Select all

<?php echo ps_product::image_tag( urldecode($product_thumb_image), 'class="browseProductImage" border="0" title="'.$product_name.'" alt="'.$product_name .'"' ) ?>
The code above generates a url to be used as a image for the given product. I want to set that generated url as the background of a css div. How would I be able to include php in css?

This obviously wouldn't work:

[text].style {
background: <?php echo ps_product::image_tag( urldecode($product_thumb_image), 'class="browseProductImage" border="0" title="'.$product_name.'" alt="'.$product_name .'"' ) ?>;
}[/text]
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Php Include in css

Post by Celauran »

Hard to say for certain without seeing the code for ps_product::image_tag, but I'd try something like this:

Code: Select all

.style
{
    background-image: url('<?php echo urldecode($product_thumb_image); ?>');
}
dddmx3
Forum Newbie
Posts: 16
Joined: Mon Sep 20, 2010 9:05 pm

Re: Php Include in css

Post by dddmx3 »

When I put it in the css, it showed up like this live:

.itemImage {
background: url("<?php echo urldecode($product_thumb_image);
}

It's like it's not even seeing the php. Then again it is a stylesheet...
User avatar
azycraze
Forum Commoner
Posts: 56
Joined: Mon Oct 24, 2011 12:08 pm
Location: India

Re: Php Include in css

Post by azycraze »

save the file as '.php' instead of '.css'.
and instead of linking css file,include the php file.Then you will be able to write the code as Celauran said.
Do
1) if your css file is 'css.css' make it a php file (say css.php)
2) include css.php where you have linked the css.css.

The css.php will look as
======================
<style>
.............
...............
...your codes..
............
#your_div_name {

background: url("<?php echo urldecode($product_thumb_image);

}
............
.............
</style>

======================================
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Php Include in css

Post by Celauran »

dddmx3 wrote:When I put it in the css, it showed up like this live:

.itemImage {
background: url("<?php echo urldecode($product_thumb_image);
}

It's like it's not even seeing the php. Then again it is a stylesheet...
Since you were trying to include some PHP, I assumed you were referring to CSS defined in the head of the document.
Post Reply