doukang1962 2016-02-23 09:41
浏览 43
已采纳

PHP:从一个变量动态添加每个输出的值

So I'm making a WP-template using ACF, and in it there's a table. Within this table want to add some results. Now, to get the total amount I have to add the value of all $price, which will vary for each since it is in a repeater field.

So how do I make dynamic variables and then add the value of them into a new variable?

my code:

<?php if (get_sub_field('offer_pr_ob')): while(has_sub_field('offer_pr_ob')): 
            $qt = floatval(get_sub_field('offer_pr_ob_qt'));
            $hrs = floatval(get_sub_field('offer_pr_ob_hrs'));
            $hrsRt = floatval(get_sub_field('offer_pr_ob_hrs-rt'));
            $hrsTot = $qt * $hrs;
            $price = $hrsTot * $hrsRt;
            $priceVat = $price * 1.25;  
            $priceTot = $price + $price; <-- this just outputs the value of 

the latest $price, e.g. "price*2" -->
?>
                                <tr class="pricingObject">
                                    <td><?php the_sub_field('offer_pr_ob_typ'); ?></td>
                                    <td><?php echo $qt; ?></td>
                                    <td><?php the_sub_field('offer_pr_ob_hrs-rt'); ?>.-</td>
                                    <td>
                                        <?php echo $hrs; ?>/<?php the_sub_field('offer_pr_ob_per'); ?></td>
                                    <td>

                                        <?php echo $hrsTot;?>h/&aring;r</td>
                                    <td><?php echo $price;.-</td>
                                    <td><?php echo $priceVat;?>.-</td>
                                </tr>
                                <?php endwhile; endif;?>
                                <tr class="pricingResult">
                                    <th>Total &aring;rskostnad</th>
                                    <th>&nbsp;</th>
                                    <th>&nbsp;</th>
                                    <th>&nbsp;</th>
                                    <th>&nbsp;</th>
                                    <th><?php echo $priceTot; ?>.-</th>
                                    <th>10 790 000.-</th>
                                </tr>
                            </table>
                    </div>
                    <?php endwhile; endif; ?>
  • 写回答

1条回答 默认 最新

  • douaikuai2715 2016-02-23 09:46
    关注

    you need to set the varible outside the while loop: also I changed it to add price to the variable you are incrementing. The variables exist only while in the loop you created them in and nothing outside the while loop can use it.

    Every time it loops the previous $priceTot is forgotten, this is why we need to set it outside the loop so it keeps all previous values, do not think of a while loop as a single bit of code, think of it as N x block ... every time it runs its essentially a new block of code, and every time is finishes the code has ended and the information cleaned out of memory.

    <?php 
    $priceTot = 0;
    if (get_sub_field('offer_pr_ob')): while(has_sub_field('offer_pr_ob')): 
            $qt = floatval(get_sub_field('offer_pr_ob_qt'));
            $hrs = floatval(get_sub_field('offer_pr_ob_hrs'));
            $hrsRt = floatval(get_sub_field('offer_pr_ob_hrs-rt'));
            $hrsTot = $qt * $hrs;
            $price = $hrsTot * $hrsRt;
            $priceVat = $price * 1.25;  
            $priceTot = $priceTot + $price; <-- this just outputs the value of 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器