使用DB PHP中的值生成表行

时间:2015-04-27 17:49:34

标签: php mysql

我正在尝试创建一个动态生成的表,该表将在每行中显示多个下拉列表,其中包含存储在DB中的先前选定的值。

目前我只是在每个<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>";没有帮助。

我一直收到未定义的索引错误或所有字段都是单个值。

如果有任何不清楚或需要进一步解释,请告诉我,我会尽力使我的问题更清楚。

任何帮助都会很棒,

由于

2 个答案:

答案 0 :(得分:1)

尝试:

$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>";   
}

答案 1 :(得分:-2)

如果你连接像

这样的字符串会更好
$this->Table ="<table>";
while($MyRow = mysqli_fetch_array($fect)){
$this->Table .="<tr><td>".$MyRow['data']."</td></tr>";

}
$this->Table .="</table>";

return $this->Table ;