PDA

View Full Version : [MySQL] data type



cokelatdesign
February 20th, 2007, 07:18 PM
hi guys,

I got a question what is the data type in the database for a data that use input type "radio" and for the data that use option selection?

kdd
February 20th, 2007, 09:41 PM
For radio, you can send the ID or value or whatever to mysql.
For option, if the user is going to end up selecting multiple things, you can create different row for each. That's what I'd do. There are other ways to do this, though. It totally depends on how "you" want to implement your application...

skrolikowski
February 20th, 2007, 09:51 PM
I'd use enum.

cokelatdesign
February 20th, 2007, 09:55 PM
"For radio, you can send the ID or value or whatever to mysql"
but what data type should i use for that field though? is it int, or bool?
"For option, if the user is going..."
the option that I meant is the kind of drop down menu like when you registering to an e-commerce site and when you got to country and instead typing you choose form the drop down list

cokelatdesign
February 20th, 2007, 09:57 PM
for which one skrolikowski? for the radio or the "drop down list"?

bwh2
February 20th, 2007, 10:02 PM
I'd use enum.barf.

basically you would do a setup like this:


tbl_options
----------------
option_id (PK)
option_name

tbl_question
----------------
question_id (PK)
question_type_id (FK)
question_name

tbl_question_options
--------------------
question_id (FK)
option_id (FK)

tbl_question_types
-------------------
question_type_id (PK)
question_type_name

tbl_options would hold all of your possible answers. so TRUE, FALSE, USA, Canada, whatever.

tbl_question is your questions

tbl_questions options relates which options are available for which question

tbl_question_types specifies what types of questions there are (i.e., which type of form fields). so radio, checkbox, select, text, etc.

skrolikowski
February 20th, 2007, 10:50 PM
for which one skrolikowski? for the radio or the "drop down list"?
I would use enum for the radio input type:
enum('Yes','No') or whatever options you have...

And for the Drop-Down box, here's a cool trick for using enums...

//Create Drop Down List with ENUM
$sql="SHOW COLUMNS FROM table_name LIKE 'enum_column_name'";
$result = mysql_query ($sql);
$row = mysql_fetch_array ($result);

$select_arr=explode("','",preg_replace("/(enum)\('(.+?)'\)/","\\2",$row['Type']));

$select_sel="";
for($i = 1; $i < (count($select_arr) + 1); $i++) {
$select_sel .= "<option value=\"".$i."\">".$select_arr[$i - 1]."</option>";
}With this code, you can populate your enum with as many options as you like and the drop-down list will dynamically updated itself. Although it appears that some people aren't partial to enums :cantlook: Just an option.