Jump to content

[SOLVED] How to group by name with color group ?


plodos

Recommended Posts

<?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  ???

 

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

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.