dongyou26216708 2015-04-27 17:49
浏览 23
已采纳

使用DB PHP中的<td>值生成表行

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

  • 写回答

2条回答 默认 最新

  • dongxingqiu7943 2015-04-27 17:59
    关注

    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>";   
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料