Forum Moderators: coopster

Message Too Old, No Replies

While Within Foreach

Only show the first x records that foreach gives

         

doubleJ

11:30 pm on Nov 8, 2011 (gmt 0)

10+ Year Member



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

11:49 pm on Nov 8, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



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

1:11 am on Nov 9, 2011 (gmt 0)

10+ Year Member



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