我的Web应用程序应该允许用户输入标签颜色、背景颜色和字体颜色的十六进制值。我需要能够根据用户输入的保存在MySQL表中的十六进制数值更新背景颜色、标签颜色和字体色色。我的目标是从common.php(这是我的css)开头的数据库中提取十六进制颜色值,并将它们分配给三个颜色变量($clabel、$cbackground和$cfont),并在我的css中使用它们,以便所有颜色更改都在一个位置。但我遇到了一些问题,所有问题的根源是$this不在common.php中的对象上下文中——我想知道我该怎么做才能获得同样的结果。(我还想在这里提到,我尝试在common.php中包含一个类并实例化该类的对象。它不起作用,我的整个网页都失去了commom.php中定义的所有格式)
场景1:
<?php header("Content-type: text/css");
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("shiprequ_sr", $con);
$userid = $this->user->id;
$resultUser = mysql_query("SELECT * FROM users Where Id=$userid");
while($row = mysql_fetch_array($resultUser,MYSQL_NUM))
{
$templateId =$row[15];
}
$resultTemplates = mysql_query("SELECT * FROM templates Where Id=$templateId");
while($row = mysql_fetch_array($resultTemplates,MYSQL_NUM))
{
$clabel = '#'.$row[14];
$cbackground = '#'.$row[13];
$cfont = '#'.$row[16];
}
mysql_close($con);
?>
<style type='text/css'>
.text4
{
color: <?=$clabel?>;// font and bkg colors are referred to likewise
}
以上代码仅适用于$userid=$this->user->id;被替换为特定的用户id,例如$userid=101;由于某些原因$this在common.php.中无法访问
场景:2
<?php header("Content-type: text/css");
include('C:/sr/application/models/getdata.php');
$clabel = getLabelColor();
$cbackground = getBkgColor();
$cfont = getFontColor();
?>
<style type='text/css'>
.text4
{
color: <?=$clabel?>;// font and bkg colors are referred to likewise
}
</style>
getdata.php如下所示:
function getLabelColor() {
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("shiprequ_sr", $con);
$userid = $this->user->id;
$resultUser = mysql_query("SELECT * FROM users Where Id=$userid");
while($row = mysql_fetch_array($resultUser,MYSQL_NUM))
{
$templateId =$row[15];
}
$resultTemplates = mysql_query("SELECT * FROM templates Where Id=$templateId");
while($row = mysql_fetch_array($resultTemplates,MYSQL_NUM))
{
$clabel = '#'.$row[14];
}
mysql_close($con);
return $clabel;
}
//getFontColor() and getBkgColor() defined likewise
这段代码把整个网页搞砸了,就好像css没有eist一样。
当我使用layout.phtml中的相同代码时(作为测试):
<!--In layout.phtml-->
<?php
include('C:/sr/application/models/getdata.php');
echo $clabel = getLabelColor(); //purple 51:0:153
echo $cbackground = getBkgColor(); //Light blue 102:102:204
echo $cfont = getFontColor();
?>
我得到以下错误:
Fatal error: Using $this when not in object context in C:srapplicationmodelsgetdata.php on line 14
我想知道为什么$this不在对象上下文中,因为它在我的代码中使用,或者如何将它放在对象上下文?
正如您所确定的,问题出在代码行:
$userid = $this->user->id;
此代码出现问题的原因是$this
是一个特殊变量,仅在类的方法上下文中有效。
common.php是一个过程文件,没有定义类,因此$this
不在对象内,这就是您收到错误消息的原因。
假设您使用的是Zend_Auth,您可以将该行更改为:
$userid = Zend_Auth::getInstance()->getIdentity()->id;
然而,在Zend Framework应用程序的上下文中,common.php不是您期望看到的代码类型。相反,您应该将功能移动到一组控制器、模型和视图脚本中。
您只能在类中使用$this,例如
class TestClass {
private $name = "Name";
public function echoName() {
echo $this->name; // use $this only inside class
}
}
如果您有对象$user作为全局变量,那么您需要添加
global $user;
作为函数getLabelColor()的第一行,然后像这样使用它:
$userid = $user->id;
阅读php中关于类和对象的更多信息