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