Jump to content

Coloring usernames in table based off IP Address?


Lukeidiot
Go to solution Solved by jcbones,

Recommended Posts

How would I go about coloring usernames (random color) based off an IP address?

 

For example, I want all user's with IP "127.0.0.1" to be red in the table:

 

 

$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
 
Example:
 
User1(red) - 127.0.0.1
User2(red) - 127.0.0.1
User3(blue) - 44.43.44.44
User4(green) - 54.45.45.45
Edited by Lukeidiot
Link to comment
Share on other sites

Keep an array of IP addresses and their random colors as you go through the list. If the address is in there already then use its color, otherwise pick a color, use that, and save it in the array.

 

Could you give me an example code for this?

 

Because the trouble is finding when the IP is different in the while loop.

Edited by Lukeidiot
Link to comment
Share on other sites

Well, you've got some kind of loop over users I assume? In pseudocode,

$ipaddrcolors = array();
for each $user {
if $ipaddrcolors[$user IP address] exists {
$color = $ipaddrcolors[$user IP address]
} else {
pick a random $color
$ipaddrcolors[$user IP address] = $color
}
color the user with $color
}
Edited by requinix
Link to comment
Share on other sites

You really need to rethink how you are going to generate "random" colors. You are going to generate colors that will not be readable on whatever background you choose. If you are using a white background maybe limit it to only 0-9 for darker colors.

Link to comment
Share on other sites

Well, you've got some kind of loop over users I assume? In pseudocode,

$ipaddrcolors = array();
for each $user {
    if $ipaddrcolors[$user IP address] exists {
        $color = $ipaddrcolors[$user IP address]
    } else {
        pick a random $color
        $ipaddrcolors[$user IP address] = $color
    }
    color the user with $color
}

 

 

$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];

$ipaddrcolors = array(); 

foreach($row_rs['ipaddress']){ 
if ($row_rs['ipaddress']){ 
$colored_username = "<font color='$color'>$row_rs[username]</font>";
 } else {
 $colored_username = "<font color='$color'>$row_rs[username]</font>";
 }
 $colored_username = "<font color='$color'>$row_rs[username]</font>";
 }
Edited by Lukeidiot
Link to comment
Share on other sites

Well, you've got some kind of loop over users I assume? In pseudocode,

$ipaddrcolors = array();
for each $user {
    if $ipaddrcolors[$user IP address] exists {
        $color = $ipaddrcolors[$user IP address]
    } else {
        pick a random $color
        $ipaddrcolors[$user IP address] = $color
    }
    color the user with $color
}

 

 

You can't use for each with a single $variable. How would I go about doing this? Also, how do I tell if the $ip has been set a color?
Link to comment
Share on other sites

I wrote pseudocode, not PHP code. You're supposed to read what I wrote as if it were a more technical and precise version of English, understand what it's doing, and translate that into actual PHP code.

I understand that completely, but my problem lies in finding which IP has already been added to the array.
Link to comment
Share on other sites

This is what I tried, but doesnt work.

$user_ip = $row_rs['ipaddress'];
$user = $row_rs['username'];

$ipaddrcolors = array();

foreach ($user as $user2) {
if(in_array($ipaddrcolors[$user_ip])){
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,9)].$rand[rand(0,9)].$rand[rand(0,9)].$rand[rand(0,9)].$rand[rand(0,9)].$rand[rand(0,9)];
$color = $ipaddrcolors[$user_ip];
} else {
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,9)].$rand[rand(0,9)].$rand[rand(0,9)].$rand[rand(0,9)].$rand[rand(0,9)].$rand[rand(0,9)];
$ipaddrcolors[$user_ip] = $color;
}
$user_colored = "<font color='$color'>$user2</font>";
}


<td><div align="center"><strong><?php echo substr($user_colored, 0, 30); ?></strong></div></td>
Edited by Lukeidiot
Link to comment
Share on other sites

Here you go:

