...

View Full Version : Drop down box for text color



Peuplarchie
06-07-2007, 05:13 AM
Good day to you all,
I'm trying to find a code for a drop down box displaying a color.
It would show different colored bar, and their value would be the color code example: "#003366".

The valu would then be $_post

Can somebody help me ?
Thanks!

Fou-Lu
06-07-2007, 05:15 AM
Do you have a specified colour set you want to use, or every possible combination between 000000 and FFFFFF? Just cause that can get awefully long...

Peuplarchie
06-07-2007, 05:46 AM
I just want black, white, couple blue, beige.

Fou-Lu
06-07-2007, 06:02 AM
Oh, ok this isn't really a php issue then since its so few its easy to hard code.
Obviously you know the general code to use:


<select ...>
<option value="000000" id="opt000000">#000000</option>
<option value="F5F5DC" id="optF5F5DC">#F5F5DC</option>
<option value="FFFFFF" id="optFFFFFF">#FFFFFF</option>
<option value="0000FF" id="opt0000FF">#0000FF</option>
</select>

Not sure what blues and stuff, but the f5f5dc is your beige.
Anyway, change the colour using css like so:



option#opt000000 { background-color: #000000; color: #FFFFFF;}
option#opt0000FF { background-color: #0000FF; color: #FFFFFF;}
option#optF5F5DC { background-color: #F5F5DC; color: #000000;}
option#optFFFFFF { background-color: #FFFFFF; color: #000000;}


As you can see, this is rather repetative. But since this isn't a php concern, best to check out the javascript forum for a method that can iterate all of the select items and specify their background and forground colours. Its simple to do, but I don't trust my off the cuff javascript to suit your purposes :)
Good luck!


Sorry my bad, I completely missed your php question lol
$_POST will send a value back, in my above example it sends the colour code. Now, in php you must store the values in an array or whatever you want to use, and construct the colour options from there:


$colourArray = array("000000", "F5F5DC", "FFFFFF", "0000FF");
for ($i = 0; $i < count($colourArray); $i++)
{
$selected = ($colourArray[$i] == trim($_POST['COLOUR_CHOICE'])) ? ' selected="selected"' : '';
echo '<option value="' . $colourArray[$i] . '"' . $selected . '>#' . $colourArray[$i] . '</option>';
}


I didn't test this out, but seems to work out ok in my head, lol
Change COLOUR_CHOICE to whatever name is given to your select element.

Peuplarchie
06-09-2007, 01:17 AM
good, but I need a colored bar to be shown not the code or the name.
Thanks !

Len Whistler
06-09-2007, 02:29 AM
Peuplarchie....I believe that option value ID and the CSS that it corresponds to will set the background color for each option value. You could enter text into the option box the same color as the background, such as:


option#optF5F5DC { background-color: #F5F5DC; color:#F5F5DC;}

If there is no text in an option box then the option box is very small, text like qwerty will provide a good width.


<option value="F5F5DC" id="optF5F5DC">qwerty</option>

whizard
06-09-2007, 03:40 AM
or just several &nbsp;s

Or, to be nice to people, you could write the name of the color

Dan

Peuplarchie
06-09-2007, 08:02 PM
Yes, sorry I misread your answer lol ..
It's working perfectly!!!

Peuplarchie
06-09-2007, 08:45 PM
But I cannot in the select box have the name instead of the color code as the choice ?

Fou-Lu
06-10-2007, 02:06 AM
Yes.
Not sure what your using exactly, so back to my code :P


$colourArray = array("000000", "F5F5DC", "FFFFFF", "0000FF");
for ($i = 0; $i < count($colourArray); $i++)
{
$selected = ($colourArray[$i] == trim($_POST['COLOUR_CHOICE'])) ? ' selected="selected"' : '';
echo '<option value="' . $colourArray[$i] . '"' . $selected . '>#' . $colourArray[$i] . '</option>';
}

We'll change the colourArray to be multidimensional:


$colourArray = array("Black" => "000000", "Beige" => "F5F5DC", "White" => "FFFFFF", "Blue" => "0000FF");

Then edit the for loop like so:


$selected = ($colourArray[$i][1] == trim($_POST['COLOUR_CHOICE'])) ? 'selected="selected"' : '';
echo '<option value="' . $colourArray[$i][1] . '"' . $selected . '>' . $colourArray[$i][0] . '</option>';

There you go.

Peuplarchie
06-10-2007, 04:16 AM
it dont work

Peuplarchie
06-10-2007, 04:34 AM
Your first code was perfect put the last one don't work, I trying different thing but still...

Fou-Lu
06-10-2007, 04:23 PM
it dont work
Can you clarify what didn't work? I didn't test the code out, is it syntactical or is it logical?

Peuplarchie
06-10-2007, 06:09 PM
nothing appears in the selection box.

Fou-Lu
06-11-2007, 02:58 AM
Are you still running the loop to generate the options?


for ($i = 0; $i < count($colourArray); $i++)
{
$selected = ($colourArray[$i][1] == trim($_POST['COLOUR_CHOICE'])) ? 'selected="selected"' : '';
echo '<option value="' . $colourArray[$i][1] . '"' . $selected . '>' . $colourArray[$i][0] . '</option>';
}

Peuplarchie
06-12-2007, 01:24 AM
yes, i do the exact same way as you just stated..

Peuplarchie
06-12-2007, 01:28 AM
I probably just missed something



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum