TeddyKiller Posted April 16, 2010 Share Posted April 16, 2010 I have a field with multiple check values. In the database.. if all 4 are checked, it displays similar to.. value1, value2, value3, value4 If two is checked.. its value1, value2 or whatever. You get the drift. We use an explode, to seperate it all. Now because the code isn't a mind reader.. the explode, will give up a maximum of 4 values in the array. The code is below. $proin = explode(', ', $profile->interested_in); echo ' <input type="checkbox" name="intin[]" value="men"'; if($proin[0] == 'men') { echo 'checked'; } echo '/> Men <input type="checkbox" name="intin[]" value="women"'; if($proin[1] == 'women') { echo 'checked'; } echo ' /> Women <input type="checkbox" name="intin[]" value="none"'; if($proin[2] == 'none') { echo 'checked'; } echo ' /> None <input type="checkbox" name="intin[]" value="other"'; if($proin[3] == 'other') { echo 'checked'; } echo ' /> Other Now, whats the problem? The problem is.. if a person only checked in "none" The checkbox wont display checked. As it only checks as if there are 4 values in the database. How can we solve this? Well.. i know if this way, here is one. echo '<input type="checkbox" name="intin[]" value="men"'; if($proin[0] == 'men' || $proin[1] == 'men' || $proin[2] == 'men' || $proin[3] == 'men') { echo 'checked'; } echo '/> Men'; This is alright, if there is maybe only 2 or 3 fields. However.. in my case there is 4, and 4 rows of that code, wil llook very messy. I also have another lot of checkboxes. There is 9 of them. This will end up very messy. I was wondering, is there an easier way to do this? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/ Share on other sites More sharing options...
deanlearner Posted April 16, 2010 Share Posted April 16, 2010 Im not entirely sure if im understanding but... would the following work? $fields = (0 => 'men', 1=> 'women', 2=> 'none', 3=> 'other'); $proin = explode(', ', $profile->interested_in); foreach($fields as $num => $name){ if(in_array($name,$proin)){$checked = 'checked';}else{$checked = '';} echo '<input type="checkbox" name="intin[]" value="'.$name.'" '.$checked.' /> '.ucfirst($name); } Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043022 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 Good lord. There was a few syntax errors. $fields should of been an array. It works though! I imagine you may not of be able to come up with something similar for this too could you? echo '<option value="" '; if($profile->orien_status == '') { echo 'selected'; } echo '>Select Orientation:</option> <option '; if($profile->orien_status == 'straight') { echo 'selected'; } echo '>Straight</option> <option '; if($profile->orien_status == 'bisexual') { echo 'selected'; } echo '>Bisexual</option> <option '; if($profile->orien_status == 'lesbian/gay') { echo 'selected'; } echo '>Lesbian/Gay</option> <option '; if($profile->orien_status == 'asexual') { echo 'selected'; } echo '>Asexual</option> <option '; if($profile->orien_status == 'pansexual') { echo 'selected'; } echo '>Pansexual</option> Only one is aloud. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043026 Share on other sites More sharing options...
deanlearner Posted April 16, 2010 Share Posted April 16, 2010 Woops yeah, sorry i quickly rushed it together! You can do what you're after using the same logic I used in the previous example, but change the field array to straight, bisexual, etc, etc and change the echo row as required as well Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043039 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 Because it only has 1 value in the database. It can't be an array.. So I came up with this.. which doesn't work. $fields = array(0 => 'straight', 1 => 'bisexual', 2 => 'lesbian/gay', 3 => 'asexual', 4 => 'pansexual'); foreach($fields as $num => $name){ if(in_array($name,$profile->orien_status)){$selected = 'selected';}else{$selected = '';} echo '<option value="'.$name.'" '.$selected.' /> '.ucfirst($name).' </option>'; } All the fields come up, but none of them are selected. I'm assuming it's because $profile->orien_status isn't an array. How can I fix it? Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043063 Share on other sites More sharing options...
deanlearner Posted April 16, 2010 Share Posted April 16, 2010 Almost $fields = array(0 => 'straight', 1 => 'bisexual', 2 => 'lesbian/gay', 3 => 'asexual', 4 => 'pansexual'); foreach($fields as $num => $name){ if(in_array($name,$profile->orien_status)){$selected = 'selected';}else{$selected = '';} echo '<option value="'.$name.'" '.$selected.' /> '.ucfirst($name).' </option>'; } should be $fields = array(0 => 'straight', 1 => 'bisexual', 2 => 'lesbian/gay', 3 => 'asexual', 4 => 'pansexual'); foreach($fields as $num => $name){ if($name==$profile->orien_status){$selected = 'selected';}else{$selected = '';} echo '<option value="'.$name.'" '.$selected.' /> '.ucfirst($name).' </option>'; } Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043066 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Probably a bit more wordy, but cleaner. function GenerateListOption ($value, $text, $IsSelected) { $selected = $IsSelected ? 'selected="selected"' : ''; return sprintf('<option value="%s" %s>%s</option>', $value, $selected, $text); } $oriens = array('' => 'Sexual Orientation', 'straight' => 'Straight', ... ); foreach ($oriens as $key => $value) { echo GenerateListOption($key, $value, $profile->orien_status === $key); } Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043068 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 Ahh. Yeah. I didn't think the foreach was a look. Though I did think of that, but because I didn't think it was a loop (Blonde moment) i thought it wouldn't of worked. Ken2k7, that also works. It's cleaner too. Both brilliant respones! Good thing with yours Ken2k7, you can use it in other places. Good job! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043080 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 I've tried doing the same for the checkboxes too. None of them come up as checked. function CheckboxMenuDisplay($value, $text, $IsChecked) { $checked = $IsChecked ? 'checked' : ''; return sprintf('<input type="checkbox" name="intin[]" value="%s" %s />%s', $value, $checked, $text); } $proin = explode(', ', $profile->interested_in); $intins = array('men' => 'Men', 'women' => 'Women', 'none' => 'None', 'other' => 'Other'); foreach ($intins as $key => $value) { echo CheckboxMenuDisplay($key, $value, $proin === $key); } Whats wrong Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043086 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 $proin is an array. $key is a string. They will never be equal. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043091 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 echo CheckboxMenuDisplay($key, $value, in_array($key, $proin); Would that work? if not.. is there any solution to this at all? At the moment I changed it back to the other method as given by deanlearner. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043118 Share on other sites More sharing options...
KevinM1 Posted April 16, 2010 Share Posted April 16, 2010 Logically, wouldn't it make more sense to have these options as a set of radio buttons? I mean, this is for some sort of social networking site, correct? Someone can be interested in men, women, both, or none. With a set of radio buttons, you wouldn't have to do as much work to manipulate the data as you wouldn't have those "The user clicked both 'none' and 'men'" cases. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043122 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 echo CheckboxMenuDisplay($key, $value, in_array($key, $proin); ^ you missed a closing ). Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043130 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author 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? Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043144 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 deanlearner, heres the one with 9 options. It uses <br/> and <td> 's etc. I can't figure it out in a method like you chose. echo '<tr><td width="137"> <input type="checkbox" name="ethnicity[]" value="asian" /> Asian <br /> <input type="checkbox" name="ethnicity[]" value="black/african" /> Black/African <br /> <input type="checkbox" name="ethnicity[]" value="causcasian/white" /> Caucasian/White </td> <td width="124"> <input type="checkbox" name="ethnicity[]" value="east indian" /> East Indian <br /> <input type="checkbox" name="ethnicity[]" value="hispanic/latino" /> Hispanic/Latino <br /> <input type="checkbox" name="ethnicity[]" value="middle eastern" /> Middle Eastern </td> <td width="128"> <input type="checkbox" name="ethnicity[]" value="native american" /> Native American <br /> <input type="checkbox" name="ethnicity[]" value="pacific islander" /> Pacific Islander <br /> <input type="checkbox" name="ethnicity[]" value="other" /> Other </td></tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043151 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 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 ($oriens 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/#findComment-1043164 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 Thanks for the reply. $oriens isn't defined in that example. Should it be $eths? foreach ($oriens 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($key, $value, $profile->orien_status === $key); echo '</td>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043165 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 I think you viewed it before I finished editing. I hit submit by mistake. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043167 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 foreach ($oriens as $key => $value) Still states $oriens.. ? Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043168 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Oh that part. Oops. Yep, should be $eths. Nice catch. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043169 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 GenerateListOption Should of been.. GenerateInputOption It also doesn't access the database in any way at all. There for nothing will be checked upon load. $profile->ethnicity holds the values of the ethnicities. These are similar to.. value1, value2, value3 So you'd do an implode to seperate them. Though.. I'm not sure how that'd go in? Also, after every checkbox, should be a <br /> except the last one. eg: <checkbox> <br /> <checkbox> <br /> <checkbox> It becomes unaligned otherwise. Although, instead of using <br/>.. would there be a way to do 2 extra tr's and manage to fit it in? Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043178 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 It's probably not the best set-up. 1. You shouldn't use CSV in the database. Makes searching very difficult. 2. Table is bad unless you're display some statistics. There are better ways to display something other than TD + breaks. Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043185 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 So if I'm not using CSV. How can I do it. Would I have like.. ethnicity1, ethnicity2.. I'm confused. Then when I display them.. it'll just implode it for the display. Yet they'll be like 9 collums for ethnicities. It's rather stupid. This is frustrating me. Can people have more than 1 ethnicity, in real life, If they can't.. can I have checkboxes acting like radiobuttons? Would this make the search easier as there is only 1 value in the database.. Really annoyed ¬,¬ Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043187 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Well, someone can be 50% Asian and 50% Hispanic. The decision is up to you. I will explain the CSV issue though. In your database, you can have this: Table ethnicity --------------------------- eth_id | ethnicity --------------------------- 1 | Asian 2 | Hispanic . . . Table user_eth ----------------------------------- id | user_id | ethnicity ----------------------------------- 1 | 4 | 1 2 | 4 | 2 . . . Quote Link to comment https://forums.phpfreaks.com/topic/198745-getting-multiple-checked-values-from-the-database-in-a-checkbox-help/#findComment-1043192 Share on other sites More sharing options...
TeddyKiller Posted April 16, 2010 Author Share Posted April 16, 2010 I understand, and table Ethnicity, holds all the ethnicities. To insert into user_eth, I could do a foreach. So foreach ethnicities checked.. insert a row into database based in this ethnicity. Lets say I did that, how would I then populate the checkboxes. Another site, uses similar methods. Although like I said before, using td's instead. There checkboxes are displayed like below. <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/#findComment-1043199 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.