colors.php - provided by *unkown* (can't remember)

 

 
<?php
$colors = array("*TITLE01*"       => "Reds",
      "Indian Red"        => "#CD5C5C",
      "Light Coral"        => "#F08080",
      "Salmon"        => "#FA8072",
      "Dark Salmon"        => "#E9967A",
      "Light Salmon"        => "#FFA07A",
      "Crimson"        => "#DC143C",
      "Red"           => "#FF0000",
      "Fire Brick"        => "#B22222",
      "Dark Red"        => "#8B0000",
      
      "*TITLE02*"       => "Pinks",
      "Pink"           => "#FFC0CB",
      "Light Pink"        => "#FFB6C1",
      "Hot Pink"        => "#FF69B4",
      "Deep Pink"        => "#FF1493",
      "Medium Violet Red"     => "#C71585",
      "Pale Violet Red"     => "#DB7093",
      
      "*TITLE03*"       => "Oranges",
      "Light Salmon"        => "#FFA07A",
      "Coral"        => "#FF7F50",
      "Tomato"        => "#FF6347",
      "Orange Red"        => "#FF4500",
      "Dark Orange"        => "#FF8C00",
      "Orange"        => "#FFA500",
      
      "*TITLE04*"       => "Yellows",
      "Gold"           => "#FFD700",
      "Yellow"        => "#FFFF00",
      "Light Yellow"        => "#FFFFE0",
      "Lemon Chiffon"     => "#FFFACD",
      "Light Goldenrod Yellow" => "#FAFAD2",
      "Papaya Whip"        => "#FFEFD5",
      "Moccasin"        => "#FFE4B5",
      "Peach Puff"        => "#FFDAB9",
      "Pale Goldenrod"     => "#EEE8AA",
      "Khaki"        => "#F0E68C",
      "Dark Khaki"        => "#BDB76B",
            
      "*TITLE05*"        => "Purples",
      "Lavender"        => "#E6E6FA",
      "Thistle"        => "#D8BFD8",
      "Plum"           => "#DDA0DD",
      "Violet"        => "#EE82EE",
      "Orchid"        => "#DA70D6",
      "Fuchsia"        => "#FF00FF",
      "Magenta"        => "#FF00FF",
      "Medium Orchid"     => "#BA55D3",
      "Medium Purple"     => "#9370DB",
      "Blue Violet"        => "#8A2BE2",
      "Dark Violet"        => "#9400D3",
      "Dark Orchid"        => "#9932CC",
      "Dark Magenta"        => "#8B008B",
      "Purple"        => "#800080",
      "Indigo"        => "#4B0082",
      "Slate Blue"        => "#6A5ACD",
      "Dark Slate Blue"     => "#483D8B",
      
      "*TITLE06*"       => "Greens",
      "Green Yellow"        => "#ADFF2F",
      "Chartreuse"        => "#7FFF00",
      "Lawn Green"        => "#7CFC00",
      "Lime"           => "#00FF00",
      "Lime Green"        => "#32CD32",
      "Pale Green"        => "#98FB98",
      "Light   Green"        => "#90EE90",
      "Medium Spring Green"     => "#00FA9A",
      "Spring Green"        => "#00FF7F",
      "Medium Sea Green"     => "#3CB371",
      "Sea Green"        => "#2E8B57",
      "Forest Green"        => "#228B22",
      "Green"        => "#008000",
      "Dark Green"        => "#006400",
      "Yellow Green"        => "#9ACD32",
      "Olive Drab"        => "#6B8E23",
      "Olive"        => "#808000",
      "Dark Olive Green"     => "#556B2F",
      "Medium Aquamarine"     => "#66CDAA",
      "Dark Sea Green"     => "#8FBC8F",
      "Light Sea Green"     => "#20B2AA",
      "Dark Cyan"        => "#008B8B",
      "Teal"           => "#008080",
      
      "*TITLE07*"       => "Blues",
      "Aqua"           => "#00FFFF",
      "Cyan"           => "#00FFFF",
      "Light Cyan"        => "#E0FFFF",
      "Pale Turquoise"     => "#AFEEEE",
      "Aquamarine"        => "#7FFFD4",
      "Turquoise"        => "#40E0D0",
      "Medium Turquoise"     => "#48D1CC",
      "Dark Turquoise"     => "#00CED1",
      "Cadet Blue"        => "#5F9EA0",
      "Steel Blue"        => "#4682B4",
      "Light Steel Blue"     => "#B0C4DE",
      "Powder Blue"        => "#B0E0E6",
      "Light Blue"        => "#ADD8E6",
      "Sky Blue"        => "#87CEEB",
      "Light Sky Blue"     => "#87CEFA",
      "Deep Sky Blue"     => "#00BFFF",
      "Dodger Blue"        => "#1E90FF",
      "Cornflower Blue"     => "#6495ED",
      "Medium Slate Blue"     => "#7B68EE",
      "Royal Blue"        => "#4169E1",
      "Blue"           => "#0000FF",
      "Medium Blue"        => "#0000CD",
      "Dark Blue"       => "#00008B",
      "Navy"           => "#000080",
      "Midnight Blue"     => "#191970",
            
      "*TITLE08*"       => "Browns",
      "Cornsilk"        => "#FFF8DC",
      "Blanched Almond"     => "#FFEBCD",
      "Bisque"        => "#FFE4C4",
      "Navajo White"        => "#FFDEAD",
      "Wheat"        => "#F5DEB3",
      "Burly Wood"        => "#DEB887",
      "Tan"           => "#D2B48C",
      "Rosy Brown"        => "#BC8F8F",
      "Sandy Brown"        => "#F4A460",
      "Goldenrod"        => "#DAA520",
      "Dark Goldenrod"     => "#B8860B",
      "Peru"           => "#CD853F",
      "Chocolate"        => "#D2691E",
      "Saddle Brown"        => "#8B4513",
      "Sienna"        => "#A0522D",
      "Brown"        => "#A52A2A",
      "Maroon"        => "#800000",
            
      "*TITLE09*"       => "Whites",
      "White"        => "#FFFFFF",
      "Snow"           => "#FFFAFA",
      "Honeydew"        => "#F0FFF0",
      "Mint Cream"        => "#F5FFFA",
      "Azure"        => "#F0FFFF",
      "Alice Blue"        => "#F0F8FF",
      "Ghost White"        => "#F8F8FF",
      "White Smoke"        => "#F5F5F5",
      "Seashell"       => "#FFF5EE",
      "Beige"          => "#F5F5DC",
      "Old Lace"       => "#FDF5E6",
      "Floral White"       => "#FFFAF0",
      "Ivory"          => "#FFFFF0",
      "Antique White"       => "#FAEBD7",
      "Linen"          => "#FAF0E6",
      "Lavender Blush"    => "#FFF0F5",
      "Misty Rose"       => "#FFE4E1",
      
      "*TITLE10*"       => "Greys",
      "Gainsboro"       => "#DCDCDC",
      "Light Grey"       => "#D3D3D3",
      "Silver"       => "#C0C0C0",
      "Dark Gray"       => "#A9A9A9",
      "Gray"          => "#808080",
      "Dim Gray"       => "#696969",
      "Light Slate Gray"    => "#778899",
      "Slate Gray"       => "#708090",
      "Dark Slate Gray"    => "#2F4F4F",
      "Black"          => "#000000"
      );
 
 
?>
Link to comment
Share on other sites

Second part:

 

Whatever.php
<?php
include('colors.php');

function randomColor($colors) {
  $color = array_rand($colors,1);
  return  (strpos($color,'*') === false) ? substr($colors[$color],1) : randomColor($colors);
}
 
$usedColors = array();
foreach($row['ipaddress'] as $address) {
  if(array_key_exists($address,$usedColors)) {
    $color = $usedColors[$address];
  } else {
    $color = randomColor($colors);
    $usedColors[$address] = $color;
  }
  echo '<span style="background-color:#' . $color . '">' . $address . '</span><br />';
}
Link to comment
Share on other sites

Second part:

 

Whatever.php

<?php
include('colors.php');

function randomColor($colors) {
  $color = array_rand($colors,1);
  return  (strpos($color,'*') === false) ? substr($colors[$color],1) : randomColor($colors);
}
 
$usedColors = array();
foreach($row['ipaddress'] as $address) {
  if(array_key_exists($address,$usedColors)) {
    $color = $usedColors[$address];
  } else {
    $color = randomColor($colors);
    $usedColors[$address] = $color;
  }
  echo '<span style="background-color:#' . $color . '">' . $address . '</span><br />';
}

 

 

	$user_ip = $row_rs['ipaddress'];
	$user = $row_rs['username'];
	
						function randomColor($colors) {
						  $color = array_rand($colors,1);
						  return  (strpos($color,'*') === false) ? substr($colors[$color],1) : randomColor($colors);
						}
						 
						$usedColors = array();
						foreach($user_ip as $address) {
						  if(array_key_exists($address,$usedColors)) {
							$color = $usedColors[$address];
						  } else {
							$color = randomColor($colors);
							$usedColors[$address] = $color;
						  }
						 $new = '<span style="background-color:#' . $color . '">' . $address . '</span><br />';
						}


 <td><div align="center"><strong><?php echo $new; ?></strong></div></td>	
I have included colors.php outside the while loop. However this code is not working :( Edited by Lukeidiot
Link to comment
Share on other sites

<?php
include("colors.php");
function randomColor($colors) {
$color = array_rand($colors,1);
return (strpos($color,'*') === false) ? substr($colors[$color],1) : randomColor($colors);
}

$sql = mysql_query("SELECT * FROM ph_referers WHERE owner = '$_SESSION[username]' ORDER BY id DESC");
while($row = mysql_fetch_assoc($sql)){

$ipaddress = $row['ipaddress'];

$usedColors = array();
foreach($ipaddress as $address) {
if(array_key_exists($address,$usedColors)) {
$color = $usedColors[$address];
} else {
$color = randomColor($colors);
$usedColors[$address] = $color;
}
$new = '<font color="#' . $color . '">' . $address . '</font><br />';
}

echo "$new - $row[username]:$color<br>";

}
$color is showing as blank. Any ideas? Edited by Lukeidiot
Link to comment
Share on other sites

No idea, is that some sort of kitchen appliance?

You will not be able to integrate this code into yours without learning some basics. If you actually want someone to write it for you, post in freelance. If you want to learn, do some basic research. We are not here to write it for you for free.
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.