webguy262 Posted November 6, 2011 Share Posted November 6, 2011 Trying to get a form to display checked and unchecked values based on what is in the database. Hard coded form works fine. I implode the array and it goes into the database as a comma delimited string. But I need to let users edit the checkbox values, so I need to create the form by exploding the string, iterating through the array, and outputting the checkbox form elements. Here's what I have... <p>Specialization: <p> <?php //echo $session->userinfo['specialization']; THIS DISPLAYS THE STRING CORRECTLY $aSpecialization = array('Automotive', 'Aerospace', 'Biotech/Life Sciences', 'Chemicals', 'Damages Analysis', 'Electronics', 'Litigation', 'Manufacturing ', 'Materials', 'Medical Devices', 'Mobile Applications', 'Patent Prosecution', 'Physics', 'Renewable Energy', 'Semiconductors', 'Software', 'Telecommunications', 'Utilities'); //converting comma separated into array using explode function $dbspecialization= explode(',',$session->userinfo['specialization']); // echo $dbspecialization; THIS IS CONFIRMED AS AN array foreach ($aSpecialization as $specialization) { if(in_array($specialization,$dbspecialization)) { echo '<input name="specialization[]" type="checkbox" value="$specialization" CHECKED> $aSpecialization[] <br />'; } else { echo '<input name="specialization[]" type="checkbox" value="$specialization"> $aSpecialization[] <br />'; } } ?> But the output I get is... <input name="specialization[]" type="checkbox" value="$specialization" CHECKED> $aSpecialization[] <br /> <input name="specialization[]" type="checkbox" value="$specialization"> $aSpecialization[] <br /> <input name="specialization[]" type="checkbox" value="$specialization"> $aSpecialization[] <br /> etc... What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/250565-checkbox-values-from-database/ Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 Single quotes are not parsed, and are interpreted literally (variables aren't parsed) You need to concatenate the variables using the dot operator echo '<input name="specialization[]" type="checkbox" value="'.$specialization.'"> '.$specialization.' <br />'; Quote Link to comment https://forums.phpfreaks.com/topic/250565-checkbox-values-from-database/#findComment-1285592 Share on other sites More sharing options...
webguy262 Posted November 6, 2011 Author Share Posted November 6, 2011 Thanks xyph... I'm almost there! The issue now is that only the first of several checked values comes back checked. Here's the current code... <?php // $session->userinfo['specialization'] is a database field. // echo $session->userinfo['specialization']; THIS WORKS AND OUTPUTS THREE VALUES AS "Automotive, Aerospace, Biotech/Life Sciences" $aSpecialization = array('Automotive', 'Aerospace', 'Biotech/Life Sciences', 'Chemicals', 'Damages Analysis', 'Electronics', 'Litigation', 'Manufacturing ', 'Materials', 'Medical Devices', 'Mobile Applications', 'Patent Prosecution', 'Physics', 'Renewable Energy', 'Semiconductors', 'Software', 'Telecommunications', 'Utilities'); //converting comma separated into array using explode function $dbspecialization= explode(',',$session->userinfo['specialization']); // echo $dbspecialization; THIS IS CONFIRMED AR AN ARRAY... OOUTPUTS 'Array' foreach ($aSpecialization as $specialization) { if(in_array($specialization,$dbspecialization)) { echo '<input name="specialization[]" type="checkbox" value="'.$specialization.'" CHECKED> '.$specialization.' <br />'; } else { echo '<input name="specialization[]" type="checkbox" value="'.$specialization.'"> '.$specialization.' <br />'; } } ?> So I'm still missing something.. Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/250565-checkbox-values-from-database/#findComment-1285626 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 It's hard to say, it's possibly a typo. Try using print_r on both of your arrays to verify spelling and capitalization is identical. Quote Link to comment https://forums.phpfreaks.com/topic/250565-checkbox-values-from-database/#findComment-1285639 Share on other sites More sharing options...
webguy262 Posted November 6, 2011 Author Share Posted November 6, 2011 That suggestion helped me find the problem... Another form in the project displays the imploded array as a comma delimited list. I added a space after the comma in the implode statement so I could just grab the string and output it as is. So when I ran the in_array, I was comparing strings with the extra space, against strings without the space. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/250565-checkbox-values-from-database/#findComment-1285667 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.