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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.