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++; } ?> Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/ 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) Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619200 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 Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619212 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. Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619222 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. Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619232 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? Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619237 Share on other sites More sharing options...
mark110384 Posted August 18, 2008 Author Share Posted August 18, 2008 Sure is, quite puzzling ??? Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619242 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. Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619248 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++; } ?> Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619876 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??? Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619883 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619887 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++; } ?> Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619889 Share on other sites More sharing options...
Andy-H Posted August 19, 2008 Share Posted August 19, 2008 gdgd, glad I could help Link to comment https://forums.phpfreaks.com/topic/120198-solved-while-condition-problem/#findComment-619895 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.