Page 1 of 1
Need help limiting a loop....
Posted: Mon Jan 23, 2012 10:44 am
by tech0925
Here is the code:
Code: Select all
if ($num_results == 0) {
echo "<p>No articles found!</p>";
} else {
for ($i=0; $i < $num_results; $i++) {
$row = mysql_fetch_assoc($articleresults);
Lets say I want it to only loop through 10 results, what should I change here? I tried limited the sql query first but I am using a pagination script and I just get errors so I figured the answer could be in the code above.
Thanks!
Re: Need help limiting a loop....
Posted: Mon Jan 23, 2012 10:53 am
by Celauran
Adding a limit clause to the query is the way to do it.
Re: Need help limiting a loop....
Posted: Mon Jan 23, 2012 11:08 am
by tech0925
Ok thanks! Here is the query:
Code: Select all
// Setup pagination controls
$rowsquery = "select * from articles where status=0";
$rowsresults = mysql_query($rowsquery,$connection) or die(mysql_error());
$rows_results = mysql_num_rows($rowsresults);
$pages = new Paginator;
if ($seourls == 1) {
$pages->nav = $siteurl;
} else {
$pages->urlparam = "?";
}
$pages->items_total = $rows_results;
$pages->mid_range = 9;
$pages->paginate();
if ($pages->items_total) {
$query = "select * from articles where status=0 order by date desc".$pages->limit;
} else {
$query = "select * from articles where status=0 order by date desc";
}
$articleresults = mysql_query($query,$connection) or die(mysql_error());
$num_results = mysql_num_rows($articleresults);
Here is what I tried one at a time:
Code: Select all
$query = "select * from articles where status=0 order by date desc [color=#800000][b]Limit 8[/b][/color]".$pages->limit; <--- Gives php error
} else {
$query = "select * from articles where status=0 order by date desc [color=#800000][b]Limit 8[/b][/color]"; <----Pagination works but it doesn't pull the data
Re: Need help limiting a loop....
Posted: Mon Jan 23, 2012 11:24 am
by Celauran
Re: Need help limiting a loop....
Posted: Mon Jan 23, 2012 11:26 am
by tech0925
Thanks, but I am not sure I understand. Still learning this stuff lol Can you show me what you mean? Thanks again!
Re: Need help limiting a loop....
Posted: Mon Jan 23, 2012 11:35 am
by Celauran
What is the output of that code? Without knowing what $pages->limit is, we won't be able to help you further.