Use conditional after retrieving database content from url

Hi everyone,

I’m retrieving some info from a database and I’d like to display a different coloured background and/or image on my webpage, based on the specific value contained in the url.

$q = “SELECT name FROM forum WHERE name = ‘$name’”;

For example, if the url contains the name ‘Peter’ then the page will load with a blue background; if the url contains the name ‘Sarah’ a pink background will load. A lame example but thats all I got.

I have tried the following:


if (name == 'Peter') {$bgcolor=='blue';} else {$bgcolor =='pink';}

print'<h1 style="background-color:"' . $bgcolor . '" >blah blah blah</h1>';


If anyone knows what I can do to get this to work, then please let me know.

Thank you!

Did’t that code work?

Hi RedBishop

Please try with this code:

if ($name == ‘Peter’) {$bgcolor=‘blue’;} else {$bgcolor =‘pink’;}
print’<h1 style=“background-color:‘.$bgcolor.’;” >blah blah blah</h1><br><br>';

If, as you stated, you want the whole page to change color based on the user I would suggest adding a field in the database (for each row) that contains the color.
Then you retrieve the color field from the record (along with the user name)

Another way to accomplish this would be to define a CSS stylesheet for each user. Then use code like this:

<link rel="stylesheet" href"path_to_stylesheets/default.css" />
<link rel="stylesheet" href="path_to_stylesheets/stylefor<? echo $name ?>.css" />

Leveraging the “Cascading” effect of stylesheets, the default will be used if a valid ‘named’ stylesheet does not exist.

Hi guido2004 and others,

thank you for your input!

After some “struggle” I got it working with the following code:

  if ($name == 'Peter') {$bgcolor='blue';} else {$bgcolor ='red';}

Enjoy the day