使用PHP获取表头以用作行数据标签

时间:2018-02-23 20:20:03

标签: php html-table

如何检索每个相应的表标题$col以用作每行数据标签?

<table>
    <thead>
    <tr>
        <?php foreach ( $t[ 0 ] as $col ): ?>
            <th>
                <?php echo $col; ?>
            </th>
        <?php endforeach; ?>
    </tr>
    </thead>

    <tbody>
    <?php foreach ( $t as $idx => $row ): ?>
        <?php if ( $idx == 0 )
            continue; ?>
        <tr>
            <?php foreach ( $row as $col ): ?>
                    <td data-label="<?php Need to retrieve "th" col here ?>">
                    <div>
                        <?php echo str_replace( '"', '&quot;', $col ) ?>                                
                    </div>
                </td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>

    </tbody>
</table>

以下是var_dump(t)的示例:

array(3) { [0]=> array(6) { [0]=> string(11) "Part Number" [1]=> string(3) "CED" [2]=> string(3) "CEL" [3]=> string(5) "SHANK" [4]=> string(3) "OAL" [5]=> string(3) "CUT" } [1]=> array(6) { [0]=> string(11) "SCODSS-140A" [1]=> string(6) "1/4″" [2]=> string(6) "3/4″" [3]=> string(6) "1/4″" [4]=> string(8) "2-1/2″" [5]=> string(7) "Downcut" } [2]=> array(6) { [0]=> string(11) "SCOUSS-140A" [1]=> string(6) "1/4″" [2]=> string(6) "3/4″" [3]=> string(6) "1/4″" [4]=> string(8) "2-1/2″" [5]=> string(5) "Upcut" } }

所需的HTML输出。请注意,列标题已填充为数据标签:

<table>
    <thead>
    <tr>
                            <th>
                Part Number                    </th>
                            <th>
                CED                    </th>
                            <th>
                CEL                    </th>
                            <th>
                SHANK                    </th>
                            <th>
                OAL                    </th>
                            <th>
                CUT                    </th>
                    </tr>
    </thead>

    <tbody>
                                                    <tr>
                                    <td data-label="Part Number">
                    <div>
                        SCODSS-140A                             
                    </div>
                </td>
                                    <td data-label="CET">
                    <div>
                        1/4″                                
                    </div>
                </td>
                                    <td data-label="CEL">
                    <div>
                        3/4″                                
                    </div>
                </td>
                                    <td data-label="SHANK">
                    <div>
                        1/4″                                
                    </div>
                </td>
                                    <td data-label="OAL">
                    <div>
                        2-1/2″                              
                    </div>
                </td>
                                    <td data-label="CUT">
                    <div>
                        Downcut                             
                    </div>
                </td>
                            </tr>
                                    <tr>
                                    <td data-label="Part Number">
                    <div>
                        SCOUSS-140A                             
                    </div>
                </td>
                                    <td data-label="CET">
                    <div>
                        1/4″                                
                    </div>
                </td>
                                    <td data-label="CEL">
                    <div>
                        3/4″                                
                    </div>
                </td>
                                    <td data-label="SHANK">
                    <div>
                        1/4″                                
                    </div>
                </td>
                                    <td data-label="OAL">
                    <div>
                        2-1/2″                              
                    </div>
                </td>
                                    <td data-label="CUT">
                    <div>
                        Upcut                               
                    </div>
                </td>
                            </tr>

    </tbody>
</table>

1 个答案:

答案 0 :(得分:2)

根本不需要内部foreach循环。在迭代每行中的列时,可以使用该列的数字索引来引用第一行中的相应索引以获取列标题值。

<?php foreach ( $row as $colIndex => $colValue ): ?>
    <td data-label="<?= $t[0][$colIndex] ?>">
        <div><?= htmlspecialchars($colValue) ?></div>
    </td>
<?php endforeach; ?>

此外,str_replace不足以转义HTML文档的输出。您应该使用htmlspecialchars代替。