0

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

1

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 
Sign up to request clarification or add additional context in comments.

3 Comments

Gee, thanks that was so much easier than expected. I don't really get the logic behind this, if you have time to explain, what actually is happening here? o_O
@CarlPapworth hopefully my edit explains it somewhat better, any more questions dont be afraid to ask
Cheers mate, that clarified it a bit :) You made me a little bit smarter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.