Grischa
Grischa

Reputation: 80

Change CSS background-color with PHP

I will change the background color of some table th. In my example here I used a <div>. I have a script, which seperate 2 different printing outputs in a css tab design, so it is the same website, if you change the tabs.

I want to use for every tab different backround-colors.

I have tried to add a new php variable into one class in my print.php file like `

<class="tab_new '.$css_class.'">

which should only have a css rule only for the backround color.

$css_class = 'classname1' // first case
$css_class = 'classname2' // second case

I will get the error PHP var $css_class is undefinied. But the problem is, that my print.php is requiering_once before the file, where the definition of $css_class is (verb.php).

Please have a look at this 2 files. Maybe my example here isn't correct for my question, but I don't know, how to explain it otherwise.

.blue  {background-color:CornflowerBlue; font-weight:bold}
<div class="blue">My backround is CornflowerBlue</div>
<br><br>
<div class="blue">My backround  should be green</div>

Upvotes: 0

Views: 161

Answers (1)

bassxzero
bassxzero

Reputation: 5041

I don't see $css_class in print.php anywhere, but it looks like your problem is your functions. It looks like you are trying to reference a variable in global space from inside your function. Try adding global $css_class; to the top of the function. php.net/manual/en/language.variables.scope.php

Is this the best solution? IMO no. Google dependency injection and you will learn why it's not the best solution.

Is this the simplest solution? Judging based on the little bit of your code I can see, then I would say probably yes.

Upvotes: 2

Related Questions