Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Restrict results in foreach loop

Posted on 2011-03-15
1
Medium Priority
?
287 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

609 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