Home » Php » Php foreach loop wrapping every 2 items

Php foreach loop wrapping every 2 items

Posted by: admin July 12, 2020 Leave a comment

Questions:
<div class="puffar">
        <?php
        //Set up the objects needed
        $my_wp_query = new WP_Query();
        $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

        //Get children
        $children = ($post->post_parent) ? get_page_children( $post->post_parent, $all_wp_pages ) :  get_page_children( $post->ID, $all_wp_pages );

        $i = 0;
        //Build custom items 
        foreach($children as $child){ 
        $i++;

        /*
        if (i % 2 == 0) { ?>

        <?php
        } */
        ?>

<div class="col-sm-6">
    <div class="puff">
        <div class="puff-image-holder">
            <?php echo get_the_post_thumbnail( $child->ID, 'full' ); ?>
        </div>
        <fieldset class="linedHeadline hlmedium">
            <legend><?php echo get_the_title($child->ID); ?></legend>
        </fieldset>
        <?php echo get_field("puff_introtext", $child->ID); ?>
        <?php
        $values = get_field( 'puff_lanktext', $child->ID );
        if (get_field( "popup_eller_lank", $child->ID ) == "popup") {
        ?>
        <fieldset class="linedHeadline hlmedium">
            <legend><a class ="linktopage open-popup" href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field( "puff_lanktext", $child->ID ); ?> </a></legend>
        </fieldset>
        <?php
        } elseif (get_field( "popup_eller_lank", $child->ID ) == "extern") {
        ?>
            <fieldset class="linedHeadline hlmedium">
            <legend><a class ="linktopage" href="<?php echo get_field( "puff_lank", $child->ID ); ?>"><?php echo get_field( "puff_lanktext", $child->ID ); ?> </a></legend>
        <?php
        } else { }
        ?>

    </div>
</div>

<?php } ?>
</div>

Hi stackers!

I need some php help on how to wrap the looped elements. I want to wrap 2 elements in a <div class="row">. So basically <row> <item> <item> </row>

I have tried with some modulo as you can see, some if statements is still there. I set i as 0, and was trying to put <div class="row"> when 1 % 2 = 0 but found no solution on how to close the tags correctly ( should be closed after the second item)
Any chance you could help out me as a novice php hacker?

EDIT:

    <div class="puffar">
        <?php
        //Set up the objects needed
        $my_wp_query = new WP_Query();
        $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

        //Get children
        $children = ($post->post_parent) ? get_page_children( $post->post_parent, $all_wp_pages ) :  get_page_children( $post->ID, $all_wp_pages );

        $i = 0;
        //Build custom items 
        echo "<div class='row'>";
        foreach($children as $child){   
        ?>

<div class="col-sm-6">
    <div class="puff">
        <div class="puff-image-holder">
            <?php echo get_the_post_thumbnail( $child->ID, 'full' ); ?>
        </div>
        <fieldset class="linedHeadline hlmedium">
            <legend><?php echo get_the_title($child->ID); ?></legend>
        </fieldset>
        <?php echo get_field("puff_introtext", $child->ID); ?>
        <?php
        $values = get_field( 'puff_lanktext', $child->ID );
        if (get_field( "popup_eller_lank", $child->ID ) == "popup") {
        ?>
        <fieldset class="linedHeadline hlmedium">
            <legend><a class ="linktopage open-popup" href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field( "puff_lanktext", $child->ID ); ?> </a></legend>
        </fieldset>
        <?php
        } elseif (get_field( "popup_eller_lank", $child->ID ) == "extern") {
        ?>
            <fieldset class="linedHeadline hlmedium">
            <legend><a class ="linktopage" href="<?php echo get_field( "puff_lank", $child->ID ); ?>"><?php echo get_field( "puff_lanktext", $child->ID ); ?> </a></legend>
        <?php
        $i++;  
        if ($i % 2 == 0) { 
            echo "</div><div class='row'>";
        }
        } else { }
        ?>
    </div>
</div>

<?php } ?>
</div>
</div>

This only wraps all my looped items, I want the div class=row to only wrap every 2 items

How to&Answers:

you’re almost there:

    //Build custom items 
    echo "<row>";
    $i = 0;
    foreach($children as $child) { 

        echo "item "; 

        $i++;  
        if ($i % 2 == 0 && $i != count($children)) { 
            echo "</row><row>";
        }

    }
    echo "</row>"

Answer:

Or this:

<?php
$i=0;
foreach($children as $child){ 
    ++$i;
    if($i==1){
        echo "<row>";
        echo "<item>$child</item>";
    }
    if($i==2){
        echo "<item>$child</item>";
        echo "</row>"
        $i=0;
    }
}
[UPDATE]

This bugs me: An odd count of children could possibly lead to a row without closing tag.
While most Browser will just add the tag on render and you will have no problems at all, this is still not 100% correct.

On odd children count, you would want a check and a closing row after the foreach loop like this:

if($i==1){
    echo "</row>";
}

If $i == 1 after the loop, it was an odd count of children, and the row have to be closed.

[/UPDATE]

Answer:

You should use a for loop instead of a foreach loop like so:

for($i = 0; $i < count($children); $i+=2) {
    $child1 = $children[$i];
    $child2 = $children[$i+1];
    // print both
}

if you may have an odd number of children you have to check if $i+1 < count($children) before printing it.

Answer:

This is in WordPress but understands it and it will help you.

<?php $query = new WP_Query( array( 'post_type' => 'slides', 'order'=> 'DESC', 'post_status' => 'publish', 'posts_per_page' => -1) );
               $posts = $query->posts; 
               $numOfCols = 2;
                $rowCount = 0;
               if(!empty($posts)){ ?>

                   <div class="carousel-item <?php echo ($numOfCols-1==1)?'active':''; ?>">
                    <div class="row"> 

                      <?php foreach ($posts as $post) { ?>
                        <div class="col-md-6 pt-4 pb-0 " >
                           <h6 class="mb-2 text-uppercase"><b><a href="<?php echo get_permalink( $post->ID); ?>" target="_blank"><?php echo $post->post_title; ?></a></b></h6>
                           <span><?php echo get_the_excerpt($post->ID); ?></span><span class="float-right"><a href="<?php echo get_permalink( $post->ID); ?>" target="_blank"><i _ngcontent-ttx-c19="" class="material-icons icon-image-preview" style="position: relative;  top: 7px;">arrow_forward</i></a></span>
                        </div>

                      <?php 
                      $rowCount++; 
                      if($rowCount % $numOfCols == 0 && $rowCount != count($posts)) echo '</div></div><div class="carousel-item "><div class="row"> ';
                    } ?>

                </div>
                  </div>
    <?php }  ?>

Answer:

Try this

$i = 1;
        //Build custom items
        foreach($children as $child){
        if($i>2){
         $i =1;
        }

        if ($i==2) {
           //close row
        }
       $i++;
   }