Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i limit the results of a foreach loop? 1

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB
i have a basic foreach loop. its all thats been given to me by the developer as all the other parent code is in another locked away script. here the loop...

Code:
foreach ($item as $items) {


}

the loop is returning results from a database for example the array contains the following...

$headline
$date
$copy
$image

and will continue looping through


the array of results are sorted by most recent articles first, but what i need to do is limit the loop to return the first 7 results.

how would i incorporate this into the existing foreach loop?
 
Setting aside the fact that doing this from the query itself would be the easiest and more correct way to do this, you can add a counter, and when it reaches seven, exit the loop.


Code:
$counter=0;[green]\\Initialize counter[/green]
foreach ($item as $items) {
...[green]\\do whatever you do in the loop[/green]

counter++;[green]\\Increment counter[/green]
if($counter>=7){
break; [green]\\end foreach loop here[/green]
}

}

Again I reiterate it would be better do this from the query, its not good to query larger amounts of data you are not going to use afterwards.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
As Vacunita says, rewrite your query to only return what you need.

In MySQL you might do this

Code:
SELECT mt.something,mt.somethingelse,mt.etc FROM mytable mt ORDER BY mt.something DESC LIMIT 7

This will retrieve the data in the columns something, somethingelse and etc. It will put the results in descending order according to the contents of something and will only retrieve the top 7 rows of that resultset.


--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Was there a reason you can't use a proper SQL select instead of retrieving stuff you don't need?

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
some sql backends do not support limit clauses.
 
yes, the developer locked down all his code where the queries reside. the queries were also being shared by xml linked to a flash app, so he didnt want me to modify the query.
 
some sql backends do not support limit clauses.

Granted, I was using MySQL as an example.
Even if they don't support LIMIT clauses would they not support some kind of similar command?


yes, the developer locked down all his code where the queries reside. the queries were also being shared by xml linked to a flash app, so he didnt want me to modify the query.

So it's a different query.
I don't know how much data you are dealing with, but it seems wasteful of resources.
Why not simply write another query based on the old one but limiting the resultset to your requirements?
Alternately, parameterise the query so you can pass a value to it if you need a limit.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
foamcow - i've not tested all the dbs around but i am pretty sure that some big names don't have limit clauses or equivalents. Oracle for example.
 
I'll bow to your knowledge then.
I believe Oracle uses ROWNUM as a means of limiting the number of rows returned by a query.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
no knowledge of oracle at all, me! my info comes from reading the notes on database abstraction layers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top