-2

I'm trying to add a container every 2 iterations. I've implemented a count, but I'm getting weird results as I inspect the page.

Any ideas what may be going on and what I may be doing wrong with the wrapper injection?

<?php
include "includes2014_new/db_conx.php";
$sql ="SELECT * FROM articles WHERE id ORDER BY id DESC LIMIT 5";
$resultsHome = mysqli_query($db_conx,$sql);
$productCount = mysqli_num_rows($resultsHome); // count the output amount
if ($productCount > 0) {

$counter = 1;

while($row = mysqli_fetch_array($resultsHome)){
$id = $row["id"];
$article_title = $row["article_title"];
$category = $row["category"];
$readmore = $row["readmore"];
$author = $row["author"];
$date_added = $row["date_added"];
$content = $row["content"];
$short = substr(strip_tags($content), 0, 80);

     if (($counter % 2) === 1) {
         $blogList .= '<div class="clearer">';
         }
         //----------------
            if($readmore == ''){
                    $blogList .= '<div class="col span_6_of_12 postBox textLeft fulltwelvepadfix">
                    <div class="section miniPad">
                    <div class="col span_8_of_12">'.$article_title.'</div>
                    <div class="col span_4_of_12"><img src="images/'.$id.'.jpg" height="80px" width="100px" alt="'.$category.' " /></div>
                    </div>
                    <div class="section miniPad">
                    <div class="col span_12_of_12">'.$date_added.' - '.$author.'</div>
                    <div class="col span_12_of_12"><p>'.$short.'[...]</p></div>
                    <div class="section group"><a href="r.php?id='.$id.'">Read More</a></div>
                    </div>
                    <div class="comments section group">
                    <div class="commentsInner">Comments(0)</div>
                    </div>
                    </div>';
                }

            else {
                $blogList .= '<div class="col span_6_of_12 postBox textLeft fulltwelvepadfix">
            <div class="section miniPad">
            <div class="col span_8_of_12">'.$article_title.'</div>
            <div class="col span_4_of_12"><img src="images/'.$id.'.jpg" height="80px" width="100px" alt="'.$category.' " /></div>
            </div>
            <div class="section miniPad">
            <div class="col span_12_of_12">'.$date_added.' - '.$author.'</div>
            <div class="col span_12_of_12"><p>'.$short.'[...]</p></div>
            <div class="section group"><a href="'.$readmore.'">Read More</a></div>
            </div>
            <div class="comments section group">
            <div class="commentsInner">Comments(0)</div>
            </div>
            </div>';
            }
        //----------------
    if (($counter % 2) === 0) { $blogList .= '</div>'; }
    $counter++;
    }
}
?>
user3135730
  • 320
  • 1
  • 3
  • 8
  • You’re checking for `$counter % 2 != 0` multiple times inside your loop, but you are incrementing the counter in between those checks … I doubt that’s really what you want? Increment it at the end of the loop. – CBroe Jun 09 '14 at 01:02
  • Hi @CBroe I'm new to this method. Can you explain what you mean. How would I insert the closing div if I don't check it twice? – user3135730 Jun 09 '14 at 01:06
  • I’m not saying you should not _check_ it twice, I am saying you should not increment the counter _in between_ those two checks. – CBroe Jun 09 '14 at 01:07
  • @CBroe I've updated my question to clarify a little about what I'm trying to do. Let me know if that clears things up. – user3135730 Jun 09 '14 at 01:17
  • @CBroe I think I understand what you meant. You're referring to placing the counter within the `if` statement and else statement? As opposed to just within the actual while loop so taht it only triggers if the if or else is met? – user3135730 Jun 09 '14 at 01:22
  • First of all, you should _indent_ your code properly. Right now it makes little sense – your `else{ //Code for old post` _is not_ an else to the `if($readmore == ''){ //Code for new post`, but only to the `if ($counter % 2 == 0)` right above it … in _properly_ indented code you should have been able to spot that yourself. – CBroe Jun 09 '14 at 01:29
  • And after fixing that logical error, you’ll need to adapt the logic of outputting the opening/closing container div tags as well. It makes most sense IMHO, to output the _closing_ tag first, if counter modulo 2 equals zero _and_ it is not the first loop iteration, and then directly after that output the _opening_ tag for the following container div (if counter modulo 2 equals zero). And after your loop, you simply output a final closing tag. – CBroe Jun 09 '14 at 01:35
  • @CBroe I really appreciate the help. I've posted an updated version. Has my updated version solved that issue? – user3135730 Jun 09 '14 at 02:03

1 Answers1

0

Try to change

SELECT * FROM articles WHERE id ORDER BY id DESC LIMIT 5

to

SELECT * FROM articles WHERE `id`= '$id' ORDER BY id DESC LIMIT 5