PHP计数循环添加额外的行

时间:2017-07-20 15:33:17

标签: php html

我正在尝试将每3篇文章包装在一个div中,以便我的HTML像这样:

<div class="row">
  <article>Article One</article>
  <article>Article Two</article>
  <article>Article Three</article>
</div>
<div class="row">
  <article>Article Four</article>
  <article>Article Five</article>
  <article>Article Six</article>
</div>

下面是我的PHP。这就是我目前所拥有的,但是在开头添加了一行,我不想要。

$i = 0;
echo '<div class="row">';
if ($my_query->have_posts()) : 
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php if($i % 3 == 0) {echo '</div><div class="row">';}?>

以上内容如下:

<div class="row"></div> //I don't want this to be in the HTML
<div class="row">
  <article>Article One</article>
  <article>Article Two</article>
  <article>Article Three</article>
</div>
<div class="row">
  <article>Article Four</article>
  <article>Article Five</article>
  <article>Article Six</article>
</div>

我尝试将$i = 0更改为$i = 1,但这也无效。这将在标记中打印出来:

    <div class="row">
      <article>Article One</article>
      <article>Article Two</article>
    </div>
    <div class="row">
      <article>Article Three</article>
      <article>Article Four</article>
      <article>Article Five</article>
    </div>
    <div class="row">
      <article>Article Six</article>
    </div>

我尝试了不同的数字组合,但我似乎无法简单地将每3篇文章包装好。

3 个答案:

答案 0 :(得分:3)

写下您的情况如下: -

if(!empty($i) && $i % 3 == 0)

答案 1 :(得分:2)

对于这种事情,我通常会发现创建一个HTML元素数组并在需要时打印它们更容易。它会是这样的:

$listItems = array();
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) {
        $my_query->the_post();

        // Convert the post to <article>...</article>
        // and put the result in $articleHtml

        $listItems[] = $articleHtml;

        if (count($listItems) === 3) {
            // We have 3 items - print them
            echo '<div class="row">' . implode('', $listItems) . '</div>';
            $listItems = array();
        }
    }
}

// Don't forget the last items
if (count($listItems)) {
    echo '<div class="row">' . implode('', $listItems) . '</div>';
}

答案 2 :(得分:0)

让我们先分析需求并制定计划。

由于您需要在<div>中包装每三个项目,因此<div>开放标记必须在之前生成每组三个并且</div>结束标记之后每组三个。 之前一组三个意味着“在第一篇文章之前的零个或多个三个组之后”。

最后一组必须特别小心。如果它已经完成,那么在循环期间已经产生了结束标记,否则它必须在循环之后生成。

现在让我们编写代码:

// $i always count how many articles were already displayed
// none yet
$i = 0;
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) {
        if ($i % 3 == 0) {
            // $i is multiple of three
            // Zero or more complete groups were already displayed
            // Start a new group
            echo '<div class="row">';
        }

        // Display the current article and count it
        $my_query->the_post();
        $i ++;

        // After each group of three, close the group
        if ($i % 3 == 0) {
            // A multiple of three articles was displayed; close the group
            echo '</div>';
        }
    }

    // If the last group is not complete then must close it now
    if ($i % 3 != 0) {
        // The last group contains 1 or 2 articles; it was not closed yet
        echo '</div>';
    }
}