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 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题