您好,我想基于foreach
中的奇数或偶数来呈现不同的html。我现在有以下代码:
<?=fuel_edit('create', 'Create Post', 'blog/posts')?>
<?php if (!empty($posts)) : ?>
<?php foreach($posts as $post) : ?>
<div class="cell small-12 large-6"></div>
<div class="cell small-12 large-6">
<div class="post">
<?=fuel_edit($post)?>
<?=blog_block('post_unpublished', array('post' => $post))?>
<h2><a href="<?=$post->url?>"><?=$post->title?></a></h2>
<div class="post_date">
<?=day($post->publish_date, 'd')?>.<?=month($post->publish_date, 'm')?>
<!--by <strong><span class="post_author_name"></*?=$post->author_name?></span></strong-->
</div>
<div class="post_content">
<?php if ($post->has_thumbnail_image()) : ?>
<img src="<?=$post->get_thumbnail_image_path()?>" alt="">
<?php endif; ?>
</div>
<div class="post_meta">
<?=$post->tags_linked ?>
</div>
</div>
<div class="clear"></div>
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="no_posts">
<p>There are no posts available.</p>
</div>
<?php endif; ?>
我想要什么时候第一个<div class="cell small-12 large-6"></div>
呈现为空,而这个div何时是第二个<div class="cell small-12 large-6"></div>
之后
请帮助我,我不知道。
答案 0 :(得分:1)
如果您想知道帖子列表中帖子是偶数还是奇数,则可以在循环浏览帖子时跟踪循环索引。
在以下示例中,每次执行循环时,$index
都会增加。通过检查$index
是否可被2整除,您知道它是偶数并呈现所需的内容:
<?php $index = 0; foreach($posts as $post) : ?>
<?php if($index % 2 == 0) { $?>
<!-- HTML for even post here. -->
<?php } else { ?>
<!-- HTML for odd post here. -->
<?php
}
$index = $index + 1;
?>
<?php endforeach; ?>