homepage Welcome to WebmasterWorld Guest from 54.205.173.252
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Visit PubCon.com
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:4385087
 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




msg:4385095
 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:4385114
 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved