jaek Posted March 15, 2012 Share Posted March 15, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258989-change-div-background-colour-dependent-on-php-variable/ Share on other sites More sharing options...
Muddy_Funster Posted March 15, 2012 Share Posted March 15, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/258989-change-div-background-colour-dependent-on-php-variable/#findComment-1327687 Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258989-change-div-background-colour-dependent-on-php-variable/#findComment-1327688 Share on other sites More sharing options...
jaek Posted March 15, 2012 Author Share Posted March 15, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258989-change-div-background-colour-dependent-on-php-variable/#findComment-1327711 Share on other sites More sharing options...
Muddy_Funster Posted March 15, 2012 Share Posted March 15, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258989-change-div-background-colour-dependent-on-php-variable/#findComment-1327723 Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 This will be tricky to do solely with PHP unless you build the HTML dynamically inside the PHP from the data gathered. PHP can't modify html elements. You could use JS for this, though. Quote Link to comment https://forums.phpfreaks.com/topic/258989-change-div-background-colour-dependent-on-php-variable/#findComment-1327728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.