homchz Posted May 20, 2006 Share Posted May 20, 2006 I would like to have a list of check boxes and have each value entered into a db table feild seperated by a comma for separation later. I have tried having a group of check boxes with the same name posted into the db but it only grabs the last one. <input name='category' type='checkbox' dir='ltr' value='9,' /.><input name='category' type='checkbox' dir='ltr' value='10,' /><input name='category' type='checkbox' dir='ltr' value='11,' /><input name='category' type='checkbox' dir='ltr' value='12,' /><input name='category' type='checkbox' dir='ltr' value='13,' />I want to $_POST['category'] and if all are checked I would get9,10,11,12,13, Is this even possible? Thanks, Josh Quote Link to comment https://forums.phpfreaks.com/topic/10082-adding-more-than-one-value-to-a-db-table-field/ Share on other sites More sharing options...
zq29 Posted May 20, 2006 Share Posted May 20, 2006 Try this: [code]<input name='category[]' type='checkbox' dir='ltr' value='9,' /.><input name='category[]' type='checkbox' dir='ltr' value='10,' /><input name='category[]' type='checkbox' dir='ltr' value='11,' /><input name='category[]' type='checkbox' dir='ltr' value='12,' /><input name='category[]' type='checkbox' dir='ltr' value='13,' />[/code]You can then try this on your processing page to see how its structured:[code]<?phpecho "<pre>";print_r($_POST['category']);echo "</pre>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10082-adding-more-than-one-value-to-a-db-table-field/#findComment-37495 Share on other sites More sharing options...
homchz Posted May 20, 2006 Author Share Posted May 20, 2006 That works great for getting the values together, thanks. However it inserts "Array" into the db table and not the numbers.could I then insert the numbers as$categery[$i] after a count and a for stamement or am I over thinking this?? Quote Link to comment https://forums.phpfreaks.com/topic/10082-adding-more-than-one-value-to-a-db-table-field/#findComment-37499 Share on other sites More sharing options...
zq29 Posted May 20, 2006 Share Posted May 20, 2006 Yeah, that should be fine, you could probably even do it with a foreach() for less typing...[code]<?php$str = "";foreach($_POST['category'] as $x) { $str = $str.$x.",";}$str = substr($str,0,-1);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10082-adding-more-than-one-value-to-a-db-table-field/#findComment-37505 Share on other sites More sharing options...
homchz Posted May 20, 2006 Author Share Posted May 20, 2006 Thanks that works great, And thanks for the -1 in the substr(), that would have been my next question.Josh Quote Link to comment https://forums.phpfreaks.com/topic/10082-adding-more-than-one-value-to-a-db-table-field/#findComment-37509 Share on other sites More sharing options...
Barand Posted May 21, 2006 Share Posted May 21, 2006 [code]$db_cats = join (',' , $_POST['category'];[/code]A better table design would be to to use a separate table to hold categories. so if item #1 is in categories 1,5,7 and item #2 is in cats 5,6 you'd have[code]itemid | catid 1 | 1 1 | 5 1 | 7 2 | 5 2 | 6[/code] Now it's easy to query which cats an item belongs to and also which items are in a particular category Quote Link to comment https://forums.phpfreaks.com/topic/10082-adding-more-than-one-value-to-a-db-table-field/#findComment-37614 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.