0

Hi I have the below code that runs correctly in a WordPress container on my local machine but reports the following errors in the staging environment.

Can anybody explain what is happening? Even in staging the values contained in the array are actually displayed/echoed correctly.

Notice: Undefined variable: contact_repeater_array in /www/wp-content/themes/my_theme/template-parts/flexible-content/contact/contact-section.php on line 37

Notice: Undefined variable: contact_repeater_array in /www/wp-content/themes/my_theme/template-parts/flexible-content/contact/contact-section.php on line 43

<?php if ( get_row_layout() == 'contact_section' ) :

    class ContactRepeaterItem {

        public $icon_image;
        public $title;
        public $body;
    }

    class ContactSection {

        public $contact_repeater_array;
        public $map_image;
        public $message;
        public $ninja_forms_id;

    }



    $contact_section                 = new ContactSection();
    $contact_section->map_image      = get_sub_field( 'map_image' );
    $contact_section->message        = get_sub_field( 'message' );
    $contact_section->ninja_forms_id = get_sub_field( 'ninja_forms_id' );

    if ( have_rows( 'contact_repeater' ) ) :
        while ( have_rows( 'contact_repeater' ) ) :
            the_row();

            $contact_repeater_item             = new ContactRepeaterItem();
            $contact_repeater_item->icon_image = get_sub_field( 'icon' );
            $contact_repeater_item->title      = get_sub_field( 'title' );
            $contact_repeater_item->body       = get_sub_field( 'body' );

            // pushing the same object over and over and over to the array.
            $contact_section->$contact_repeater_array[] = $contact_repeater_item;
        endwhile;
    endif;

    function display_contact_items( $_contact_section ) {

        if ( ! empty( $_contact_section->$contact_repeater_array ) ) :
            foreach ( $_contact_section->$contact_repeater_array as $item ) {
                ?>

                    <div class="contact_item">
                        <div class="contact_icon_container">
                            <div class="icon">
                                <img src="<?php echo esc_url( $item->icon_image['url'] ); ?>" alt=""/>
                            </div>
                        </div>
                        <div class="contact_detail_container">
                            <div class="title">
                                <?php echo esc_html( $item->title ); ?>
                            </div>
                            <div class="body">
                                <?php echo esc_html( $item->body ); ?>
                            </div>
                        </div>
                    </div>

                <?php
            }
        endif;
    }

    ?>
cabrerahector
  • 3,653
  • 4
  • 16
  • 27
Robin M
  • 664
  • 6
  • 18
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – aynber Dec 11 '19 at 14:27
  • 2
    Remove the `$` before `contact_repeater_array` when trying to access it in `$contact_section->$contact_repeater_array[]`. Dollar signs aren't used when accessing regular class variables. – aynber Dec 11 '19 at 14:28
  • @aynber I've seen that "Does this...." lately. Is that a new (undocumented) method of flagging as a duplicate? – Funk Forty Niner Dec 11 '19 at 14:28
  • @FunkFortyNiner Yup, it is. It's a bit confusing, isn't it? It's not the undocumented way, it's just the new wording when you flag it. – aynber Dec 11 '19 at 14:29
  • 1
    Ah ok, thanks @aynber I doubt I can see it since I don't need to flag as a duplicate, given I have a gold badge for it. Maybe I'll never know *lol!* – Funk Forty Niner Dec 11 '19 at 14:49

1 Answers1

0

You have a typo.

$contact_section->$contact_repeater_array[] = $contact_repeater_item;

should be

$contact_section->contact_repeater_array[] = $contact_repeater_item;

without the $

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39