Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Repeat a function with loop 'for' PHP

Hi everybody, I have a function called repeat_this_field() and I want to repeat some code inside that function, so I am using this code but it does not work:

<?php
function get_item_data_persons( $other_data, $cart_item ) {
    foreach ( $cart_item['booking'] as $key => $value ) {
        $total_persons = array_sum($value);
        if(!function_exists('repeat_this_field')) {
            function repeat_this_field( $checkout ) {
                for($i=0; $i<$total_persons; $i++) {
                    echo '<div id="my_custom_checkout_field'.$i.'"><h2>My Field</h2>';

                        woocommerce_form_field( 'my_field_name', array(
                        'type'          => 'date',
                        'class'         => array('form-row form-row-first validate-required validate-required'),
                        'label'         => __('Fill in this field'),
                        'placeholder'   => __('Enter something'),
                        ), $checkout->get_value( 'my_field_name' ));

                    echo '</div>';
                }
            add_action( 'woocommerce_after_checkout_billing_form', 'repeat_this_field' );
            }
        }
    }        
    return $other_data;
}
?>

Also 'my_field_name' must be different foreach item like 'my_field_name_1', 'my_field_name_2', 'my_field_name_3', etc.

Please help me. Thanks

2 Answers

What are die values of $key and $value for the first iteration of the first loop? Can you give an dump of the values please?

Thanks fro your answer, $key is the name of each $value, the $value retrieves a number, so i created the variable $total_persons to make a sum of all $values, I have tested $total_persons and it works ok. If I use this code repeats correctly:

<?php
function get_item_data_persons( $other_data, $cart_item ) {
    foreach ( $cart_item['booking'] as $key => $value ) {
        $total_persons = array_sum($value);
        for($i=0; $i < $total_persons; $i++) {
            echo '<div id="my_custom_checkout_field'.$i.'"><h2>My Field</h2>';   
        }
    }        
    return $other_data;
}
?>

But the problem is when i use this code because the variable $total_persons is not retrieving. How can i retrieve the value of $total_persons in this code:

<?php
function get_item_data_persons( $other_data, $cart_item ) {
    foreach ( $cart_item['booking'] as $key => $value ) {
        $total_persons = array_sum($value);
        if(!function_exists('repeat_this_field')) {
            function repeat_this_field( $checkout ) {
                for($i=0; $i < $total_persons; $i++) {
                    echo '<div id="my_custom_checkout_field'.$i.'"><h2>My Field</h2>';

                        woocommerce_form_field( 'my_field_name'.$i, array(
                        'type'          => 'date',
                        'class'         => array('form-row form-row-first validate-required validate-required'),
                        'label'         => __('Fill in this field'),
                        'placeholder'   => __('Enter something'),
                        ), $checkout->get_value( 'my_field_name'.$i ));

                    echo '</div>';
                }
            }
            add_action( 'woocommerce_after_checkout_billing_form', 'repeat_this_field' );
        }
    }        
    return $other_data;
}
?>

If you want the total items in $value you can use

<?php
count($value) 

// or

sizeof($value)
?>

sizeof() function in php is just an alias of the count() method

array_sum() counts the values in an array. It doesn't give you the number of items in an array