My Web app should allow the user to input hex values for label-color, background-color and font-color.I need to be able to update the background color, label color and font color depending on the hex values that the user inputs which are saved in a MySQL table. My goal is to pull the hex color values from the database at the beginning of the common.php (which is my css) and assign them to three color variables ($clabel, $cbackground and $cfont) and use them inside my css so that the all the color changes are in one place. But I am running into some problems and the root of all the problem is that $this is not in object context inside common.php - - - I was wondering what an I do to achieve the same result going forward. (I also want to mention here that I tried including a class and instantiating an object of that class inside common.php. It does not work and my whole webpage loses all its formatting defined in commom.php)
Scenario 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
}
The above code only works when $userid = $this->user->id; is replaced with a specific user id for example $userid = 101; For some reason $this is not accessible in common.php.
Scenario: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 looks like following:
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
This code messes up the whole webpage as if the css does not eist.
When I use the same code from layout.phtml (as a test):
<!--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();
?>
I get the following error :
Fatal error: Using $this when not in object context in C:\sr\application\models\getdata.php on line 14
I am wondering why $this is not in object context as it is used in my code or how can put it in object context?
You can use $this only in class, for example
class TestClass {
private $name = "Name";
public function echoName() {
echo $this->name; // use $this only inside class
}
}
If you have object $user as global variable, then you need to add
global $user;
as first line of your function getLabelColor() and then use it like that:
$userid = $user->id;
Read more about Classes and Objects in php
As you have determined, the problem is with the code line:
$userid = $this->user->id;
The reason that there is a problem with this code is that $this
is a special variable that is only valid within the context of a method of a class.
common.php is a procedural file and does not have a class defined and hence $this
is not within an object, which is why you get the error message.
Assuming you're using Zend_Auth, you could change that line to:
$userid = Zend_Auth::getInstance()->getIdentity()->id;
However, in the context of a Zend Framework application, common.php is not the type of code you'd expect to see. Rather, you should move the functionality to a set of controllers, models and view scripts.