4

I currently edit an open source program and have a question: the php code is actually like this:

print "<select multiple name=\"appliesto[]\" size=\"5\">";
foreach ($products as $k => $v) {
    $pid = $k;
    $group = $v['group'];
    $prodname = $v['name'];
    print "<option value=\"$pid\">$group - $prodname</option>";
}
print "</select>

Is it possible to get 2 values with each selected option? Because I need to save $pid and $prodname in 2 different columns on a mysql table.

Thanks!

2
  • 1
    That is not possible. Can't you just look up the product name in the database since you have the product id? Commented Apr 20, 2016 at 21:22
  • 1
    I was just about to say what @joserojas said, however you could also have a json string as the value, then on the other end you just decode it and you then already have an array. Commented Apr 20, 2016 at 21:39

1 Answer 1

3

You can put at the value attribute some delimiter to the values something like this:

print "<select multiple name=\"appliesto[]\" size=\"5\">";
foreach ($products as $k => $v) {
    $pid = $k;
    $group = $v['group'];
    $prodname = $v['name'];
    print "<option value=\"$pid|$prodname\">$group - $prodname</option>";
}
print "</select>

when you post the data you can explode the data by the delimiter in this case | in order to get the 2 values.

list($pid,$prodname) = explode("|", $inputSelect, 2);
Sign up to request clarification or add additional context in comments.

2 Comments

If you use this solution, make sure there is no | character in the product name.
Also, might I suggest list($pid,$prodname) = explode("|", $inputSelect, 2);? Just to make it shorter and the extra limit parameter in the explode function call ensures that only two elements are returned (in case there is a pipe character in the product name).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.