Jump to content

Change Div background colour dependent on PHP variable


jaek

Recommended Posts

Hi,

 

I have looked everywhere for a way to do this and would be grateful for a pointer in the right direction.

 

I want to change the background color of the div with the id of "loggedin", depending on the status. The status currently echos text depending on the value.

 

<div class="grid_12" id="loggedin">

                <div>
	 <ul id="loggedin">
                <li><?php global $current_user; get_currentuserinfo(); echo 'Status : ' . $current_user->status . "\n";?></li>

                <li>
                <?php if ($current_user->status=="RED") echo "Red info"; ?>
                <?php if ($current_user->status=="GREEN") echo "Green info"; ?>
                <?php if ($current_user->status=="BLUE") echo "Blue info"; ?>
	<?php if ($current_user->status=="YELLOW") echo "Yellow info"; ?>

                </li>

                </div>

</div>

 

Thanks

try this:

<div class="grid_12" id="loggedin" STYLE="background-color:<?php echo $current_user->status; ?>">

                <div>
	 <ul id="loggedin">
                <li><?php global $current_user; get_currentuserinfo(); echo 'Status : ' . $current_user->status . "\n";?></li>

                <li>
                <?php if ($current_user->status=="RED") echo "Red info"; ?>
                <?php if ($current_user->status=="GREEN") echo "Green info"; ?>
                <?php if ($current_user->status=="BLUE") echo "Blue info"; ?>
	<?php if ($current_user->status=="YELLOW") echo "Yellow info"; ?>

                </li>

                </div>

</div>

1. Don't use the global keyword, ever. Especially when you are already in the global scope.

 

2. Id's are meant to specify one element by nature, you have specified a <div> and a <ul> with the same id. This defeats the purpose of using an id. Either set unique id's for each element or use a class.

That' s brilliant thanks for the help! I have changed it slightly, as below,  but it is now working.

<div class="grid_12" id="loggedin" style="background-color: <?php global $current_user; get_currentuserinfo(); echo $current_user->status ;?>">

 

I have also sorted out my css and id's :)

 

One more question regarding the colors..

 

So this is now setting BLUE etc as the background colour. Can this be changed to a hex color? so for example if

($current_user->status=="BLUE")

i can asign the color #003954

 

Thanks again

I think you missed the part of AyKays post that said "Dont use global....EVER".  There is a good reason for that, honestly.  You could write some CASE logic to assign a hashcode dependant to each value in the status and use that in the css.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.