Hello guys,

I am using a Bootstrap grid with 6 element (As articles). 5 come directly from the database, and I need to add one more article element at the beginning, just for aesthetics. This is what I have so far. Any help is welcome. Thank you!

Here's the idea of what i need https://imgur.com/a/JTlROZ2

<?php $i = 0 ?>
<?php  START  LOOP ?>
<?php if($i % 2 == 0): ?> 
<div class="ps-box"><!-- POST -->
   <div class="row"><!-- ROW -->
      <?php endif; ?>
      <article>
         Element
      </article>
      <?php if($i % 2 != 0): ?>
   </div><!-- / ROW -->
</div><!-- / POST -->
<?php endif; ?>
<?php $i++ ?>

<?php END LOOP ?>

Recommended Answers

Otherwise, you can change lines 7-9 to be something like:

<?php if ($i == 0): ?>
    <article> 6th Element </article>
<?php endif; ?>
<article>
    Element
</article>

That will inject the 6th element in at the beginning, before we increment $i.

Jump to Post

All 3 Replies

Re: Adding extra element with modulus in a PHP loop 80 80

What is <?php START LOOP ?> ?? I assume it's some logic you have that loops through articles?

I'm having a hard time following your code because of your indenting.

I actually have to run in just a moment so I don't have much time to figure out what you're trying to do here ... my guess is that you want to use modular division so that when you loop through the articles, you increment $i each time. Then, if $i is even, open the div, and if $i is odd, close the div.

Does the code that you have do just that and work properly?

If so, at which point are you trying to add another article? Do you have data about the article? Why can't you just add it to the stack of articles that you are loopng through on line 2?

In other words, it sounds to me like the 6th article should be added by the controller, not the view.

Re: Adding extra element with modulus in a PHP loop 80 80

Otherwise, you can change lines 7-9 to be something like:

<?php if ($i == 0): ?>
    <article> 6th Element </article>
<?php endif; ?>
<article>
    Element
</article>

That will inject the 6th element in at the beginning, before we increment $i.

commented: Dani, that worked pertect! Gracias! +0
Re: Adding extra element with modulus in a PHP loop 80 80

Glad it worked. I'm going to mark this question solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.