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条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,