1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Execute function in CSS?

Discussion in 'CSS' started by supriyadisw, Aug 26, 2006.

  1. #1
    Is it possible to execute some functions in CSS from outsite file? I have a PHP file (eg. file.php) that contain, image function (eg. image() ).

    file.php file
    
    <?php
    function image() {
       $image = image.gif;
       return $image;
    }
    ?>
    Code (markup):
    CSS file
    body {
    background-image: url([B]image()[/B]);
    margin-left: 5px;
    margin-top: 5px;
    margin-right: 5px;
    margin-bottom: 2px;
    }
    Code (markup):
    Is it possible to do this? Perhaps we need include or something? I try insert include file.php into CSS file and negative result :( Need help please. Newbie for CSS here...

    :) Thanks for any help. Have a nice day
     
    supriyadisw, Aug 26, 2006 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    you can give a php file a CSS header - so browsers view a .php file as a css file. Then in your php file you can do the code above
     
    ccoonen, Aug 26, 2006 IP
  3. supriyadisw

    supriyadisw Peon

    Messages:
    203
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can you giving me a little example? Thanks a bunch
     
    supriyadisw, Aug 26, 2006 IP
  4. AdamSee

    AdamSee Well-Known Member

    Messages:
    422
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Unfortunately it's quite inefficient to do that as you'd have to keep downloading the CSS file each image change. I'm guessing you're using this for image rotation? You might want to take a look at:

    http://alistapart.com/articles/randomizer/

    Seperate your style from content and you scripts from style.
     
    AdamSee, Aug 27, 2006 IP
  5. supriyadisw

    supriyadisw Peon

    Messages:
    203
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually I want to implement that script (randomizer) to my CSS style. This script only read one directory. I want choose many directory. In PHP we can use random($folder='foldername').

    background-image: url(<?php random($foldername); ?>);
    Code (markup):
    Can I make this? Or any another trick?

    In other question is how to make random image in CSS file. This is my main question. Thanks for your help.

    Gud lak
     
    supriyadisw, Aug 27, 2006 IP
  6. supriyadisw

    supriyadisw Peon

    Messages:
    203
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Perhaps this is quick and dirty solution:

    background-image: url(folder01.php);
    Code (markup):
    background-image: url(folder02.php);
    Code (markup):
    background-image: url(folder03.php);
    Code (markup):
    So I have 3 file
    folder01.php -> $folder = /folderimages01/
    folder02.php -> $folder = /folderimages02/
    folder03.php -> $folder = /folderimages03/

    Can I use another solutions? Thanks
     
    supriyadisw, Aug 27, 2006 IP
  7. sojic

    sojic Active Member

    Messages:
    133
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    #7
    I am not sure, but try this:

    style.css.php
    
    <?
    header("Cache-Control: no-cache, must-revalidate");
    
    function yourfunction($parametar) {
    // bla bla bla
    }
    
    ?>
    ....
    background-image: url(<?=yourfunction?>);
    
    Code (markup):
     
    sojic, Aug 28, 2006 IP
  8. wmtips

    wmtips Well-Known Member

    Messages:
    598
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #8

    To make this you need to "teach" your php engine to parse .css as .php files and strip all <?php ?> tags.
    Add this line into your .htaccess file, so all .css will be pre-processed by php :

    AddHandler application/x-httpd-php .css

    Maybe you'll need also send a header "Content-type: text/css;" if css processed by PHP, I don't know.
     
    wmtips, Aug 29, 2006 IP
  9. supriyadisw

    supriyadisw Peon

    Messages:
    203
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for your help. Sound so hard. But I'll try it or perhaps search for another solution.

    Thanks a bunch
     
    supriyadisw, Aug 29, 2006 IP
  10. FightRice

    FightRice Peon

    Messages:
    1,082
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #10
    looks pretty complicated, but i'm going to give it a shot to.
     
    FightRice, Aug 30, 2006 IP
  11. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I'm not sure how often the browser would refresh it in this case, but you can also use a php file as an image. Obviously you have the php file return the image header, but the rest is done through the GD library. Read the basics about it if you want to do it that way. The main function you'll need is: ImageCreateFromJPEG() (JPEG can be replaced with GIF, WBMP, or PNG).
     
    Gordaen, Aug 31, 2006 IP
  12. edelman

    edelman Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    the best way to do what i think you're trying to do is this:

    in your stylesheet, have

    style_1 {}
    style_2 {}
    style_3 {}

    and then in your backend code, have a random number generate a number 1, 2, or 3, and insert it into your class, like <div class="style_<? generate number here ?>>

    HTH

    Edelman
     
    edelman, Sep 5, 2006 IP
  13. supriyadisw

    supriyadisw Peon

    Messages:
    203
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks for your help. So we should have a lot of style_X {} with this solution, isn't it?

    Have a nice day
     
    supriyadisw, Sep 5, 2006 IP
  14. hexed

    hexed Peon

    Messages:
    333
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #14
    meh meh meh!

    Don't have your apache engine parse css files as .php, this is extremely wrong as another member already commented.

    You should use different styles and rotate the style type within the php file instead of parsing the css file.

    Hexed
     
    hexed, Sep 6, 2006 IP
  15. supriyadisw

    supriyadisw Peon

    Messages:
    203
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Perhaps it's look like comment no 6? I'm still confusing... sorry...

    Thanks a bunch...

    Have a great day
     
    supriyadisw, Sep 6, 2006 IP
  16. edelman

    edelman Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    just refer to comment 12 :) that's the way to do it.
     
    edelman, Sep 6, 2006 IP
  17. supriyadisw

    supriyadisw Peon

    Messages:
    203
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Solved! Many thanks for your helps. Here my solution:

    - using "AUTOMATIC IMAGE ROTATOR" from http://www.automaticlabs.com/
    - in the rotate.php file use this parameter
    $folder = 'images/' . $_GET['folder'] . '/';
    Code (markup):
    - in CSS file use
    style_1 {background-image: url(rotate.php?folder=folder1)}
    style_2 {background-image: url(rotate.php?folder=folder2)}
    style_3 {background-image: url(rotate.php?folder=folder3)}
    Code (markup):
    - place my images into images/$folder. $folder should be folder1, folder2, and folder3
    - bingo...

    Thanks for all of you guys. I use it for my adsenseBeautifier to make random images near adsense code.

    Any suggestion for a better solution? Thanks...

    Have a wonderfull day and gud lak
     
    supriyadisw, Sep 7, 2006 IP