While Loop Repeating Content Not Rows

I’m using a repeater with the following data:
Header1 Data 1
Header2 Data 2
Header3 Data 3
Header4 Data 4
Header5 Data 5

What’s going on? The data posts, but the table seems to repeat the data.

It’s obvious that I don’t want the whole table to repeat data. Is my ‘while’ loop in the wrong place?
Code:

<?php if( get_field('specs') ): ?>
        <div role="tabpanel" class="tab-pane fade" id="specifications">
            <div class="container">
            <?php if( have_rows('specs') ): ?>

            <?php while( have_rows('specs') ): the_row(); 
        
                // vars
                $rows = get_field('specs');
                $specsheading = get_sub_field('heading');
                $specsdescription = get_sub_field('description');
        
             echo '<table class="table table-striped" border="0">';
             echo '<tbody>';
                if($rows) { 
                     foreach($rows as $row) {
                echo '<tr>';
                    echo '<th scope="row">' . $row['heading'] .'</th>';
                        echo '<td>' .$row['description'] . '</td>';
                
                echo '</tr>';
                    }
                }
             echo '</tbody>';
             echo '</table>';
             ?>

            <?php endwhile; ?>
        
        
        <?php endif; ?>

Is this a CMS?

No.
You can see the results here.

That looks like it’s a WordPress site.

I asked because the question may be better in the CMS and WP category where people may have a better understanding of some of the functions like have_rows() and get_field().

Would you like it moved there?

Sure! Thank you!
I figured that since this was PHP that it would be fitting to be here.

No probs!

Looks like I’ve managed to fix my code, except that the very first record does not display:

<?php if( get_field('specs') ): ?>
        <div role="tabpanel" class="tab-pane fade" id="specifications">
            <div class="container">
            <?php if( have_rows('specs') ): the_row(); ?>
            <?php
                // vars
                $rows = get_field('specs');
                $specsheading = get_sub_field('heading');
                $specsdescription = get_sub_field('description');
        ?>
             
             <table class="table table-striped" border="0">
             <tbody>

            <?php while( have_rows('specs') ): the_row(); ?>
            
                <tr>
                    <th scope="row"><?php the_sub_field('heading'); ?></th>
                        <td><?php the_sub_field('description'); ?></td>
                
                </tr>
            <?php endwhile; ?>
            
            </tbody>
            </table>
        
            <?php endif; ?>
            </div>
             
        </div>
    <?php endif; ?>

There should be a record above the circled one (POWER).

Okay…I think I’ve fixed the issue where the first record wasn’t displaying:

<?php if( get_field('specs') ): ?>
        <div role="tabpanel" class="tab-pane fade" id="specifications">
            <div class="container">
            <?php
                // vars
                $rows = get_field('specs');
                $specsheading = get_sub_field('heading');
                $specsdescription = get_sub_field('description');
        ?>
             
            <?php if( have_rows('specs') ): ?>
             <table class="table table-striped" border="0">
             <tbody>

             <?php while( have_rows('specs') ): the_row(); ?>
        
                <tr>
                    <th scope="row"><?php the_sub_field('heading'); ?></th>
                    <td><?php the_sub_field('description'); ?></td>
                
                </tr>
            <?php endwhile; ?>
            
            </tbody>
            </table>
        
            <?php endif; ?>
            </div>
             
        </div>
    <?php endif; ?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.