Skip to content
Advertisement

PHP grouping of options in optgroup

I am working on a woocommerce site and I need to find a way to group select options in optgroups.

My option are stored as array like

JavaScript

No what I need is to make by php a select that would group the options in optgroup by using the string up to first space

By using the function

JavaScript

I can split the values as required.

I need to adapt the code that I am currently using for showing options:

JavaScript

So I can show the options grouped by optgroup like:

JavaScript

I would appreciate if someone could help me.

Advertisement

Answer

This formats to option group placing the values in the value attr within each option. <option value="1.9tdi (2003-2009)"> and wraps it all in a select tag.

JavaScript

In html echo out your $stmt variable.

JavaScript

https://3v4l.org/bb2nR

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement