?
Solved

Restrict results in foreach loop

Posted on 2011-03-15
1
Medium Priority
?
282 Views
Last Modified: 2012-05-11
Hi all,

I have this code:

foreach ($val as $key=>$fval) {
            
        echo 'show data here';
        echo etc...
}

Now this code is showing all the results ...

How can I get it to show only 5 or 6 etc?

Basically I want to control how many results are displayed.

Can someone help please?

Thanks


0
Comment
Question by:error77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
enachemc earned 2000 total points
ID: 35136423

$i  = 0;
foreach ($val as $key=>$fval) {
           
        echo 'show data here';
        echo etc...
       
$i+=1;
if($i > 6)break;
}
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question