2012-03-12 76 views
2

我正在修改一个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" /> 
+0

是解决了吗? – 2012-03-12 07:01:16

+0

不,它没有解决。请参阅下面的UPDATE。 – Sumit 2012-03-12 22:05:26

回答

2

加入这一行作为页的第一行common.php

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

请注意'header'调用应该放在任何*输出之前,甚至是空格/换行符。否则PHP会抛出“头文件已发送”的错误。 – 2012-03-12 07:09:37

+0

请参阅下面的UPDATE。 – Sumit 2012-03-12 22:05:34

2

你只需要使用

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

然后你可以在你的css中使用php。你可以在php中添加适当的头文件(text/css)。

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

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

请参阅下面的UPDATE。 – Sumit 2012-03-12 22:05:42

1

我和你有同样的问题,于是我将十六进制颜色更改为数据库中颜色的名称,一切正常。我不知道为什么十六进制颜色不能像其他文本一样出来,但无论如何,只需更改颜色的名称:
E.I .:白色为#FFFFFF
我希望能为您解决问题。
编辑:我想这可能是也有帮助,
当插入到数据库时,使用hexdec()http://www.php.net/manual/en/function.hexdec.php并从数据库中显示时,用dechex()http://php.net/manual/en/function.dechex.php。 dechex将返回十六进制没有#(#),因此你可能需要打印这样的:

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

我希望这是有用的哥们。