在foreach循环中为每个类添加+1

时间:2015-07-10 08:36:01

标签: php wordpress foreach

我在网站上使用WordPress。 我有下面的粘贴循环,我想为每个输出ID名称添加+1。

所以当前的ID是id =" plan1_price" 在每个循环之后,我希望它成为id =" plan2_price",id =" plan3_price"等...

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

1 个答案:

答案 0 :(得分:2)

您可以添加$ count并在每个循环中递增它:

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