我正在修改一个web应用程序。它允许用户输入标签颜色,背景颜色和字体颜色的十六进制值。当用户输入值并点击“更新模板”时。我需要能够根据用户输入的十六进制值更新背景颜色,标签颜色和字体颜色。我将标签颜色,背景颜色和字体颜色与其他模板信息一起存储在MySQL表中。我能做的所有.phtml
文件控制视图:css文件中的动态内容
$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文件一样。我的直觉是我不允许在css中的heredoc中实例化一个对象,即使我将其重命名为common.php。在这种情况下可以采取哪种解决方法?
更新: 那么我已经包括头(“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>
令人惊讶这个工作。但是,当我试图用
$Idtemplate = $this->user->defaultTemplate;
$Objtemplate = new Template();
$Thetemplate = $Objtemplate->fetchRow("id=" . $Idtemplate);
$clabel = $Thetemplate->descriptionlabelColor;
return ('#'.$clabel);
更换
$clabel = '#001122';
(以$ cbackground和$的CFont在各自的功能一起)就停止工作。所以我搬了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" />
是解决了吗? – 2012-03-12 07:01:16
不,它没有解决。请参阅下面的UPDATE。 – Sumit 2012-03-12 22:05:26