SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple foreach statements

    Hi
    I'm using costum field in wp to show image and text multiple time but after the second time it give me this warning ... why?
    webpage

    meta

    Warning: Invalid argument supplied for foreach()

    PHP Code:
    <?php 
    $text 
    get_post_meta($post->ID"text"false);
    $image get_post_meta($post->ID"image"false);
    if (
    $text $image[0]=="")

     { 
    ?>
    <?php 
    } else { ?>

    <div class="Caption">
                                    
        <?php 
        
    foreach($text as $text) {
       foreach(
    $image as $image){
       echo     
    '<div class="tutorial">' ,   '<img src="'.$image.'"  width="250"" align="left" >'  ;
       echo          
    '<p>'.$text.'</p>'   
        
       }
    ?>

    </div>      <?php ?>

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    I'm... not a WP guru, but i'm fairly sure you shouldnt be nesting your foreach.

    Also, you cant say $text as $text, or you've overwritten $text.

    So...
    Script starts, gets an array for $text.
    Foreach $text as $text; the loop starts, takes the first element of $text, and sets it to $text. $text is now a string.
    Output happens.
    Loop ends. Foreach looks for the next element of the $text array... but $text isnt an array anymore. So it throws a warning.

    I think you're looking for something more like...
    PHP Code:
        foreach($text as $key => $textcur) {
       echo     
    '<div class="tutorial">' ,   '<img src="'.$image[$key].'"  width="250"" align="left" >'  ;
       echo          
    '<p>'.$textcur.'</p>'   
       } 
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    God bless you .... work perfectly ... have a fantastic day tnx

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    The error in your foreach script is because the foreach script expects an array as the first parameter and iterates through the array showing each string $item and possibly a numeric $key if it is mapped to the string $item.

    Your foreach script first parameter is an array and uses the same array name as the second iterator parameter.

    I always pluralise the first array parameter and then use the $key => $item:

    Try this: (not tested
    PHP Code:

    <?php  
      $textS   
    get_post_meta($post->ID"text"false); 
      
    $imageS get_post_meta($post->ID"image"false); 

      if (
    $textS $imageS[0] !== "")
      { 
        echo 
    '<div class="Caption">'
           foreach(
    $textS as $text)
           { 
               foreach(
    $imageS as $key => $image)
               { 
                   echo  
    '<div class="tutorial">';
                     echo 
    '<img src="' .$image .'"  width="250"" align="left" />'  
                     echo 
    '<p>';   
                        echo 
    '$key => ' .var_dump($key);
                     echo 
    '</p>';   
                     echo 
    '<p>' .$text .'</p>';  
                   echo 
    '</div>'// tutorial
               

           } 
        echo 
    '</div>'// Caption 
      
    }

    // using alternative syntax - do you think it is easier to understand?

      
    if ($textS $imageS[0] !== "") : // trailing colon

        
    echo '<div class="Caption">'
           foreach(
    $textS as $text) : // trailing colon
               
    foreach($imageS as $key => $image)  :  // trailing colon
                   
    echo  '<div class="tutorial">';
                     echo 
    '<img src="' .$image .'"  width="250"" align="left" />'  
                     echo 
    '<p>';   
                        echo 
    '$key => ' .var_dump($key);
                     echo 
    '</p>';   
                     echo 
    '<p>' .$text .'</p>';  
                   echo 
    '</div>'// tutorial
               
    endforeach; 
           endforeach; 
        echo 
    '</div>'// Caption 
      
    endif;

    ?>
    Last edited by John_Betong; Apr 22, 2013 at 22:34. Reason: formatting, wording and added alternative syntax
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •