简体   繁体   中英

Dynamic content in css file

I am modifying a web app. It allows the user to input hex values for label-color, background-color and font-color. When the user input the values and clicks "update template". I need to be able to update the background color, label color and font color depending on the hex values that the user inputs. I am storing the label-color, background-color and font-color along with other template information in a MySQL table. I can do the following in the all the .phtml files that control the view:

$Idtemplate = $this->user->defaultTemplate;
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);
$Title_Shiprequest = $Thetemplate->descriptionShipRequestTitle;
$clabel = $Thetemplate->descriptionlabelColor;
$cbackground = $Thetemplate->descriptionBkgColor;
$cfont =$Thetemplate->descriptionFontColor;

$clabel , $cbackground , $cfont are used for the color styling in the .phtml .

The problem is there are about 34 .phtml files. If I want to do it this way I have to find the div classes responsible for the colors and do the respective changes there. It sounds like a very inefficient and lengthy solution to me.

But there are about 4-5 classes in the css file where all the color values are defined and used. Its much easier and efficient for me to do something like the above mentioned code in css file (the only problem is it does not work in css file) and refer to them like below (i have renamed my common.css as common.php ):

.panel1
{
    width:                      auto;
    height:                     22px;
    background-color:           <?= $cbackground ?>;
    padding:                    5px;    
}

When I try to include the code to fetch color information from the database it messes up all the formatting as if the program cant see my .css file. My hunch is that I am not allowed to instantiate an object inside a heredoc in a css even though I renamed it as common.php. What could be a workaround solution in this situation?

UPDATE: Well I was already including header("Content-type: text/css"); But i tried the followings again just to make sure:

<?php 
header("Content-type: text/css");

$Idtemplate = $this->user->defaultTemplate;
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);
$clabel = $Thetemplate->descriptionlabelColor;
$cbackground = $Thetemplate->descriptionBkgColor;
$cfont =$Thetemplate->descriptionFontColor;
?>
<style type='text/css'>
.text4                          
{
color:                    <?=$clabel?>;// font and bkg colors are referred to likewise  
}
</style>

Unfortunately this did not work. I also tried:

<?php 
header("Content-type: text/css");
include('C:/sr/public/css/getdata.php');
?>
<style type='text/css'>
.text4                          
{
color:                    <?=$clabel?>;// font and bkg colors are referred to likewise  
}
</style>

where getdata.php has the code:

<?php
$Idtemplate = $this->user->defaultTemplate;
include('C:/sr/application/models/Template.php');
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);
$clabel = $Thetemplate->descriptionlabelColor;
$cbackground = $Thetemplate->descriptionBkgColor;
$cfont =$Thetemplate->descriptionFontColor;
?>

It did not work either. As a fourth variation I tried to define a function and call it from common.php:

<?php

function getLabelColor() { 

$clabel = '#001122';
return $clabel;
}

function getBkgColor() { 

$cbackground = '#002233';
return $cbackground;
}

function getFontColor() { 

$cfont = '#004455';
return $cfont;
}
?>

where common.php has :

<?php 
header("Content-type: text/css");
include('C:/sr/public/css/getdata.php');
?>
<style type='text/css'>
.text2                          
{
color: <?php echo getLabelColor();?>; /* Bkg and font colors are referred to likewise*/
}
</style>

Surprising this worked. But when I tried to replace

$clabel = '#001122'; 

(along with $cbackground and $cfont in the respective functions) with

$Idtemplate = $this->user->defaultTemplate;
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);
$clabel = $Thetemplate->descriptionlabelColor;
return ('#'.$clabel);

it stopped working. So I moved getdata.php to models made class that included all the above mentioned functions:

<?php
Class fetchData extends Template
{

function getLabelColor() { 


$Idtemplate = 101// I had assign a known value here. I was not able to use $this->user->defaultTemplate; Not sure why I wont be able to use $this
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);
$clabel = $Thetemplate->descriptionlabelColor;//for future usage for dynamic color

return ('#'.$clabel);
}

function getBkgColor() { 


$Idtemplate = 101// I had assign a known value here. I was not able to use $this->user->defaultTemplate; Not sure why I wont be able to use $this
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);

$cbackground = $Thetemplate->descriptionBkgColor;//for future usage for dynamic color

return ('#'.$cbackground);
}

function getFontColor() { 


$Idtemplate = 101// I had assign a known value here. I was not able to use $this->user->defaultTemplate; Not sure why I wont be able to use $this
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);

$cfont =$Thetemplate->descriptionFontColor;//for future usage for dynamic color
return ('#'.$cfont);
}

}
?>

I was calling the functions like below:

include('C:/sr/application/models/getdata.php');
$datafetchObj  = new fetchData();

$clabel = $datafetchObj->getLabelColor(); //purple 51:0:153
$cbackground = $datafetchObj->getBkgColor(); //Light blue 102:102:204
$cfont = $datafetchObj->getFontColor(); 

This only worked from .phtml but not from common.php. I suppose common.php is NOT allowed to instantiate an object.

PS: This what my common.php call looks like:

<link rel="stylesheet" type="text/css" href="<?php echo $this->baseUrl();?>/css/common.php" />

将此行添加为common.php中页面的第一行:

header("Content-type: text/css");

You just use

<link rel="stylesheet" type="text/css" media="screen" href="/css/mycss.php" />

and then you can use php inside your css. you could add appropriate headers (text/css) with header in php.

 // mycss.php
 <?php
 header("content-type:text/css");
 ?>

<style type='text/css'>
// Stylesheet
</style>

I was having the same problem as you, then I changed the hex color to the name of the color in the database and everything worked fine. I don't know why the hex color can't come out like other texts, but anyways, simply change the color the name:
EI: white for #FFFFFF
I hope that worked out for you.
EDIT: I thought that could be helpful too,
When inserting to database, use hexdec() http://www.php.net/manual/en/function.hexdec.php and when displaying from database, use dechex() http://php.net/manual/en/function.dechex.php . dechex will return the hex without the hash (#) though, so you might need to print it like that:

<?php echo "#".dechex($decimal_number); ?>

I hope that was useful buddy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM