homepage Welcome to WebmasterWorld Guest from 54.161.185.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
While Within Foreach
Only show the first x records that foreach gives
doubleJ



 
Msg#: 4385085 posted 11:30 pm on Nov 8, 2011 (gmt 0)

Hello...
I'm trying to combine two chunks of code that I've worked on.
I'm using foreach to get contents of an xml file. Only it displays all of the records.

foreach($data->channel->item as $items) {
echo $items->title;
}

I'm using this code to show only the first 15 records from a different flat-text database.

$maxCount = 15;
$count = 0;
while ($count<$maxCount) {
$count++;
echo "some text";
}

My joining of the two loops has caused every result except the one that I want.
Hehehe...
This is the closest thing that I've found, but it displays the first record 5 times.

$maxCount = 15;
$count = 0;
foreach($data->channel->item as $items) {
while ($count<$maxCount) {
$count++;
echo $items->title;
}
}

I've looked at pagination, but I'm not really wanting to have all the records navigatable, just the first x number.
I thought a do/while loop would work, but I'm working with an array. My understanding is that foreach is what you want for that.
JJ

 

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4385085 posted 11:49 pm on Nov 8, 2011 (gmt 0)

the two loops are making the confusion I would think

$maxCount = 15;
$count = 0;
foreach($data->channel->item as $items) {
if ($count < $maxCount) {
echo $items->title;
$count++;
} else {
break;
}
}

doubleJ



 
Msg#: 4385085 posted 1:11 am on Nov 9, 2011 (gmt 0)

Bah...
if, not while.
Thanks for the input.
JJ

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved