Insert Selected data In MySQL

Thread Solved

Join Date: Feb 2008
Posts: 19
Reputation: m.cliter is an unknown quantity at this point 
Solved Threads: 0
m.cliter m.cliter is offline Offline
Newbie Poster

Insert Selected data In MySQL

 
0
  #1
Feb 11th, 2008
Hi,
I have a long list of options( 60 option) where user can selecte none or all of them.
the code is like that
  1. <selecte name="list">
  2. <option value="1">option</option>
  3. ...
  4. </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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Insert Selected data In MySQL

 
0
  #2
Feb 11th, 2008
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 ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 19
Reputation: m.cliter is an unknown quantity at this point 
Solved Threads: 0
m.cliter m.cliter is offline Offline
Newbie Poster

Re: Insert Selected data In MySQL

 
0
  #3
Feb 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: Insert Selected data In MySQL

 
1
  #4
Feb 12th, 2008
since most people will probably never pick more than a few, it would save space to only have one column.

try this:

  1. <?php
  2. //multi-list boxes post an array of results
  3. //get this array into a single comma-seperated-value string
  4. foreach($_POST['select'] as $key=>$val)
  5. {
  6. //add item as comma-seperated-value
  7. $csv .= $val .',';
  8. }
  9.  
  10. //trim trailing comma
  11. $csv = rtrim($csv,',');
  12.  
  13. //INSERT $csv INTO A SINGLE FIELD IN YOUR DATABASE ROW HERE!!!
  14. //you can do the sql HERE...
  15.  
  16. //end part one!
  17.  
  18. //later you can sort your comma-seperated-value that you retrieve from the database back into individual elements like so...
  19.  
  20. //you can do the sql HERE...
  21.  
  22. //store all individual items in an array
  23. $arrayOfItems = explode(',',$csv);
  24.  
  25. //build each of your items in a list
  26. foreach($arrayOfItems as $key=>$val)
  27. {
  28. $output .= '<li>' . $val . '</li>';
  29. }
  30.  
  31. //display output
  32. echo $output;
  33.  
  34.  
  35. ?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 19
Reputation: m.cliter is an unknown quantity at this point 
Solved Threads: 0
m.cliter m.cliter is offline Offline
Newbie Poster

Re: Insert Selected data In MySQL

 
0
  #5
Feb 12th, 2008
Thanks for you help, I really appreciated
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 19
Reputation: m.cliter is an unknown quantity at this point 
Solved Threads: 0
m.cliter m.cliter is offline Offline
Newbie Poster

Re: Insert Selected data In MySQL

 
0
  #6
Mar 2nd, 2008
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="checked"/> sentence 3<br />
<input type="checkbox" name="name4" /> sentence 4
any hint?
PS: I have too many options.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: Insert Selected data In MySQL

 
0
  #7
Mar 3rd, 2008
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...

  1. //array to hold checkbox options
  2. $checkBoxOptions = array('sentence 1' , 'sentence 2' , 'sentence 3' , 'sentence 4');
  3.  
  4. //create checkbox output
  5. $cbOut = '';
  6.  
  7. foreach($checkBoxOptions as $val)
  8. {
  9. //test to see if stored choices match a checkbox value.
  10. if(in_array($val , $arrayOfItems))
  11. {
  12. //stored choice was found, mark this box
  13. $cbOut .= '<input type="checkbox" name="'.$val.'" checked="checked" value="'.$val.'"/><label>' . $val . '</label><br/>';
  14. }
  15. else
  16. {
  17. //choice has not been stored, leave box unchecked
  18. $cbOut .= '<input type="checkbox" name="'.$val.'" value="'.$val.'"/><label>' . $val . '</label><br/>';
  19. }
  20. }
  21.  
  22. //output checkboxes
  23. echo $cbOut;
The End
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC