mark110384 Posted August 18, 2008 Share Posted August 18, 2008 I have a table that displays a news archive, however I wan't to display 6 news links at a time I have the code working perfectly to pop out the links the first time ($i <= 6) around but i'm not too sure how to the condition of links 7 and 12. The following code is what I have at the moment. Any suggestions would be appreciated, thanks <?php $i = 1; $sql = "SELECT * FROM other_news ORDER BY news_date desc"; $result = mysql_query($sql) or die ("Data not found"); while ($i <= 6) { $i++; } #This is where I am having difficulties with! while ($i >=7) { $myrow = mysql_fetch_array($result); $date = $myrow['news_date']; $title = $myrow['news_title']; $url = $myrow['news_url']; $formatdate = $date; $dateformated=date('d/m/Y',strtotime($formatdate)); ?> <table width="92%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td> <td width="74%"><font face="Lucida sans" size="1" color="#666666"> <? echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000"> <? echo "<a class='other_news' href=$url>$title </a></br>"; ?> </font></td> </tr> <? $i++; } ?> Quote Link to comment Share on other sites More sharing options...
Mchl Posted August 18, 2008 Share Posted August 18, 2008 You can do (at least) two things: Either set $i = 1; before second while loop, and use same condition. Or use while ($i <=12) Quote Link to comment Share on other sites More sharing options...
mark110384 Posted August 18, 2008 Author Share Posted August 18, 2008 If I use $i <= 12; then it will return all 12 results Quote Link to comment Share on other sites More sharing options...
Mchl Posted August 18, 2008 Share Posted August 18, 2008 At this point $i is equal to 7, so it will return results 7-12. Quote Link to comment Share on other sites More sharing options...
mark110384 Posted August 18, 2008 Author Share Posted August 18, 2008 Thats the logic that I went for but it just displays the previous 6 results. Quote Link to comment Share on other sites More sharing options...
Mchl Posted August 18, 2008 Share Posted August 18, 2008 That shouldn't happen. Is what you posted above, entire the code you have for this? Quote Link to comment Share on other sites More sharing options...
mark110384 Posted August 18, 2008 Author Share Posted August 18, 2008 Sure is, quite puzzling ??? Quote Link to comment Share on other sites More sharing options...
Mchl Posted August 18, 2008 Share Posted August 18, 2008 It isn't You have no code responsible for echoing results in the first while loop. Quote Link to comment Share on other sites More sharing options...
mark110384 Posted August 19, 2008 Author Share Posted August 19, 2008 I've come up with this but the same result just gets pumped out $i = 1; $sql = "SELECT * FROM other_news ORDER BY news_date desc"; $result = mysql_query($sql) or die ("Data not found"); while ($i <= 12) { if ($i >=7) { $myrow = mysql_fetch_array($result); $date = $myrow['news_date']; $title = $myrow['news_title']; $url = $myrow['news_url']; $formatdate = $date; $dateformated=date('d/m/Y',strtotime($formatdate)); ?> <table width="92%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td> <td width="74%"><font face="Lucida sans" size="1" color="#666666"> <? echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000"> <? echo "<a class='other_news' href=$url>$title </a></br>"; ?> </font></td> </tr> <? } $i++; } ?> Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Share Posted August 19, 2008 <?php $i = 1; $sql = "SELECT * FROM other_news ORDER BY news_date desc"; $result = mysql_query($sql) or die ("Data not found"); while ($myrow = mysql_fetch_array($result) && $i <= 12) { if ($i >=7) { $date = $myrow['news_date']; $title = $myrow['news_title']; $url = $myrow['news_url']; $formatdate = $date; $dateformated=date('d/m/Y',strtotime($formatdate)); ?> <table width="92%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td> <td width="74%"><font face="Lucida sans" size="1" color="#666666"> <?php echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000"> <?php echo "<a class='other_news' href=$url>$title </a></br>"; ?> </font></td> </tr> <?php } $i++; } ?> That work??? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Share Posted August 19, 2008 Infact, do you use previous and next links? just add <a href="yoursite.com/script.php?limit1=1&limit2=6">Next</a> Like: <?php if (empty($_GET['limit1'])){ $limit1 = 1; } if (empty($_GET['limit2'])){ $limit2 = 6; } $sql = "SELECT * FROM other_news ORDER BY news_date desc LIMIT $limit1 , $limit2"; $result = mysql_query($sql) or die ("Data not found"); while ($myrow = mysql_fetch_array($result)) { $date = $myrow['news_date']; $title = $myrow['news_title']; $url = $myrow['news_url']; $formatdate = $date; $dateformated=date('d/m/Y',strtotime($formatdate)); ?> <table width="92%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td> <td width="74%"><font face="Lucida sans" size="1" color="#666666"> <?php echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000"> <?php echo "<a class='other_news' href=$url>$title </a></br>"; ?> </font></td> </tr> <?php } echo "<a href=\"yoursite.com/page.php?limit1=".$limit1+6."&limit2=".$limit2+6."\">Next</a>"; ?> Quote Link to comment Share on other sites More sharing options...
mark110384 Posted August 19, 2008 Author Share Posted August 19, 2008 Sweet thanks Andy, using your idea I made a couple of adjustments and I got the desired result that was looking for, heres the end coding that works! $i = 1; $sql = "SELECT * FROM other_news ORDER BY news_date desc"; $result = mysql_query($sql) or die ("Data not found"); while ($myrow = mysql_fetch_array($result) && $i <= 12) { if ($i >=7) { while($myrow = mysql_fetch_array($result)) { $date = $myrow['news_date']; $title = $myrow['news_title']; $url = $myrow['news_url']; $formatdate = $date; $dateformated=date('d/m/Y',strtotime($formatdate)); ?> <table width="92%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="26%" height="93"><img src="../news_image.bmp" width = "50%"/></td> <td width="74%"><font face="Lucida sans" size="1" color="#666666"> <?php echo $dateformated; ?></font> - <font face="Lucida sans" size="1" color="#000000"> <?php echo "<a class='other_news' href=$url>$title </a></br>"; ?> </font></td> </tr> <?php } } $i++; } ?> Quote Link to comment Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Share Posted August 19, 2008 gdgd, glad I could help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.