Concatenate selects options and checkboxes to one value separated by ","

Hello, I’m trying to concatenate I think they call it and it seems I’m not doing it correctly.

I have several selects and you choose options, I would like to make them go as one value to databa base separated by “,” at the same time you can add using javascript more rows that have the same selects and checkboxes so you can add more users to the form at the same time but this ones go in another row.

Something like this:

<td><input class="form-control" type="email" name="useremail[]" required/></td>'+
<td>
  <select class="form-select" name="section[]" required />
      <option value="Option 3">Option 3</option>
      <option value="Option 4">Option 4</option>
    </optgroup>
  </select>
</td>
<td>
  <select class="form-select" name="section[]" />
      <option value="">N/N</option>
      <option value="Option 5">Option 5</option>
      <option value="Option 6">Option 6</option>
    </optgroup>
  </select>
</td>

So you submit form:

if(isset($_POST['submit']))
{
    
$useremail = $_POST['useremail'];
$sections = $_POST['section'];
$section="";  
    foreach($sections as $sectoring)  
    {  
        $section .= $sectoring.",";
    }

    for($i=0;$i<count($useremail) && ($role) ;$i++)// Loop for each added user
    {
        $sql_add = 'INSERT INTO datadatabase (useremail,section) VALUES (?,?)';
        $stmt_add = $pdo->prepare($sql_add);
        $stmt_add->execute(array($useremail[$i],$section[$i]));
        echo "Submitted";
        } else {   
        echo "Failed";
    }          
}

It submites says success, etc. Then I go to the database but I only see that one option has been recorded.

I want to go to database and see Option 2, Option 10, Option 12, whatever. All as one value.

What I’m I doing wrong. Thanks

I’m going to start off by telling you that the database guys are going to wander into this thread and tell you what you’re doing wrong is putting all the option choices together in a comma separated string in a single field.

Secondly, this isnt what you want. I can already tell you this isnt what you want. But we’ll go ahead with it for now, and you can come back and ask for what you actually want later :wink:

Right. Now that that’s out of the way, how do you get what you asked for.

This can be simplified; implode can do all this in a single command.

$section isnt an array anymore, it’s a string. you should just have $section here, not $section[$i].

2 Likes

As pointed out in your previous thread, to get the select/option field data to match up with the corresponding email field data, you must use a two dimensional array name for the select/option field and the 1st array index must be set, starting at zero, to match the implied index value for the email field.

For the posted markup, this would look like -

<td><input class="form-control" type="email" name="useremail[]" required/></td>
<td>
  <select class="form-select" name="section[0][]" required />
      <option value="Option 3">Option 3</option>
      <option value="Option 4">Option 4</option>
    </optgroup>
  </select>
</td>
<td>
  <select class="form-select" name="section[0][]" />
      <option value="">N/N</option>
      <option value="Option 5">Option 5</option>
      <option value="Option 6">Option 6</option>
    </optgroup>
  </select>
</td>

For the next dynamically added section, the index value would be 1, …

Note: for the required attribute to do anything useful for a select/option menu, the first choice must have an empty value - value="" attribute.

If you then look at the submitted post data using - echo '<pre>'; print_r($_POST); echo '</pre>'; you can see what it is.

You would access the corresponding data for the fields in each set using -

foreach(array_keys($_POST['useremail']) as $key)
{
	echo $_POST['useremail'][$key];
	print_r($_POST['section'][$key]);
}

And lastly, you would not store the section values as a delimited list in a single column. This makes code and queries for every operation on that data more complicated. You would store the section data, one row per data item in a ‘section’ table, related back to the parent table through the parent’s id (autoincrement primary index.)

1 Like

I would suggest you take a look at JSON. It is really easy to understand and it will be the perfect solution for your problem…

1 Like

Thanks a lot of your answers and time, at the end I’ve decided to just add one more column in my database, I already made that decision before making the question since I can still print the output in the html like I want to.
But I was trying and not getting results and I really just wanted to know how to do it if I weren’t adding a new column, I won’t use the answers in this particular form, but ill use them and work with them to learn more thats for sure.

Hehe. That was the outcome I expected tbh.

I would actually have modified @mabismad’s code above personally, and put the section indicies to be 0 and 1 respectively (or given them two different names), so that you can index email, the first selection, and the second selection all by the same value.

1 Like