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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