Jump to content

Checkbox values from database


webguy262

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/250565-checkbox-values-from-database/
Share on other sites

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 />';

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?

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.