How to skip duplicating DIV in mysql when my div support only 2 value?

Hello Guys,
I am creating a new website. For similar category all news will be there. I create a DIV for showing those news. My div support 2 value at a time. One is for left side and another is for right side. So if i have 10 news then i need 5 DIV but when i am going to echo them then PHP creating 10 DIV so my news is 20 now. I want to stop this.Image is below

My code is:

 <!--========================================================-->
<?php
  $VCateNews = "SELECT * FROM allnewsdetails WHERE ncaTegory='$getCategory'     ORDER BY publishDate DESC";
  $QryCNews = @mysql_query($VCateNews);
  
  while($getCateNews = mysql_fetch_array($QryCNews)){
?>
<div class="posts_wrapper">
  <article>
    <div class="pic"><a href="post-standart.html" class="w_hover img-link img-wrap"><img src="<?php base('none');?>images_post/2-600x346.jpg" alt="" ><span class="link-icon"></span></a></div>
    <div class="field_group">
      <h3><a href="post-standart.html"><?php echo $getCateNews['id'];?> Dolorem ipsum quia dolor sits...</a></h3>
      <div class="text"> aperiam, eaque ipsa q]et quasi architecto. Beatae vitae dicta sunt....</div>
      <ul class="icons">
        <li><a href="post-standart.html" class="post_date">May 28, 2013</a></li>
        <li><a href="post-standart.html" class="post_views">1571</a></li>
        <li><a href="post-standart.html" class="comments_count">1</a></li>
      </ul>
    </div>
  </article>
  <article>
    <div class="pic"><a href="post-standart.html" class="w_hover img-link img-wrap"><img src="images_post/4-600x346.jpg" alt="" /><span class="link-icon"></span></a></div>
    <div class="field_group">
      <h3><a href="post-standart.html"><?php echo $getCateNews['id'];?> Beatae vitae dicta...</a></h3>
      <div class="text"> veritatis et quasi architecto. Beatae vitae dicta sunt....</div>
      <ul class="icons">
        <li><a href="post-standart.html" class="post_date">May 28, 2013</a></li>
        <li><a href="post-standart.html" class="post_views">1026</a></li>
        <li><a href="post-standart.html" class="comments_count">2</a></li>
      </ul>
    </div>
  </article>
</div>
  <?php } ?>
  <!--========================================================-->

Will you please help me for this.
Thanks in Advance.
Thank you.

Standard Disclaimer: mysql_ library is going away in PHP7. Switch to mysqli_ or PDO.

so what i would do in your case is:

Initialize a counter to 0.

Move your while down to after the opening wrapper div.

Stop the while after the first </article>.

Just before you stop the while:

  1. Increment the counter.
  2. do an if check with the condition of "The modulo of the counter and 2 is 0 AND counter is NOT equal to the number of rows returned by the query.
  3. If the check was passed, close the wrapper div and open a new one.

After the while loop is closed, close the wrapper div.

Hello @StarLion
If i will start put my while after wrapper and end it after </article> then only first row is going well then from the second row only left side is visible but there is no content at right side. check image for more clear concept.

Show me your new code. It’s actually more understandable for me to read your code than to look at a picture.

Hello @StarLion
This is here :

  <?php
        $VCateNews = "SELECT * FROM allnewsdetails WHERE nCoverNews='bannernews' AND publishOrSave='publish' AND nimage!='' ORDER BY publishDate DESC LIMIT 4";
        $QryCNews = @mysql_query($VCateNews);
    
        
      ?>
      <div class="posts_wrapper">
      <?php 
        while($getCateNews = mysql_fetch_array($QryCNews)){
      ?>
        <article>
          <div class="pic"><a href="post-standart.html" class="w_hover img-link img-wrap"><img src="<?php base('none');?>images_post/2-600x346.jpg" alt="" ><span class="link-icon"></span></a></div>
          <div class="field_group">
            <h3><a href="post-standart.html"><?php echo $getCateNews['id'];?> Dolorem ipsum quia dolor sits...</a></h3>
            <div class="text">aPerspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto. Beatae vitae dicta sunt....</div>
            <ul class="icons">
              <li><a href="post-standart.html" class="post_date">May 28, 2013</a></li>
              <li><a href="post-standart.html" class="post_views">1571</a></li>
              <li><a href="post-standart.html" class="comments_count">1</a></li>
            </ul>
          </div>
        </article>
      <?php } ?>
      </div>

So, you skipped this section of my post.

will you please write the code for me ? I am unable to understand.

What part are you unable to understand?

this Part

I believe this is what StarLion was suggesting.
The only difference is I set the $cnt to 1 instead of 0.

Notice after </article> I check that the “remainder” of cnt%2 is 0 AND cnt is less than the number of rows so we don’t add an extra div when we have an even records returned,e.g. 4,6,8,10 etc. If these conditions are met, we close the div and start a new one. We then increment the cnt with ++ just inside the closing bracket.

<?php    

        $VCateNews = "SELECT * FROM allnewsdetails WHERE nCoverNews='bannernews' AND publishOrSave='publish' AND nimage!='' ORDER BY publishDate DESC LIMIT 4";
        $QryCNews = @mysql_query($VCateNews);
        $num_rows = mysql_num_rows($QryCNews);
      ?>
      <div class="posts_wrapper">
      <?php
          $cnt = 1; 
        while($getCateNews = mysql_fetch_array($QryCNews)){
      ?>
        <article>
          <div class="pic"><a href="post-standart.html" class="w_hover img-link img-wrap"><img src="<?php base('none');?>images_post/2-600x346.jpg" alt="" ><span class="link-icon"></span></a></div>
          <div class="field_group">
            <h3><a href="post-standart.html"><?php echo $getCateNews['id'];?> Dolorem ipsum quia dolor sits...</a></h3>
            <div class="text">aPerspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto. Beatae vitae dicta sunt....</div>
            <ul class="icons">
              <li><a href="post-standart.html" class="post_date">May 28, 2013</a></li>
              <li><a href="post-standart.html" class="post_views">1571</a></li>
              <li><a href="post-standart.html" class="comments_count">1</a></li>
            </ul>
          </div>
        </article>
      <?php
      if($cnt%2 == 0 && $cnt < $num_rows){
      ?>
      </div>
      <div class="posts_wrapper">      
      <?php
      }
      $cnt++; 
      } 
      ?>
      </div>

Hello @Drummin Thank you so much. Yea what i wanted. Its totally working perfectly. Thank you so much.

Thank you @StarLion :slight_smile:

I got my ans :slight_smile:

Yeah, the “start at 1” vs start at 0 is all depending on when you do the increment. If you increment before the if, start at 0. If you increment after, start at 1. The idea just being “When you get to the IF, the counter should equal the current number of the record you’re looking at.”

1 Like

Yes, yes. Good explanation for those not understanding what’s going on.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.