KevinM1 Posted April 16, 2010 Share Posted April 16, 2010 I see what you mean Nightslyr, though radio buttons wouldn't quite do the trick would it.. I mean.. radio buttons run off 1 check only, the rest remain unchecked. No matter which one is checked. That only limits someone to be interested in 1 option... which limits me for example? Exactly right, which is why it's more logical to do it that way. Given the choices: Men - Women - Both - None If someone is interested in, say, women, then logically it's impossible for them to choose none as well. They can't have it both ways. Radio buttons enforce this behavior, and save you from having to check for those kinds of logically impossible combinations. It just looks like you're trying to over-design it, IMO. Checkboxes aren't a one-size-fits-all tool. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043212 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 I believe the option Transgender, and intersexed isn't there. I'm making the site to be friendly to all users. Not forcing them to simple pick 1 and 1 only. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043216 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 Ken2k7, in the code you gave me, how does it display it as collums? I was wondering if it was possible to migrate it to display as rows? <tr><td> checkbox one </td><td> checkbox two</td> so on. If so, i'll appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043220 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 What code? Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043229 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 This one echo '<tr>'; function GenerateInputOption ($name, $value, $text, $IsSelected) { $selected = $IsSelected ? 'checked="checked"' : ''; return sprintf('<input type="checkbox" name="%s" value="%s" %s />%s', $name, $value, $selected, $text); } $eths = array( 'Column1' => array('width' => 137, 'asian' => 'Asian', ...), 'Column2' => array('width' => 124, 'east indian' => 'East Indian', ...) ); foreach ($eths as $key => $value) { if (is_array($value)) { echo sprintf('<td width="%d">', $value['width']); foreach ($value as $eth => $option) if (strcmp($eth, "width") != 0) echo GenerateListOption('ethnicity[]', $eth, $option, false); echo '</td>'; } } echo '</tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043237 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Replace echo sprintf('<td width="%d">', $value['width']); foreach ($value as $eth => $option) if (strcmp($eth, "width") != 0) echo GenerateListOption('ethnicity[]', $eth, $option, false); echo '</td>'; With foreach ($value as $eth => $option) { echo sprintf('<td width="%d">', $value['width']); if (strcmp($eth, "width") != 0) GenerateListOption('ethnicity[]', $eth, $option, false); echo '</td>'; } Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043244 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 It doesn't display anything.. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043264 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 I think I got lost in what you wanted to do. Could you please clarify? Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043268 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 I'd like it displayed like it is below. So it's like.. <tr><td> blah </td><td> blah </td><td> blah </td></tr> <table id="ethnicity"> <tr> <td> <input type="checkbox" name="asian" rel="ethnicity"/><label>Asian</label> </td><td> <input type="checkbox" name="east indian" rel="ethnicity"/><label>East Indian</label> </td><td> <input type="checkbox" name="native american" rel="ethnicity"/><label>Native American</label> </td> </tr> <tr> <td> <input type="checkbox" name="black/african" rel="ethnicity"/><label>Black/African</label> </td><td> <input type="checkbox" name="hispanic/latino" rel="ethnicity"/><label>Hispanic/Latino</label> </td><td> <input type="checkbox" name="pacific islander" rel="ethnicity"/><label>Pacific Islander</label> </td> </tr> <tr> <td> <input type="checkbox" name="caucasian/white" rel="ethnicity"/><label>Caucasian/White</label> </td><td> <input type="checkbox" name="middle eastern" rel="ethnicity"/><label>Middle Eastern</label> </td><td> <input type="checkbox" name="other" rel="ethnicity"/><label>Other</label> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043272 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 <?php function GenerateColumnCheckboxTable ($num_cols, $values, $table_id, $input_attrs) { $contents = sprintf('<table id="%s"><tr>', $table_id); $count = 1; $input_params = ''; foreach ($input_attrs as $key => $val) $input_params .= sprintf('%s=%s', $key, $val); foreach ($values as $key => $value) { if ($count == $num_cols) { $contents .= '</tr><tr>'; $count = 1; } $contents .= sprintf('<td><input type="checkbox" name="%s" %s /><label>%s</label></td>', $key, $input_params, $value); $count++; } for (; $count <= $num_cols; $count++) $contents .= '<td> </td>'; $contents .= '</tr></table>'; return $contents; } $ethnics = array( 'asian' => 'Asian', 'east indian' => 'East Indian', 'native american' => 'Native American', 'black/african' => 'Black/African', . . . ); echo GenerateColumnCheckboxTable (3, $ethnics, 'ethnicity', array('rel' => 'ethnicity')); Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043285 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 Thanks I'll give it a go. Another thing I thought of.. You know the echo's, when it displays the checkboxes. Couldn't we put that into an array. eg: $array[] = 'checkbox here'; Then I'd put out my table.. and put $echo array[0] where I want the to be displayed. I know still it's not the best design.. although it'd work out properly wouldn't it? (Even with access to the database. For example.. accessing it via deanlearners method) Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043288 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 That code is set for 2 collums, is it possible for 3? cheers. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043290 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Yeah, sorry. Replace $count = 1; in the function to $count = 0; Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043295 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 That works thanks and also.. my method of putting it in the array works too! It's incredibly very messy. However.. the job works! $fields = array(0 => 'asian', 1 => 'black/african', 2 => 'caucasian/white', 3 => 'east indian', 4 => 'hispanic/latino', 5 => 'middle eastern', 6 => 'native american', 7 => 'pacific islander', 8 => 'other'); $proin = explode(', ', $profile->ethnicity); $array = array(); foreach($fields as $num => $name){ if(in_array($name,$proin)){$checked = 'checked';}else{$checked = '';} $array[] = '<input type="checkbox" name="ethnicity[]" value="'.$name.'" '.$checked.' /> '.ucwords($name); } echo '<tr><td height="34">Ethnicity:</td><td> <table> <tr> <td width="137">' . $array[0] . '<br />' . $array[1] . '<br />' . $array[2] . '</td> <td width="124">' . $array[3] . '<br />' . $array[4] . '<br />' . $array[5] . '</td> <td width="128">' . $array[6] . ' <br />' . $array[7] . '<br />' . $array[8] . '</td> </tr> </table> </td></tr> I am jumping with joy. The simplest solution huh. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043305 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Yup, until you start adding more entries in that array. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043306 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 Yup, until you start adding more entries in that array. See your point. Though I don't think I'll be having 100 ethnicities. That'll be too much. I'm sweet with what I have. Thanks everyone Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/page/2/#findComment-1043321 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.