| | |
Insert Selected data In MySQL
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 19
Reputation:
Solved Threads: 0
Hi,
I have a long list of options( 60 option) where user can selecte none or all of them.
the code is like that
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?
I have a long list of options( 60 option) where user can selecte none or all of them.
the code is like that
PHP Syntax (Toggle Plain Text)
<selecte name="list"> <option value="1">option</option> ... </selecte>
since most people will probably never pick more than a few, it would save space to only have one column.
try this:
try this:
php Syntax (Toggle Plain Text)
<?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; ?>
•
•
Join Date: Feb 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
any hint?
PS: I have too many options.
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="checked"/> sentence 3<br />
<input type="checkbox" name="name4" /> sentence 4PS: 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...
Here would be one way to ensure the checkboxes were checked if they had chosen them previously...
php Syntax (Toggle Plain Text)
//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;
The End
![]() |
Similar Threads
- php script for dropdown to fetch DB list and then Tables (PHP)
- Cannot get MySQL to talk to .php file (MySQL)
- mysql_num_rows(): supplied argument is not a valid MySQL result resource (PHP)
- Storing dynamic form values in Arrays for display & insert (PHP)
- inserting a size (PHP)
- php mysql image again PLEASE HELP (PHP)
- php script (PHP)
- script problem adding to database (PHP)
Other Threads in the PHP Forum
- Previous Thread: SSI include call
- Next Thread: is it possible to disable a button through php script?
| Thread Tools | Search this Thread |
# .htaccess 5.2.10 ajax apache api array beginner binary broken cakephp checkbox class clients cms code cron curl database date directory display dissertation download dynamic echo email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link login loop mail memberships menu mlm mod_rewrite multiple mysql mysqlquery oop open paypal pdf persist php problem query radio random recursion regex remote rss script search server sessions sms soap sockets source space sql syntax system table tutorial update upload url validation validator variable video web xml youtube






