Skip to content
Advertisement

PHP – Add dropdown options from an array

I have an array

// Output of: print_r($table_data);
Array
(
    [5] => Array
        (
            [0] => hno1
            [1] => hno1
            [2] => hno1
            [3] => hno1
            [4] => hno2
            [5] => hno3
            [6] => hno3
            [7] => hno3
        )

    [7] => Array
        (
            [0] => Mhno1
            [1] => Mhno1
            [2] => Mhno2
        )

    [8] => Array
        (
            [0] => Ehno1
            [1] => Ehno1
        )

)
// Output of: var_export($table_data);
array (
  5 => 
  array (
    0 => 'hno1',
    1 => 'hno1',
    2 => 'hno1',
    3 => 'hno1',
    4 => 'hno2',
    5 => 'hno3',
    6 => 'hno3',
    7 => 'hno3',
  ),
  7 => 
  array (
    0 => 'Mhno1',
    1 => 'Mhno1',
    2 => 'Mhno2',
  ),
  8 => 
  array (
    0 => 'Ehno1',
    1 => 'Ehno1',
  ),
$row_count = count(max($table_data));

Then I’m looping through the result set and output the data

for($i = 0; $i < $row_count; $i++){
                        $row = $i + 1;
                        echo "<tr><td>{$row}</td>";
                        foreach($table_data as $column){
                            $items_unique=array_unique($column);
                            $field = $column[$i] ?? NULL;
                            if($field !==NULL){
                                foreach($items_unique as $dropdown_item){
                                    echo "<td><select name=''><option value='strtolower($dropdown_item)'>$dropdown_item</option></select></td>";
                                }
                            }
                            else{
                                echo "<td>$field</td>";
                            }
                        }
                        echo "</tr>n";

Output HTML should look like this:

    <table class="table table-responsive table-bordered">
  <tbody>
    <tr>
      <td>1</td>
      <td><select name="">
          <option value="strtolower(hno1)">hno1</option>
          <option value="strtolower(hno2)">hno2</option>
          <option value="strtolower(hno3)">hno3</option>
        </select></td>
      <td><select name="">
          <option value="strtolower(Mhno1)">Mhno1</option>
          <option value="strtolower(Mhno2)">Mhno2</option>
        </select></td>
      <td><select name="">
          <option value="strtolower(Ehno1)">Ehno1</option>
        </select></td>
    </tr>
    <tr>
      <td>2</td>
      <td><select name="">
          <option value="strtolower(hno1)">hno1</option>
          <option value="strtolower(hno2)">hno2</option>
          <option value="strtolower(hno3)">hno3</option>
        </select></td>
      <td><select name="">
          <option value="strtolower(Mhno1)">Mhno1</option>
          <option value="strtolower(Mhno2)">Mhno2</option>
        </select></td>
      <td><select name="">
          <option value="strtolower(Ehno1)">Ehno1</option>
        </select></td>
    </tr>
    <tr>
      <td>3</td>
      <td><select name="">
          <option value="strtolower(hno1)">hno1</option>
          <option value="strtolower(hno2)">hno2</option>
          <option value="strtolower(hno3)">hno3</option>
        </select></td>
      <td><select name="">
          <option value="strtolower(Mhno1)">Mhno1</option>
          <option value="strtolower(Mhno2)">Mhno2</option>
        </select></td>
      <td></td>
    </tr>
    <tr>
      <td>4</td>
      <td><select name="">
          <option value="strtolower(hno1)">hno1</option>
          <option value="strtolower(hno2)">hno2</option>
          <option value="strtolower(hno3)">hno3</option>
        </select></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td>5</td>
      <td><select name="">
          <option value="strtolower(hno1)">hno1</option>
          <option value="strtolower(hno2)">hno2</option>
          <option value="strtolower(hno3)">hno3</option>
        </select></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td>6</td>
      <td><select name="">
          <option value="strtolower(hno1)">hno1</option>
          <option value="strtolower(hno2)">hno2</option>
          <option value="strtolower(hno3)">hno3</option>
        </select></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td>7</td>
      <td><select name="">
          <option value="strtolower(hno1)">hno1</option>
          <option value="strtolower(hno2)">hno2</option>
          <option value="strtolower(hno3)">hno3</option>
        </select></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td>8</td>
      <td><select name="">
          <option value="strtolower(hno1)">hno1</option>
          <option value="strtolower(hno2)">hno2</option>
          <option value="strtolower(hno3)">hno3</option>
        </select></td>
      <td></td>
      <td></td>
    </tr>

What I need is for example the first column has three values in common, hno1, hno2 and hno3. I need to display these three values in all the dropdowns in the first column with the existing value($field) as preselected.

Thanks in advance.

Advertisement

Answer

Here is the full working code prototype – annotations explain everything.
Note: This will not post any values to a page or do something else as there are no <form> tags inside … at least it has unique ids for each select so it can be acessed via JS. Please mark the answer as accepted and upvote it if it is the desired result.

Update: 23.01 – 13:29 CET Inserted the code for the default selection of the element that is initially contained in the array.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php
$table_data= array
(    3 =>
        array ( ),
        5 =>
        array (
            0 => 'hno1',
            1 => 'hno1',
            2 => 'hno1',
            3 => 'hno1',
            4 => 'hno2',
            5 => 'hno3',
            6 => 'hno3',
            7 => 'hno3',
        ),
    7 =>
        array (
            0 => 'Mhno1',
            1 => 'Mhno1',
            2 => 'Mhno2',
        ),
    8 =>
        array (
            0 => 'Ehno1',
            1 => 'Ehno1',
        ));


//echo "<pre>";
//print_r($table_data); //uncomment these 3 lines to see the array
//echo "</pre><p>";

$number_cols= count($table_data);
//calculate the number of columns

$number_rows = max(array_map('count',  $table_data ) );
//number of rows needed for the table construction
// code taken from a comment from the php manual at count()

$akey=array_keys($table_data);
//as the main array is only associative and we cannot use foreach because of the table structure
// so we create a helper array where we can access the associative arrays in numerical order

// uncomment the next 3 lines to see the array content
//echo "<pre>";
//print_r($akey);
//echo "</pre>";


foreach ($table_data as $col_name =>$array_col) {
    //calculate the content of the selects once per column
    $select_content[$col_name] = array_values(array_unique($array_col));
     //print_r($select_content[$col_name]); //uncomment see what it contains if you are interested
}

echo "<p><table border='1'>";

       for ($i = 0; $i < $number_rows; $i++) {
           echo "<tr> ";
           for ($j = 0; $j < $number_cols; $j++){
               echo "<td>";
              if (isset($table_data[$akey[$j]][$i])) {
               $sname =  $akey[$j] ."-". $i;
               echo "<select name="{$sname}" id="{$sname}">";
               foreach ($select_content[$akey[$j]] as $row => $select_values)
               {   $select_values == $table_data[$akey[$j]][$i]?$selected="selected":$selected="";
                   echo "<option value="{$select_values}" $selected>{$select_values}</option>n";
               }
               echo "</select> n";
           }
               echo "</td>n";}
                  echo "</tr>n";}
         //   }
    ?>

</table>
</body>
</html>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement