2

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>
    <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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot, this method worked well, but I set $i = 1 and also change it to: <?php echo ($i % 5) ? '' : ' nomargin';?>"> Now it works perfectly! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.