Support

Account

Home Forums Add-ons Repeater Field Display tables 1/2 or 1/3 screen using repeater and get_row_index

Solved

Display tables 1/2 or 1/3 screen using repeater and get_row_index

  • Hi there.

    I’m trying to display tables using repeater, and I need to show them half screen if there are 1 or 2 tables (col-md-6, bootstrap) and on the third of the screen if there are 3 or more tables (col-md-4).

    Here is the code:

    <div class="row">
                    <?php if(have_rows('table_forms')):
                        while ( have_rows('table_forms') ) : the_row(); ?>
                            <?php
                            $counttables;
                            if(get_row_index() < 3) {$counttables = 6;} else {$counttables = 4;}
                            ?>
    
                            <div class="col-md-<?php echo $counttables; ?>">
                            <div class="main-table">
                                <h3><?php the_sub_field('table_forms_name'); ?></h3>
                                <table class="table main-table-slyle">
                                    <tbody>
                                    <tr>
                                        <td class="main-table-td"><h3>Длительность</h3></td>
                                        <td><?php the_sub_field('table_forms_dlit'); ?></td>
                                    </tr>
                                    <tr>
                                        <td class="main-table-td"><h3>Количество часов</h3></td>
                                        <td>
                                            <?php the_sub_field('table_forms_kol'); ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="main-table-td"><h3>Уровень</h3></td>
                                        <td>
                                            <?php the_sub_field('table_forms_urov'); ?>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                            </div>
    
                        <?php endwhile;?>
    
                    <?php
                    else :
                        // no rows found
                    endif;
                    ?>
            </div>

    And it’s obviously doesn’t work ($counttables = 6 for first and second loop, = 4 for others), because get_rows_index counting the variable on each loop.
    Could anyone help me? I’m not asking for the correct code, just for ideas (but code is might be helpful too).
    Sorry for bad english. Thank you in advance.

  • Hi @arctica112

    I’m not fully understanding what you’re after – and this forum is really for this sort of topic (this is more of a general PHP question) BUT I think you just need to do something like this:

    $amount = count(get_sub_field('table_forms') this will give you the total number of items in your repeater. Then you can check that number to determine how many columns you’ll be needing.

    Phil

  • Thanks a lot, I’ve already did it this way, and it works

    <div class="row">
                    <?php if(have_rows('table_forms')): ?>
                        <?php $numrows = count( get_field( 'table_forms' ) ); ?>
                        <?php while ( have_rows('table_forms') ) : the_row(); ?>
                            <?php if($numrows <3) {$counttables = 6;} else {$counttables = 4;} ?>
                            <div class="col-md-<?php echo $counttables; ?>">
                            <div class="main-table">
                                <h3><?php the_sub_field('table_forms_name'); ?></h3>
                                <table class="table main-table-slyle">
                                    <tbody>
                                    <tr>
                                        <td class="main-table-td"><h3>Длительность</h3></td>
                                        <td><?php the_sub_field('table_forms_dlit'); ?></td>
                                    </tr>
                                    <tr>
                                        <td class="main-table-td"><h3>Количество часов</h3></td>
                                        <td><?php the_sub_field('table_forms_kol'); ?></td>
                                    </tr>
                                    <tr>
                                        <td class="main-table-td"><h3>Уровень</h3></td>
                                        <td>
                                            <?php the_sub_field('table_forms_urov'); ?>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                            </div>
    
                        <?php endwhile;?>
    
                    <?php
                    else :
                        // no rows found
                    endif;
                    ?>
            </div>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Display tables 1/2 or 1/3 screen using repeater and get_row_index’ is closed to new replies.