Welcome to WebmasterWorld Guest from 54.161.147.106

Forum Moderators: coopster & jatar k

While Within Foreach

Only show the first x records that foreach gives

   
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
11:49 pm on Nov 8, 2011 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 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;
}
}
1:11 am on Nov 9, 2011 (gmt 0)



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

Featured Threads

Hot Threads This Week

Hot Threads This Month