0

I have the following array and PHP code - this adds an option to a drop down list and works fine but this is one drop down list.

However, what I would like to achieve is for a separate drop down list to be created for each of the unique values in the array [0] in the array.

So based on the array below there would be three drop down lists, one for ACME, one for EDNON and one for VALUE and each list would be populated with information already included in the PHP code below.

Array

Array (
[4f5hfgb] => Array (
[0] => ACME
[1] => 4f5hfgb
[2] => Aberdeen
)
[sdf4ws] => Array (
[0] => ACME
[1] => sdf4ws
[2] => Birmingham 
)
[dfgdfg54] => Array (
[0] => EDNON
[1] => dfgdfg54
[2] => Birmingham 
)
[345bfg] => Array (
[0] => EDNON
[1] => 345bfg
[2] => Birmingham 
)
[345fgfd] => Array (
[0] => VALVE
[1] => 345fgfd
[2] => Birmingham 
)
)

PHP

echo "
    <span class='Question SelectBox'>
    <span class='qnum noshow'></span>
    <span class='qtext'>Option</span>
    <span class='shown'>
     <select class='combobox' id='option' name='option' data-placeholder=\"Option...\" '>
       <option value=\"\">Option...</option>";

    foreach ($tmp as $value) {
     echo "<option name='".$value[0]."' value='".$value[1]."'>".$value[2]."</option>";
    }
     echo"</select>
     </span>
     </span>
     <span class='clearfix'></span>";

Attempted code

I have tried modifying the code but this only produces one drop down and the options are duplicated:

foreach ($tmp as $value) {
    if($value[0]=='ACME'){
    echo "<option name='".$value[0]."' value='".$value[1]."'>".$value[2]."</option>";echo "<option name='".$value[0]."' value='".$value[1]."'>".$value[2]."</option>";
    }

}

I'm struggling to understand if this is actually possible. Any advice, feedback and suggestions welcomed.

2
  • Maybe you want to add an optgroup for each of those sections of your array? Commented Jul 13, 2014 at 15:41
  • @quickshiftin - appreciate that but I would like to split out into seperate dropdowns. Commented Jul 13, 2014 at 15:46

2 Answers 2

1

Something like this

//------------------------------------------------------------
// Original array of data
//------------------------------------------------------------
$a = array(
'4f5hfgb'  => array('ACME', '4f5hfgb', 'Aberdeen'),
'sdf4ws'   => array('ACME', 'sdf4ws', 'Birmingham'),
'dfgdfg54' => array('EDNON', 'dfgdfg54', 'Birmingham'),
'345bfg'   => array('EDNON', '345bfg', 'Birmingham'),
'345fgfd'  => array('VALVE', '345fgfd', 'Birmingham'));

//------------------------------------------------------------
// Group the options by 'name'
//------------------------------------------------------------
$aGroupedOptions = array();
foreach($a as $aRawOption) {
    list($sName, $sValue, $sOption) = $aRawOption;
    $aGroupedOptions[$sName][]      = array($sValue, $sOption);
}

//------------------------------------------------------------
// Build the HTML
//------------------------------------------------------------
foreach($aGroupedOptions as $sName => $aOptions) {
    echo
    '<span class="Question SelectBox">
    <span class="qnum noshow"></span>
    <span class="qtext">Option</span>
    <span class="shown">
     <select class="combobox" id="option-' . $sName . '" name="option-' . $sName . '">';

    foreach($aOptions as $aOption) {
        list($sValue, $sOption) = $aOption;
        echo '<option name="'. $sName . '" value="' . $sValue . '">' .
             $sOption . '</option>';

     echo"</select>
     </span>
     </span>
     <span class='clearfix'></span>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Personally I would reorganize the inbound array as I've done here, it's more expensive, but it makes the code easier to understand IMO.
0

You want a nested foreach loop. An outer loop to create the dropdowns, and an inner loop to create the options:

foreach ($dropdowns as $options) {
    echo "<select ....>";
    foreach ($options as $option) {
        echo "<option>$option</option>";
    }
    echo "</select>
}

Stay safe against XSS attacks!

Comments

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.