seany123 Posted April 6, 2009 Share Posted April 6, 2009 okay what im doing is allowing for my members username to be a different color depending on values from their mysql database... for that to happen currently i have to have this code on EVERY page that shows someones username... <?php // if the player is banned display this.. if ($member['banned'] >= 1) { echo "<b>[b] </b>"; echo "<STRIKE>" .$member['username']. "</STRIKE></td>"; } //if the player has ncolor value of 1 or bigger than show certain color. else if ($member['ncolor'] == 1) { echo "<font color=\"blue\">".$member['username']."</font>"; } else if($member['ncolor'] == 2) { echo "<font color=\"green\">".$member['username']."</font>"; } else if($member['ncolor'] == 3) { echo "<font color=\"yellow\">".$member['username']."</font>"; } else if($member['ncolor'] == 4) { echo "<font color=\"pink\">".$member['username']."</font>"; } else if($member['ncolor'] == 5) { echo "<font color=\"silver\">".$member['username']."</font>"; } //if the player is a staff member show this color. else if($member['staff'] >= 1) { echo "<font color=\"gold\">".$member['username']."</font>"; } //if player has a rm of 1 or bigger show this color. else if($member['rm'] >= 1) { echo "<font color=\"red\">".$member['username']."</font>"; } else { //if player is none of the above show just username. echo "<font color=\"\">".$member['username']."</font>"; } ?> is there a way i could make it so i dont need to add this to every page which displays someones username?? because if in he future i wished to add some "ncolor" values or anything i would have to then re change all the pages. Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/ Share on other sites More sharing options...
laffin Posted April 6, 2009 Share Posted April 6, 2009 make it into a function, through it into a global include file.... Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802531 Share on other sites More sharing options...
jonsjava Posted April 6, 2009 Share Posted April 6, 2009 here's a function you can use that might make it easier for you: <?php function colorMe($member){ $color_array = array("gold","blue","green","yellow","pink","red"); if ($member['banned'] >= 1){ return "<b>[b]</b>\n<STRIKE>$username</strike></td>"; } else { if ($member['staff']){ $color = $color_array[0]; return "<font color=\"$color\">".$member['username']."</font>"; } elseif ($member['ncolor'] >0 && $member['ncolor'] < 6) { $color = $color_array[$member['ncolor']]; return "<font color=\"$color\">".$member['username']."</font>"; } else { return "<font color=\"\">".$member['username']."</font>"; } } } /* colorMe Usage: instead of doing your if elseif else to echo the data, just do this: echo colorMe($member_array); change $member_array to your member array */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802551 Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 the font tag is deprecated .. it will be removed from future versions of HTML so before you go any further, i'd recommend using a style for your code. good practice and will save you a headache going back through your code in the near future to switch all your font tags out for styles. anyways, why not create a session holding the color as per the user. only one line to do that .. when the user logs in just grab it and toss that bad boy straight into a variable, ie. *$row is an example; use whatever you're using to grab stuff outta the db; $_SESSION['color'] = $row['color']; then... <style type="text/css"> .user_color { color : <?php echo $_SESSION['color']; ?>; } </style> then... (tag = whatever html tag you want to use) <tag class="user_color"><?php echo $member['username']; ?></tag> Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802619 Share on other sites More sharing options...
seany123 Posted April 6, 2009 Author Share Posted April 6, 2009 see im not too good at html,css or using class's. but what your saying is using a color value in the mysql database to determine their name... the problem with that is some members have a different type of font like <STRIKE></STRIKE> and that cant be done using database. Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802753 Share on other sites More sharing options...
laffin Posted April 6, 2009 Share Posted April 6, 2009 I think he's saying using the sessions as a cache, as sessions do get destroyed over time. but it shude provide a mechanism to speed up the display process if this is used throughout yer site. The only problem I see is when u get into userlists, as than u have to go through code for each user in the list. Bout the only thing I can suggest, is try compacting the code more. the ifs do there job, but ya may accomplish the same thing with an array (Per user field, i notice you have multiple fields. Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802761 Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 see im not too good at html,css or using class's. but what your saying is using a color value in the mysql database to determine their name... the problem with that is some members have a different type of font like <STRIKE></STRIKE> and that cant be done using database. the few lines of code i put down are all that are required for what i was saying .. simple cut & paste really. ultimately, the method i suggested would assign a member a colour upon registration and enter it into the database under a column named 'colour' or something. then, when a member logs in, that very same colour is taken from the db and put into a $_SESSION .. perhaps $_SESSION['color']? that session var will hold as long as the user is logged in .. it doesn't matter if that session expires 'cause the user will have to sign back in ultimately reassigning their member color. to keep it for a longer time, just toss it in a cookie. doesn't matter. then, just set your condition where if a user is banned, etc., to add different HTML elements to the equation. and anytime the member gets promoted/demoted for whatever reasons, have the color field update accordingly .. or just set up a CURL option to do that automatically. one line is better than 10, especially when you started getting into the hundreds and/or thousands. Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802775 Share on other sites More sharing options...
gizmola Posted April 6, 2009 Share Posted April 6, 2009 No he didn't say anything about sticking things in the database. All he pointed out was that the function provided could be converted so that it no longer used font tags. The proper way to do this is to use around the names. In your style sheet you simply need to define class styles that match the font statements you use, then emit the span with the appropriate .css class. So in your .css file, you would define classes for each of your name styles. For the color that would be as simple as something like this: .staff { color: gold} Now in your function you emit the span with the class tag around the name, in the same general way you are using FONT. Voila, you have started on the important path to xhtml compliance. else if($member['staff'] >= 1) { echo '' . $member['username'] . ''; } Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802791 Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 ya, guess i got a little carried away with how i'd do it. mind you i was just skimming over the idea. losing the font tags is a definite must. Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802801 Share on other sites More sharing options...
seany123 Posted April 6, 2009 Author Share Posted April 6, 2009 okay ive looked over all posts and i must admit im incredibley confused. lol for now im just going to leave the page as it is and just add what i had at the start to all the pages necessary. i will at a later date then revise what has been posted and act accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/152820-a-little-help/#findComment-802865 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.