How to modify CSS dynamically using PHP

1

I need to modify some CSS styles according to a condition in PHP, so I looked it up, and from what I saw it is necessary to include a header in the CSS file and change the file .css to .php , or create a file htacess (but where? where?) ... but I think because they are old, these articles are outdated with some (or all, or none: D) versions of PHP, CSS, Apache or Bootstrap I'm using, or most likely, that it's just me doing something wrong.

The case is that I mounted these panels with nav-tabs of Bootstrap:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>

<div class="row">
    <div class="col-md-2"></div>
    <div class="col-md-8">
        <div class="row">
        <div class="col-md-4">
            <div class="panel panel-success">
                <div class="panel-heading" align="center">
                    <label class="btn" id="cdom">
                        <i class="glyphicon glyphicon-ok">
                        </i>&nbsp; Título</label></div>
                           <ul class="nav nav-tabs">
                                <li class="active"><a data-toggle="tab" href="#sectionA">Resumo</a></li>
                                <li><a data-toggle="tab" href="#sectionB">Entenda</a></li>                                   
                            </ul>

                        <div class="tab-content">
                            <div id="sectionA" class="tab-pane fade in active">
                                Seção A
                            </div>
                            <div id="sectionB" class="tab-pane fade">
                                <h5>Section B</h5>
                            </div>                               
                        </div>
            </div>
        </div> <div class="col-md-8></div>

            </div>
        </div>
    <div class="col-md-2"></div>
</div>

And I've already been able to change the color of panel-header by escaping HTML, according to one condition.

 <div class="panel <?php if ($var1 > 60000){echo 'panel-success';}else {echo 'panel-danger';}?> bs-example" align="center">

But now I need to change the CSS of nav-tab , too, according to the same condition. I already tried to span , but it did not work. I already changed the name of the CSS file, it includes header and I called the .php where the variable is, but it still did not work.

The current CSS style.php :

<?php header('Content-type:text/css');
include "../saida.php";
?>

.nav-tabs > li.active > a,
.nav-tabs > li.active > a:hover,
.nav-tabs > li.active > a:focus {
    color: #020202;
    cursor: default;
    background-color: <?php if ($var1 > 60000){echo '#0e7363';}else {echo '#f2dede';}?> ;
    border-bottom-color: transparent;

}

But it still does not change the color according to the condition.

  

The question

How to include PHP script in the CSS file, without having to change to .php , ie using htacess or equivalent, and how to include PHP within CSS is the correct way I'm doing?)?

    
asked by anonymous 20.05.2015 / 22:34

2 answers

3

As I said in the comment, when loading a style sheet even with HTACCESS, the parameters do not pass, at most pass via GET an ULR such as: <link ... href="localhost/php.css?parametro=XXX">

In order not to have such clear data exposure and to improve the parameter, I will use base64_encode and base64_decode in the $_GET variable in the style sheet file, along with parse_str to create an array with the attributes .

For test purposes, simply change background=f3f3f3 to the value you want. Ideone does not allow HTACCESS, so there is no way to have an online example, but the execution was done as you wish.

HTACCESS

RewriteRule ^home/         /test.php
RewriteRule ^style/php.css /style.php

PHP

<?php
$argument = base64_encode( 'background=f3f3f3' );
?>

<link rel="stylesheet" type="text/css"
href="http://localhost/style/php.css?argument=<?php echo $argument;?>">

CSS

<?php
parse_str( base64_decode( $_GET['argument'] ) , $output );
?>

*{background:#<?php echo $output['background'] ;?>}

HTACCESS is accepting the URL http://localhost/home/ , I did not increase it to make it as educational as possible. You only need to create test.php and style.php .

Note that RewriteRule ^style/php.css /style.php would be as a rule only when you point the path to style/php.css always run style.php without other style sheets being redirected.

    
27.05.2015 / 10:05
2

I do not know what your need to change CSS via PHP, I had this thought in a project, and I did it as you want. However it is not recommended to do so and neither is right.

I'd advise taking a look at JavaScript, Jquery. You could do this in just one line eg:

$(".nav-tabs > li.active > a").on('click', function(){
    $(this).css({'css', '#0e7363'});
});

Using this you can modify both CSS in PHP and direct in HTML etc.

Jquery documentation: link

How to use for CSS manipulation: link

    
21.05.2015 / 00:15