0

How can I retrieve each respective table header $col for use as each rows data label?

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

Here is a sample of 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" } }

Desired HTML output. Note, the column headers have been populated as data-labels:

<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>
3
  • Can you show a little sample from var_dump($t), and an example of the HTML you're trying to produce? Commented Feb 23, 2018 at 20:23
  • Updated question with var_dump and HTML. Commented Feb 23, 2018 at 20:34
  • 1
    Looping over all values again in the place where you want to output the label is rather nonsense. Change the outer loop to foreach ( $row as $idx2 => $col ):, so that you can access the header value corresponding to the current column index via $t[ 0 ][ $idx2 ] Commented Feb 23, 2018 at 20:40

1 Answer 1

2

The inner foreach loop isn't needed at all. As you iterate the columns in each row, you can use the numeric index of that column to refer to the corresponding index in your first row to get the column header value.

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

Also, str_replace isn't sufficient to escape your output for an HTML document. You should use htmlspecialchars instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.