使用 <td> 生成表格行来自 DB PHP 的值 [英] Generate a table row with <td> values from DB PHP
问题描述
我正在尝试创建一个动态制作的表格,该表格将在每行中显示多个下拉列表,其中包含存储在数据库中的先前选择的值.
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.
目前我只是在每个 <td>
中显示正确的值.
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>";
我无法在此处获得正确显示所需的值,我还尝试执行以下操作:"<td>".$componentIndex[$selectedComponent] ."</td>";
没有帮助.
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.
任何帮助都会很棒,
谢谢
推荐答案
尝试:
$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>";
}
这篇关于使用 <td> 生成表格行来自 DB PHP 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!