1

I want to print a <div> class called 'first' in 1, 4, 7, 10, 13 ...... position What should i need to do it ?

<?php
$i=0;  
?>

<?php
while (have_posts()) : the_post(); 
?>



<div class="column dt-sc-one-third<?php if ($i==0){ ?> first <?php } ?>"> 

<p class="comments">
<a title="Comment on Latin words Ipsum" href="#">
<span class="fa fa-comment"> </span>
25
</a>
</p>


</div>


<?php $i++; endwhile; ?>

I want first div should have a class name called first and the 4 th div should have that class and so on.....

Dipankar Das
  • 131
  • 2
  • 11

1 Answers1

2

You can use Modulo operator of PHP. For example $i%3 divides the variable by 3 gives reminder of the divider operation as return value. So, in your case it will always gives 1 for the values 1, 4, 7, 10, 13 ......
Please check the below code,

<?php if ($i%3 == 1){ ?> first <?php } ?>

Please refer, http://php.net/manual/en/language.operators.arithmetic.php

manian
  • 1,418
  • 2
  • 16
  • 32
  • You should add an explanation to the answer, not just "try". – M. Eriksson May 15 '17 at 05:41
  • it wasn't when I commented. But that's a good rule of thumb... always add an explanation. – M. Eriksson May 15 '17 at 05:43
  • 1
    Sure @Magnus. I will do that in my answers – manian May 15 '17 at 05:43
  • @Magnus, Not sure if it is good idea to delete my answer. I will leave my answer to be here but with explanation. I am not worrying about the downvotes. I already have kept few of my answers with negative votes, un-deleted because I think I posted correct answers. Your inputs please? – manian May 15 '17 at 05:54
  • Your solution is correct, it was just missing an explanation (it's important so the OP understands why this is correct so he/she can modify it to fit better). Now it's spot in.. upvote worhty. :) – M. Eriksson May 15 '17 at 06:35