Mega Code Archive

 
Categories / Php / MySQL Database
 

Color Selector Widget using PHP3 & MySQL

<HTML> <TITLE>Color Selector Widget</TITLE> <BODY> <? /* Script Name: color_drop.html Script Purpose: A widget for presenting color options in a drop down menu. Script Setup: This script draws color information (color name, color code) from a MySQL database and presents it to the user as a drop down menu of color terms. There are many situations in which you will want to allow people to control the color of some object by selecting its color. In this example, I want to allow the person to control the color of text. Here is the table schema that I am using. CREATE TABLE colors ( uid MEDIUMINT(8) NOT NULL AUTO_INCREMENT, color_name VARCHAR(50), color_code VARCHAR(10), PRIMARY KEY(uid) ); The program also maintains state information about the color previously selected. If this program is passed the variable text_color, it will present its value as the currently selected item in the drop down box. */ /* Declare some variables. */ $hostname = ""; $password = ""; $user = ""; $database = "colors_db"; $table = "colors"; /* Main Loop */ echo "The text will look link this <font color=\"$text_color\">Sample</font> <br>"; mysql_connect($hosthame,$user,$password); $result = mysql($database,"SELECT distinct color_name,color_code FROM $table ORDER BY color_name"); $num_cols = mysql_numrows($result); $counter = 0; echo "<FORM METHOD = \"POST\" ACTION=\"color_drop.html\">\n"; echo "<select name=\"text_color\">\n"; if ($text_color == "") { echo "<option selected>Select Color...\n" ; }; while($counter < $num_cols): $cn = @mysql_result($result,$counter,"color_name"); $cc = @mysql_result($result,$counter,"color_code"); if ($text_color == $cc) { echo "<option value=\"$cc\" selected>$cn\n" ; } else { echo "<option value=\"$cc\">$cn\n" ; }; $counter = $counter + 1; endwhile; echo "</select>\n"; echo "<INPUT TYPE=\"submit\" VALUE=\"View Color\">\n"; echo "</FORM>\n"; ?> </BODY> </HTML>