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>
";
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.
question from:
https://stackoverflow.com/questions/65849347/php-add-dropdown-options-from-an-array 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…