Php foreach loop wrapping every 2 items

后端 未结 6 1355
梦谈多话
梦谈多话 2021-02-06 08:03
query(a
相关标签:
6条回答
  • 2021-02-06 08:31

    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 }  ?>
    
    0 讨论(0)
  • 2021-02-06 08:32

    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>"
    
    0 讨论(0)
  • 2021-02-06 08:36

    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.

    0 讨论(0)
  • 2021-02-06 08:40

    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]

    0 讨论(0)
  • 2021-02-06 08:41

    Try this

    $i = 1;
            //Build custom items
            foreach($children as $child){
            if($i>2){
             $i =1;
            }
    
            if ($i==2) {
               //close row
            }
           $i++;
       }
    
    0 讨论(0)
  • 2021-02-06 08:43
    # Process every second item starting with the first one [0].
    foreach ($array as $key => $value) {
        if (($key - 1) % 2 === 0) {
            continue;
        }
    
        # Do something here.
    
    }
    
    0 讨论(0)
提交回复
热议问题