I am trying to create a dynamically made table that will display multiple drop down lists within each row with previously selected values that are stored within a DB.
Currently I am stuck on just displaying the proper values within each <td>
.
//$query..
$data = mysqli_query($dbc, $query);
echo"<table>
<tr>
<th>Component</th>
<th>Component Type</th>
<th>Component Thickness</th>
</tr>";
while ($row = mysqli_fetch_array($data)) { //while I have rows..
//add column values to an array
$facSecComponentID[] = $row['facility_section_components_id'];
$facSecComponent[] = $row['roof_component_id'];
$facSecComponentType[] = $row['roof_component_type_id'];
$facSecComponentThickness[] = $row['component_thickness'];
//try to loop through each index of each row and get the DB value..
//eventually use this value to assign a selected index within the drop down list
foreach ($row as $componentIndex => $selectedComponent) {
echo "<tr>";
echo "<td>" . $facSecComponent[$selectedComponent] . "</td>";
echo "<td>" . $facSecComponentType[$selectedComponent] . "</td>";
echo "<td>" . $facSecComponentThickness[$selectedComponent] . "</td>";
echo "</tr>";
}
}
echo "</table>";
I can't get the values I need here to display properly, I have also tried to do something like: "<td>" . $componentIndex[$selectedComponent] . "</td>";
which didn't help.
I keep getting undefined index errors or all fields being a single value.
Let me know if anything is unclear or needs further explanation and I will try to make my question more clear.
Any help would be great,
Thanks
Try:
$facSecComponentID = array();
$facSecComponent = array();
$facSecComponentType = array();
$facSecComponentThickness = array();
while ($row = mysqli_fetch_array($data)) {
$facSecComponentID[] = $row['facility_section_components_id'];
$facSecComponent[] = $row['roof_component_id'];
$facSecComponentType[] = $row['roof_component_type_id'];
$facSecComponentThickness[] = $row['component_thickness'];
}
$numItems = mysqli_num_rows($result);
for($i=0;$i<$numItems;$i++){
echo "<tr>";
echo "<td>{$facSecComponent[$i]}</td>";
echo "<td>{$facSecComponentType[$i]}</td>";
echo "<td>{$facSecComponentThickness[$i]}</td>";
echo "</tr>";
}
It's better if you concatenate a string like
$this->Table ="<table>";
while($MyRow = mysqli_fetch_array($fect)){
$this->Table .="<tr><td>".$MyRow['data']."</td></tr>";
}
$this->Table .="</table>";
return $this->Table ;