the foreach in the loop returns bool(false) though array is

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
littleangel
Forum Newbie
Posts: 1
Joined: Sat Aug 30, 2014 11:47 pm

the foreach in the loop returns bool(false) though array is

Post by littleangel »

in my code below the **data is extracted correctly for the first foreach but doesn't return anything for 2nd foreach though selected one present inside the 2nd foreach** i still get the error Warning: Invalid argument supplied for foreach() for line `foreach ($authorlist as $post)`... however the 2nd foreach returns data correctly as soon as i remove the first foreach loop.

below is my code

Code: Select all

 <?php
    
    $all= get_posts(array('post_type' => 'books', 'numberposts' => -1,));
    
    foreach ( $all as $post ) :  setup_postdata($post);
    
        if (!empty($post))
        {
         $booklink = array();
    
         $booklist = get_field('booklist'); 
                   foreach ($booklist as $post) : setup_postdata($post);
                        if (!empty($post))
                            {
                            $booklink[] = strip_tags(get_field('booklink',$post));                                       
                            }
                    endforeach;
                    wp_reset_postdata();
         echo $booklink[0];  
    
         $authorname = array();
    
         $authorlist = get_field('authorlist'); 
                   foreach ($authorlist as $post) : setup_postdata($post);
                        if (!empty($post))
                            {
                            $authorname[] = strip_tags(get_field('authorname',$post));                                       
                            }
                    endforeach;
                    wp_reset_postdata();
    echo $authorname[0]; 
    
        }
    endforeach;
    ?>
i did var_dump($authorlist) ...it returns bool(false) as long as foreach ($booklist as $post) loop is present. The moment i remove that loop then the dump shows the data of the authorlist correctly...how to get both the loops working together.

u can see the output here http://myproject.byethost7.com/wp/?page_id=6
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: the foreach in the loop returns bool(false) though array

Post by social_experiment »

Code: Select all

<?php
$booklist = get_field('booklist'); 
                   foreach ($booklist as $post) : setup_postdata($post);
                        if (!empty($post))
                            {
                            $booklink[] = strip_tags(get_field('booklink',$post));                                       
                            }
                    endforeach;
                    //wp_reset_postdata();
         echo $booklink[0];
?>
what are your results if you comment the wp_reset_postdata() function after the first foreach loop?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply