2012-03-13 26 views
0

我的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 

我很奇怪,爲什麼$這不是對象上下文在我的代碼中使用,或者如何將它放在對象上下文中?

+0

看起來。對我來說,就好像你沒有正確使用Zend Framework。您應該通過[快速入門](http://framework.zend.com/manual/en/learning.quickstart.intro.html)學習如何使用它。 – vascowhite 2012-03-13 08:51:11

+0

您的腳本不應該發送文本/ css標題並輸出' Sumit 2012-03-13 18:24:04

回答

-1

只能在類中使用$此,例如

class TestClass { 
    private $name = "Name"; 
    public function echoName() { 
     echo $this->name; // use $this only inside class 
    } 
} 

如果你有對象$用戶爲全局變量,那麼你就需要添加

global $user; 

爲你的函數的第一行getLabelColor(),然後使用它像:

$userid = $user->id; 

瞭解更多關於Classes and Objects在PHP

0

正如你已經確定,問題是代碼行:

$userid = $this->user->id; 

之所以沒有與此代碼的問題是,$this是一個特殊的變量,它是唯一的一個範圍內有效類的方法。

的common.php是一個程序文件,並沒有一個類中定義的,因此$this是不是一個對象,這就是爲什麼你的錯誤消息中。

假設你正在使用Zend_Auth的,你可以改變該行:

$userid = Zend_Auth::getInstance()->getIdentity()->id; 

然而,在Zend框架應用程序的情況下,的common.php是不是你希望的代碼類型看到。相反,您應該將功能移到一組控制器,模型和查看腳本中。

+0

$ userid = Zend_Auth :: getInstance() - > getIdentity() - > id不起作用,它像整個頁面的格式化一樣,像$ userid = $ this-> user-> id;就好像沒有common.php存在。 – Sumit 2012-03-22 00:42:04

+0

這意味着你沒有使用Zend_Auth。 – 2012-03-26 10:40:10

+0

Zend_Auth在控制器級別使用 - - 可能我將不得不實現一個CSS控制器。 – Sumit 2012-03-28 00:51:36

相關問題