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 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?