Need help limiting a loop....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tech0925
Forum Commoner
Posts: 47
Joined: Wed Nov 09, 2011 2:46 pm

Need help limiting a loop....

Post 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!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help limiting a loop....

Post by Celauran »

Adding a limit clause to the query is the way to do it.
tech0925
Forum Commoner
Posts: 47
Joined: Wed Nov 09, 2011 2:46 pm

Re: Need help limiting a loop....

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help limiting a loop....

Post by Celauran »

Code: Select all

var_dump($pages->limit);
tech0925
Forum Commoner
Posts: 47
Joined: Wed Nov 09, 2011 2:46 pm

Re: Need help limiting a loop....

Post by tech0925 »

Thanks, but I am not sure I understand. Still learning this stuff lol Can you show me what you mean? Thanks again!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need help limiting a loop....

Post by Celauran »

What is the output of that code? Without knowing what $pages->limit is, we won't be able to help you further.
Post Reply