943,918 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3647
  • PHP RSS
Feb 11th, 2008
0

Insert Selected data In MySQL

Expand Post »
Hi,
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)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m.cliter is offline Offline
19 posts
since Feb 2008
Feb 11th, 2008
0

Re: Insert Selected data In MySQL

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 ?
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 11th, 2008
0

Re: Insert Selected data In MySQL

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m.cliter is offline Offline
19 posts
since Feb 2008
Feb 12th, 2008
1

Re: Insert Selected data In MySQL

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

try this:

php Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007
Feb 12th, 2008
0

Re: Insert Selected data In MySQL

Thanks for you help, I really appreciated
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m.cliter is offline Offline
19 posts
since Feb 2008
Mar 2nd, 2008
0

Re: Insert Selected data In MySQL

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m.cliter is offline Offline
19 posts
since Feb 2008
Mar 3rd, 2008
0

Re: Insert Selected data In MySQL

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...

php Syntax (Toggle Plain Text)
  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;
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: SSI include call
Next Thread in PHP Forum Timeline: is it possible to disable a button through php script?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC