View Full Version : Random hex color
Trommsdorff
05-21-2006, 11:13 PM
Hello,
I have some PHP code that generates a random hex color code. I would like to incorporate it for use in vbulletin (it has to be server side). In a normal PHP file, the output is something like $randomcolor.
Does anyone know how to get that to be displayed properly within VB using the plugin system (if that's the proper way to do it)?
Adrian Schneider
05-21-2006, 11:15 PM
Stick the PHP code in a new plugin using the "global_start" hook. $randomnumber should then be available to use in most templates.
Best way is to put the code in a plugin for global_start and then call the variable you are using for it wherever you want to use it.
Trommsdorff
05-21-2006, 11:18 PM
wow, fast replies, many thanks.
Is it simply called with $randomnumber ? Or does it need some {} or [] ?
Adrian Schneider
05-21-2006, 11:19 PM
wow, fast replies, many thanks.
Is it simply called with $randomnumber ? Or does it need some {} or [] ?
We can't tell you without seeing your code.
Trommsdorff
05-21-2006, 11:22 PM
OK, the code I plan on using is from here:
http://www.zend.com/tips/tips.php?id=243&single=1
My rendition is below:
<?
// randomize the color
$r = rand(128,255);
$g = rand(128,255);
$b = rand(128,255);
$randomcolor = dechex($r) . dechex($g) . dechex($b);
?>
Adrian Schneider
05-21-2006, 11:25 PM
Okay, in your plugin, do NOT include the PHP tags (<? and ?>). Everything else looks okay - be sure to add the # in front of $randomcolor when you use it. And no, you won't need any []s or {}s.
Trommsdorff
05-21-2006, 11:32 PM
OK, thanks again for the quick reply. I'm trying to use it in the postbit_legacy to display in posts, but nothing is showing.
Should I call it differently for there?
Adrian Schneider
05-21-2006, 11:37 PM
OK, thanks again for the quick reply. I'm trying to use it in the postbit_legacy to display in posts, but nothing is showing.
Should I call it differently for there?
Posts are processed in a function, so it's not available there. If you want every post to have a different color, do this: hook: postbit_display_complete$r = rand(128,255);
$g = rand(128,255);
$b = rand(128,255);
$randomcolor = dechex($r) . dechex($g) . dechex($b); and then $randomcolor will be available (and unique) in all of your posts (you can remove the other plugin you created in global_start).
If you want them all the same:
postbit_display_completeglobal $randomcolor; (while leaving the original global_start hook intact).
Trommsdorff
05-21-2006, 11:41 PM
Fantastic! My deepest thanks. I'm actually using as part of an included JS script that needs an image border color and it works well now! Thanks!
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.