php foreach包装每2个div

时间:2016-07-01 17:10:23

标签: php foreach

我需要用一个项目(一行)用另一个div包装每个2个div,所以它看起来像:

<div class="row">
  <div> Item </div>
  <div> Item </div>
</div>
<div class="row">
  <div> Item </div>
  <div> Item </div>
</div>

我尝试了一些解决方案但他们不工作,因为进来的物品很奇怪(9件)。这就是我所拥有的:

<?php 
$count = 0;
foreach ($contents as $content) 
{
    //var_dump($content);
    $books      = $content["tags"];
    $book_image = $content['content_image'];
    $book_desc  = $content['content_social_description'];

    ++$count;

    if($count == 1)
    {  
        echo "<div class='et_pb_row'>";
    }

    foreach ($books as $book) 
    {
        $book_name      = $book['tag_name'];
        $book_name_trim = str_replace(' ', '-', $book_name);
        ?>
        <!-- Inside the Book Loop -->
        <div class='et_pb_column et_pb_column_1_2 books' style="background: url('https://s3-us-west-2.amazonaws.com/crowdhubproverbs31/<?php echo $book_image ;?>');">
            <h2><?php echo $book_name; ?></h2>
            <p><?php echo $book_desc; ?></p>
            <?php echo $count; ?>
        </div>

        <?php

    }

    if ($count == 2)
    {
        echo "</div>";
        $count = 0;
     }
}
?>

除了第二行到最后一行有3个项目,即使它显示&#34;计数&#34;因为我回应它所以它应该重置但不是。所以它:

<div class="row">
  <div> Item </div> "Count 1"
  <div> Item </div> "Count 2"
</div>
<div class="row">
  <div> Item </div> "Count 1"
  <div> Item </div> "Count 2"
</div>
<div class="row">
  <div> Item </div> "Count 1"
  <div> Item </div> "Count 2"
  <div> Item </div> "Count 2"
</div>
<div class="row">
  <div> Item </div> "Count 1"
  <div> Item </div> "Count 2"
</div>

2 个答案:

答案 0 :(得分:0)

您可以利用array_chunk

//First make a generator to get all books
function allBooks($contents) {
    foreach($contents as $content) {    
        foreach($content['tags'] as $book) {
            yield $book; //Here you can yield whatever you want !
        }
    }
}

//Then create rows
$itemPerRow = 2;
$rows = array_chunk(iterator_to_array(allBooks($contents)), $itemPerRow, true);

//Display all 
foreach($rows as $row) {
   echo '<row>';
      foreach($row as $book) {
         //Display the book !
      }
   echo '</row>';
}

答案 1 :(得分:0)

您应该在图书循环中打开和关闭<rows/>,并为奇数书添加延迟检查:

<?php 
$count = 0;
foreach ($contents as $content) 
{
    //var_dump($content);
    $books      = $content["tags"];
    $book_image = $content['content_image'];
    $book_desc  = $content['content_social_description'];


    foreach ($books as $book) 
    {
        ++$count;
        if($count == 1)
        {  
            echo "<div class='et_pb_row'>";
        }
        $book_name      = $book['tag_name'];
        $book_name_trim = str_replace(' ', '-', $book_name);
        ?>
        <!-- Inside the Book Loop -->
        <div class='et_pb_column et_pb_column_1_2 books' style="background: url('https://s3-us-west-2.amazonaws.com/crowdhubproverbs31/<?php echo $book_image ;?>');">
            <h2><?php echo $book_name; ?></h2>
            <p><?php echo $book_desc; ?></p>
            <?php echo $count; ?>
        </div>

        <?php
        if ($count == 2)
        {
            echo "</div>";
            $count = 0;
        }


    }
}

if ($count > 0)
{
    echo "</div>";
}
?>

这样,只有在$count循环运行时,您的foreach($books AS $book)变量才会增加(因此您至少要打印一本书)