Aaron
Aaron

Reputation: 10430

Adding +1 to each class in a foreach loop

I'm using WordPress for the site. I have the pasted loop below that I would like to add +1 to each of the output id names.

So the current ID is id="plan1_price" After each loop I would like it to become id="plan2_price", id="plan3_price" etc...

    <table class="
                <?php
                $best = rwmb_meta( 'tb_table1_bestcss' );
                if (!empty($best)){  echo "highlight-best-" . rwmb_meta( 'tb_table1_bestcss' ) . " ";  }
                $popular = rwmb_meta( 'tb_table1_popularcss' );
                if (!empty($popular)){ echo "highlight-popular-" . rwmb_meta( 'tb_table1_popularcss' ); } 
                ?>
            " border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <?php 
                        $tableheading = rwmb_meta( 'tb_table1_heading', 'type=text' );
                        foreach ( $tableheading as $index => $heading ) { 
                    ?>
                    <th scope="col"> 
                    <h3><?php echo $heading; ?></h3>
                        <p class="sub-heading">
                        <?php 
                                $tablesub = rwmb_meta( 'tb_table1_sub_heading' );
                                if (!empty($tablesub)) {
                                    $tablesubheading = rwmb_meta( 'tb_table1_sub_heading', 'type=text' );
                                    echo $tablesubheading[$index];
                                } 
                            ?>
                        </p>
<!--
    THIS IS THE ID i WOULD LIKE TO ADD +1 TO -- id="plan1_price"
-->
                        <div id="plan1_price" class="price">
                            <sup>$</sup>
                            <span class="amount">
                                <?php 
                                    $tableprice = rwmb_meta( 'tb_table1_price_heading' );
                                    if (!empty($tableprice)) {
                                        $tablepriceheading = rwmb_meta( 'tb_table1_price_heading', 'type=text' );
                                        echo $tablepriceheading[$index];
                                    } 
                                ?>
                            </span>
                            <small class="per-period">/mo</small>
                        </div>
                        </th>
                    <?php 
                        } 
                    ?>
                </tr>
    </table>

Upvotes: 2

Views: 665

Answers (1)

Danny Broadbent
Danny Broadbent

Reputation: 1209

You can add a $count and increment it on each loop:

<table class="
                <?php
                $count=1;
                $best = rwmb_meta( 'tb_table1_bestcss' );
                if (!empty($best)){  echo "highlight-best-" . rwmb_meta( 'tb_table1_bestcss' ) . " ";  }
                $popular = rwmb_meta( 'tb_table1_popularcss' );
                if (!empty($popular)){ echo "highlight-popular-" . rwmb_meta( 'tb_table1_popularcss' ); } 
                ?>
            " border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <?php 
                        $tableheading = rwmb_meta( 'tb_table1_heading', 'type=text' );
                        foreach ( $tableheading as $index => $heading ) { 
                    ?>
                    <th scope="col"> 
                    <h3><?php echo $heading; ?></h3>
                        <p class="sub-heading">
                        <?php 
                                $tablesub = rwmb_meta( 'tb_table1_sub_heading' );
                                if (!empty($tablesub)) {
                                    $tablesubheading = rwmb_meta( 'tb_table1_sub_heading', 'type=text' );
                                    echo $tablesubheading[$index];
                                } 
                            ?>
                        </p>
<!--
    THIS IS THE ID i WOULD LIKE TO ADD +1 TO -- id="plan1_price"
-->
                        <div id="plan<?php echo $count; ?>_price" class="price">
                            <sup>$</sup>
                            <span class="amount">
                                <?php 
                                    $tableprice = rwmb_meta( 'tb_table1_price_heading' );
                                    if (!empty($tableprice)) {
                                        $tablepriceheading = rwmb_meta( 'tb_table1_price_heading', 'type=text' );
                                        echo $tablepriceheading[$index];
                                    } 
                                ?>
                            </span>
                            <small class="per-period">/mo</small>
                        </div>
                        </th>
                    <?php
                            $count++;
                        } 
                    ?>
                </tr>
    </table>

Upvotes: 2

Related Questions