Reputation: 35
I have created a mapping matrix that has requirements across the top and outcomes down the left hand side. This is in a table. Outcomes are mapped to requirements with an option in a select dropdown. There can be hundreds of requirements or outcomes, so the matrix table can be very big.
When I want to edit the saved mappings, I want the one saved in the DB table to be the selected option.
Each mapping has an x and y coordinate with a mapping.
id | xcoord | ycoord | mappedValue
-----------------------------------
1 | 1 | 1 | M2
2 | 2 | 2 | M3
The first foreach loop has the mapping options, and the second one has the saved mappedValue. Excerpt below.
foreach ($headersAndItemsArray as $eachHeaderItemRecord) {
list($thid_value, $headerText, $headerItemID, $headerItem) = explode('|', $eachHeaderItemRecord);
$dynamicTable .= "<td>";
$dynamicTable .= "<select>";
foreach ($mappingSelectionArray as $eachMappingOption) {
list($mappingCode, $mappingDesc) = explode('|', $eachMappingOption);
foreach ($matrixMappingsArray as $eachMappedOption) {
list($xcoord, $ycoord, $mappingOption) = explode('|', $eachMappedOption);
if ( ($rid_value == $xcoord) && ($headerItemID == $ycoord) ) {
$dynamicTable .= '<option value="'.$mappingCode.'" '.(($mappingCode == $mappingOption) ? 'selected' : '').'>'.$mappingCode.'</option>';
}
}
}
$dynamicTable .= "</select>";
$dynamicTable .= "</td>";
}
With the code above I can display the selected value in the dropdown. However, if there are items that have not been mapped, there is nothing in the dropdown. I have tried multiple options, including additional table cells and if/else statements. If I try to display all the mapping options inside the second foreach, I either get the options multiple times or not at all.
I do not want to query the DB for the select options each time. Since there can be hundreds of items in the matrix, this many impact performance.
Any thoughts on how to do this? Thank you!
Upvotes: 0
Views: 57
Reputation: 35
I solved this by setting a flag when looping through the foreach loops the first time, displaying nothing the first time. The second time I looped through, if the flag was TRUE, I showed the selected value. If the flag was FALSE, I showed the list of options.
Upvotes: 0