plodos Posted May 1, 2009 Share Posted May 1, 2009 <?php function random_color(){ mt_srand((double)microtime()*1000000); $c = ''; while(strlen($c)<6){ $c .= sprintf("%02X", mt_rand(0, 255)); } return $c; } $color = random_color(); $data = mysql_query("select * from data where country='greece' group by name "); //ORDER BY id ASC while($info=mysql_fetch_array($data)) { Print " <p style=\"TEXT-ALIGN: justify\"> "; Print " <font color=\"".$color."\" face=\"Verdana\"><br> "; Print " </font><font face=\"Verdana\" style=\"font-size: 7pt\"> "; Print " {$info['name']}<br /> "; Print " <br> "; Print " </font> "; Print " <font face=\"Verdana\" style=\"font-size: 7pt;\" color=\"".$color."\"></font> "; Print " </p> "; } ?> I want to sort data with color group, each name group will have same color like VGA VGA VGA VGA Cafe Cafe Cafe Cafe Purple Purple but my script is now working and if its possible how can I sort datas ORDER BY id ASC with using group by ??? Quote Link to comment https://forums.phpfreaks.com/topic/156407-solved-how-to-group-by-name-with-color-group/ Share on other sites More sharing options...
premiso Posted May 1, 2009 Share Posted May 1, 2009 <?php function random_color(){ mt_srand((double)microtime()*1000000); $c = ''; while(strlen($c)<6){ $c .= sprintf("%02X", mt_rand(0, 255)); } return $c; } $data = mysql_query("select * from data where country='greece' group by name ORDER BY id ASC"); //ORDER BY id ASC // define our variables $colors = array(); $last_name = ""; $output = ""; while($info=mysql_fetch_assoc($data)) { if ($last_name != $info['name']) { do { $color = random_color(); } while (in_array($color, $colors)); $colors[] = $color; } $output .= "<p style=\"TEXT-ALIGN: justify\"> \n" . " <font color=\"#".$color."\" face=\"Verdana\" style=\"font-size: 7pt\"><br> \n" . " " . $info['name'] . "<br /><br /> \n" . " </font> \n" . " <font face=\"Verdana\" style=\"font-size: 7pt;\" color=\"#".$color."\"></font> \n" . " </p> \n\n"; $last_name = $info['name']; } echo $output; ?> I must have been in a nice mood (or really bored). I re-formatted your html output, simply because you were taking the time to indent it but not properly breaking it (the \n) so now when you view the source it should look "pretty" and easier to read. Also removed some non-essential excess lines. Notice the do while, it stores the last color used in an array, this is to ensure that the colors are unique by looping until it gets a unique color. The color randomizer will only run if a new name exists. Added the ORDER BY id ASC clause to the SQL query. Tested with an Array I created and seemed to work just fine. I also changed the 7 or 8 prints to be appended to an output. Then you just echo or print that output after the while loop. More efficient. I also properly indented your script for easier readability. Also had to move the $color generator into the while loop, how else do you think it would be re-generated to be random? Quote Link to comment https://forums.phpfreaks.com/topic/156407-solved-how-to-group-by-name-with-color-group/#findComment-823503 Share on other sites More sharing options...
plodos Posted May 1, 2009 Author Share Posted May 1, 2009 Thank you premiso. Code is working fine but also you mention about re-generated to be random, I have more than 200 different records and some of the colors are same:) Is there a way to solve this problem Quote Link to comment https://forums.phpfreaks.com/topic/156407-solved-how-to-group-by-name-with-color-group/#findComment-823567 Share on other sites More sharing options...
premiso Posted May 1, 2009 Share Posted May 1, 2009 I have more than 200 different records and some of the colors are same:) If the script ran, there is no possible way the colors are the same. They may look the same, but I guarantee the hexadecimal is completely different. As far as how to solve the problem of similar colors, I have no clue. I never dug into the color spectrum that deep to know how to determine if a hexadecimal is similar to one already used previously. I am sure it can be done, but frankly that does not interest me at all. Quote Link to comment https://forums.phpfreaks.com/topic/156407-solved-how-to-group-by-name-with-color-group/#findComment-823572 Share on other sites More sharing options...
plodos Posted May 1, 2009 Author Share Posted May 1, 2009 Thanks for everything. Quote Link to comment https://forums.phpfreaks.com/topic/156407-solved-how-to-group-by-name-with-color-group/#findComment-823683 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.