Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php foreach loop wrapping every 2 items

Tags:

php

<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

like image 941
Joelgullander Avatar asked May 04 '15 13:05

Joelgullander


People also ask

How to use the foreach loop in PHP?

As described in the first section, there are two ways to use the foreach loop in PHP. Both are described below. In this way of using the foreach loop, you have to specify the array name followed by a $value variable. For each iteration, the current array element’s value is assigned to the $value variable.

How to modify an array inside a loop in PHP 7?

As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior. In PHP 7, foreach does not use the internal array pointer. In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

How to iterate through array elements in PHP?

The foreach loop is used to iterate through array elements in PHP. This is how you can use the foreach loop: Both of these methods of using foreach are explained in the last part of this tutorial, after the examples below. In this example, we have created an array of five elements with numeric values.

Can you modify an array in a foreach loop?

Note that foreach does not modify the internal array pointer, which is used by functions such as current () and key () . It is possible to customize object iteration . In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference .


3 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>"
like image 57
Axel Amthor Avatar answered Oct 16 '22 23:10

Axel Amthor


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]

like image 34
mondjunge Avatar answered Oct 16 '22 22:10

mondjunge


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.

like image 3
Karsten Avatar answered Oct 17 '22 00:10

Karsten