我的Web應用程序應允許用戶輸入標籤顏色,背景顏色和font-color.I需要能夠根據用戶輸入的保存在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 = $這個 - >用戶> ID上面的代碼只能; 被替換爲特定的用戶ID,例如 $ userid = 101; 由於某些原因$這是不能在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:\sr\application\models\getdata.php on line 14
我很奇怪,爲什麼$這不是對象上下文在我的代碼中使用,或者如何將它放在對象上下文中?
看起來。對我來說,就好像你沒有正確使用Zend Framework。您應該通過[快速入門](http://framework.zend.com/manual/en/learning.quickstart.intro.html)學習如何使用它。 – vascowhite 2012-03-13 08:51:11
您的腳本不應該發送文本/ css標題並輸出' – Sumit 2012-03-13 18:24:04