我正在修改一个网络应用程序。它允许用户输入标签颜色、背景颜色和字体颜色的十六进制值。当用户输入值并单击“更新模板”时。我需要能够根据用户输入的十六进制值更新背景颜色、标签颜色和字体颜色。我将标签颜色、背景颜色和字体颜色以及其他模板信息存储在 MySQL 表中。我可以在控制 View 的所有 .phtml
文件中执行以下操作:
$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
用于 .phtml
中的颜色样式。
问题是大约有 34 个 .phtml
文件。如果我想这样做,我必须找到负责颜色的 div 类并在那里进行相应的更改。对我来说,这听起来是一个非常低效且冗长的解决方案。
但是 css 文件中有大约 4-5 个类,其中定义和使用了所有颜色值。对我来说,在 css 文件中执行类似上述代码的操作(唯一的问题是它在 css 文件中不起作用)并像下面这样引用它们(我已重命名我的 common.css
作为common.php
):
.panel1
{
width: auto;
height: 22px;
background-color: <?= $cbackground ?>;
padding: 5px;
}
当我尝试包含从数据库获取颜色信息的代码时,它会弄乱所有格式,就好像程序无法看到我的 .css 文件一样。我的预感是,即使我将其重命名为 common.php,我也不允许在 css 中实例化定界文档内的对象。在这种情况下,什么是解决方案?
更新: 好吧,我已经包含 header("Content-type: text/css");但我再次尝试了以下操作以确保:
<?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>
不幸的是,这不起作用。我也尝试过:
<?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>
其中 getdata.php 包含代码:
<?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;
?>
它也不起作用。作为第四个变体,我尝试定义一个函数并从 common.php 调用它:
<?php
function getLabelColor() {
$clabel = '#001122';
return $clabel;
}
function getBkgColor() {
$cbackground = '#002233';
return $cbackground;
}
function getFontColor() {
$cfont = '#004455';
return $cfont;
}
?>
其中 common.php 有:
<?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>
令人惊讶的是这有效。但是当我尝试替换
$clabel = '#001122';
(以及相应函数中的 $cbackground 和 $cfont)和
$Idtemplate = $this->user->defaultTemplate;
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);
$clabel = $Thetemplate->descriptionlabelColor;
return ('#'.$clabel);
它停止工作了。因此,我将 getdata.php 移至包含上述所有函数的模型类:
<?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);
}
}
?>
我调用的函数如下:
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();
这只适用于 .phtml,但不适用于 common.php。我想 common.php 不允许实例化对象。
PS:这就是我的 common.php 调用的样子:
<link rel="stylesheet" type="text/css" href="<?php echo $this->baseUrl();?>/css/common.php" />
最佳答案
你只需使用
<link rel="stylesheet" type="text/css" media="screen" href="/css/mycss.php" />
然后你就可以在你的css中使用php了。您可以在 php 中添加适当的 header (文本/css)。
// mycss.php
<?php
header("content-type:text/css");
?>
<style type='text/css'>
// Stylesheet
</style>
关于php - css 文件中的动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9662789/