CodeIgniter database to view help
I'm new to Codeigniter and I'm trying to develop a simple website with a couple of pages. Under my "Work" view I have it looping through the database I've set up:
<div id="content">
<?php foreach ($records as $row){?>
<div class="item">
<p class="num"><?php echo $row->id;?></p&g开发者_StackOverflow中文版t;
<h2><?php echo $row->title;?></h2>
<p><?php echo $row->type;?></p>
<p><?php echo $row->brief;?></p>
</div>
Right now everything works fine and the view populates the information into the view, but I want every 5th entry to have an additional class of "nomargin" added to the div. For example:
<div class="item">
1
Title
Type
Desc
<div class="item">
<p class="num">1</p>
<h2>Title</h2>
<p>Type</p>
<p>Desc</p>
</div>
<div class="item">
<p class="num">1</p>
<h2>Title</h2>
<p>Type</p>
<p>Desc</p>
</div>
<div class="item">
<p class="num">1</p>
<h2>Title</h2>
<p>Type</p>
<p>Desc</p>
</div>
<div class="item nomargin">
<p class="num">1</p>
<h2>Title</h2>
<p>Type</p>
<p>Desc</p>
</div>
It's prolly a simple if statement but I dont know enough about php/codeigniter to actually do it. Thanks in advance.
This should to it
<div id="content">
<?php $i = 0; ?>
<?php foreach ($records as $row){?>
<div class="item <?php echo ($i % 5) ? '' : 'nomargin';?>">
<p class="num"><?php echo $row->id;?></p>
<h2><?php echo $row->title;?></h2>
<p><?php echo $row->type;?></p>
<p><?php echo $row->brief;?></p>
<?php $i++ ?>
</div>
I hope this helps.
精彩评论