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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.