【问题标题】:Adding +1 to each class in a foreach loop在 foreach 循环中为每个类添加 +1
【发布时间】:2015-09-28 22:16:24
【问题描述】:

我在网站上使用 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>

【问题讨论】:

  • 你的意思是像id="plan1_price_&lt;?= $plan1_price_id++ ?&gt;"这样的吗?
  • @FoxRider 你不应该建议其他用户使用 PHP 短标签,并非所有服务器/设置都会启用此功能
  • @DannyBroadbent &lt;?= 已保存以供使用,并且“无论 short_open_tag ini 设置如何,始终可用”;见php.net/basic-syntax.phptags
  • @FoxRider 自 PHP 5.4.0 起,= 始终可用。默认情况下,旧版本的 PHP(许多用户仍在使用)将不提供此功能。不是每个人都能掌握最新的 PHP(有些人拥有庞大的系统,可能需要几个月的时间来改变,有些人更喜欢旧的 PHP 代码,因为他们没有适应新的变化)。这就是我的意思。很高兴为新的更新/更改提供建议,但除非您知道他们自己在使用它,否则我不会使用它。
  • @DannyBroadbent 是的,你是对的,我绝对支持你的方法。我只是没有注意到它只对 PHP >= 5.4.0 有效。我的错。 :( 幸运的是,cmets 不能拒绝投票。^^

标签: php wordpress foreach


【解决方案1】:

您可以添加一个 $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>

【讨论】:

  • 谢谢,几分钟后我会接受你的回答。
  • 如何使第一次迭代输出为 no.1
  • 带有 $ 标签的那个
  • 对不起,第一个 id 正在输出,像这样 id="plan_price" 第二个像这样 id="plan2_price"
  • 你看到顶部的
猜你喜欢
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多