Hi,
I have a long list of options( 60 option) where user can selecte none or all of them.
the code is like that

<selecte name="list">
<option value="1">option</option>
...
</selecte>

and I have to display it again as a list ( <li></li> ), my question is what the best format to store the selected options in DATABASE?

Recommended Answers

All 6 Replies

What do you mean by best format to store the selected option ? If the <option> value is an integer, have an integer field. If its an alphanumeric, have a varchar field. Is this what you are talking about ?

thank you for your reply.
the otpions are written in english alphabet. for exemple, if the user selecte 20 option how can I insert all this options in database. do I need one column or do I need 60 columns?!!
my question is about database design, not about columns type.

since most people will probably never pick more than a few, it would save space to only have one column.

try this:

<?php
//multi-list boxes post an array of results
//get this array into a single comma-seperated-value string
foreach($_POST['select'] as $key=>$val)
{
	//add item as comma-seperated-value
	$csv .= $val .',';
}

//trim trailing comma
$csv = rtrim($csv,',');

//INSERT $csv INTO A SINGLE FIELD IN YOUR DATABASE ROW HERE!!!
//you can do the sql HERE...

//end part one!

//later you can sort your comma-seperated-value that you retrieve from the database back into individual elements like so...

//you can do the sql HERE...

//store all individual items in an array
$arrayOfItems = explode(',',$csv);

//build each of your items in a list
foreach($arrayOfItems as $key=>$val)
{
	$output .= '<li>' . $val . '</li>';	
}

//display output
echo $output;


?>
commented: Right. +3

Thanks for you help, I really appreciated

hi
What if I changed the select tag by input tag with checkbox as a type?
I know I can get each one using $_POST[] and put the comma ',' between sentances. But the problem is when the user want to edit his choices, I have to mark his previous choices.

<input type="checkbox" name="name1" /> sentence 1<br />
<input type="checkbox" name="name2" /> sentence 2<br />
<input type="checkbox" name="name3" checked="[B]checked[/B]"/> sentence 3<br />
<input type="checkbox" name="name4" /> sentence 4

any hint?
PS: I have too many options.

Working off the code sample in my earlier post...
Here would be one way to ensure the checkboxes were checked if they had chosen them previously...

//array to hold checkbox options
$checkBoxOptions = array('sentence 1' , 'sentence 2' , 'sentence 3' , 'sentence 4');
 
//create checkbox output
$cbOut = '';

foreach($checkBoxOptions as $val)
{
	//test to see if stored choices match a checkbox value.
	if(in_array($val , $arrayOfItems))
	{
		//stored choice was found, mark this box
		$cbOut .= '<input type="checkbox" name="'.$val.'" checked="checked" value="'.$val.'"/><label>' . $val . '</label><br/>';		
	}
	else
	{
		//choice has not been stored, leave box unchecked
		$cbOut .= '<input type="checkbox" name="'.$val.'" value="'.$val.'"/><label>' . $val . '</label><br/>';
	}
}

//output checkboxes
echo $cbOut;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.