Wolphie Posted August 8, 2008 Share Posted August 8, 2008 Ok so, If have a group of checkboxes, and I know how to get the values from each of them in an array. However, I can't seem to find a solution to only return a default value if "none" of the checkboxes are checked, and only return the values that are checked. I've tried: foreach($form_values['completed'] as $complete) { if($complete != 0) { $completed = array(); $completed = array_push($completed, $complete); } } if (is_array($completed)) { $completed = implode(', ', $completed); } else { $completed = 'None'; } Quote Link to comment https://forums.phpfreaks.com/topic/118769-checkboxes-and-arrays/ Share on other sites More sharing options...
Andy-H Posted August 8, 2008 Share Posted August 8, 2008 $completed = array(); Shouldn't that be declared outside of the foreach statement? Quote Link to comment https://forums.phpfreaks.com/topic/118769-checkboxes-and-arrays/#findComment-611512 Share on other sites More sharing options...
Wolphie Posted August 8, 2008 Author Share Posted August 8, 2008 Yes it should, thanks for pointing that out for me. Although doing that, I now have another problem. How will I be able to determine whether I should use "None" or not? Since $completed will always return TRUE to being an array. Quote Link to comment https://forums.phpfreaks.com/topic/118769-checkboxes-and-arrays/#findComment-611515 Share on other sites More sharing options...
Andy-H Posted August 8, 2008 Share Posted August 8, 2008 Could you use if ( (count($completed) > 0) ) { $completed = implode(', ', $completed); } else { $completed = 'None'; } Quote Link to comment https://forums.phpfreaks.com/topic/118769-checkboxes-and-arrays/#findComment-611523 Share on other sites More sharing options...
Wolphie Posted August 8, 2008 Author Share Posted August 8, 2008 OK, now "None" is being set. However, it doesn't seem to get past if($completed != 0) { ... } So no values are being pushed into the array. Quote Link to comment https://forums.phpfreaks.com/topic/118769-checkboxes-and-arrays/#findComment-611528 Share on other sites More sharing options...
Andy-H Posted August 8, 2008 Share Posted August 8, 2008 Whats the new code you got so far? Quote Link to comment https://forums.phpfreaks.com/topic/118769-checkboxes-and-arrays/#findComment-611531 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.