views:

164

answers:

5

I'm looking to limit to the first 5 results returned here.

This works, but it does not limit the data set:

<?php

foreach($sxml->status as $status){
$name = $status->user->name;
$image =$status->user->profile_image_url;
$update =$status->text;
$url = "http://twitter.com/" .$status->user->screen_name;

echo "<li><a href=\"" . $url . "\"><img src=\"" . $image . "\" alt=\"" . $name . " image\" />" . $name . " </a> " . $update . "</li>";

}

?>

I've tried this:

<?php

for($n = 0; $n <= 5; $n++){
$name = $sxml->$status[$n]->user->name;
$image = $sxml->$status[$n]->user->profile_image_url;
$update = $sxml->$status[$n]->text;
$url = "http://twitter.com/" . $sxml->$status[$n]->user->screen_name;

echo "<li><a href=\"" . $url . "\"><img src=\"" . $image . "\" alt=\"" . $name . " image\" />" . $name . " </a> " . $update . "</li>";

}

?>

and am really kind of unsure why it doesn't work. If I simply do:

<?php echo $sxml->status[0]->user->name ?>

then I get the proper result. But when attempting it within the for loop, I get NULL.

Perhaps some kind of while? A different setup altogether? Thanks so much for any help you can give on this.

+2  A: 

Change this:

for($n = 0; $n <= 5; $n++){
$name = $sxml->$status[$n]->user->name;
$image = $sxml->$status[$n]->user->profile_image_url;
$update = $sxml->$status[$n]->text;
$url = "http://twitter.com/" . $sxml->$status[$n]->user->screen_name;

echo "<li><a href=\"" . $url . "\"><img src=\"" . $image . "\" alt=\"" . $name . " image\" />" . $name . " </a> " . $update . "</li>";

}

To this:

for($n = 0; $n <= 5; $n++){
$name = $sxml->status[$n]->user->name;
$image = $sxml->status[$n]->user->profile_image_url;
$update = $sxml->status[$n]->text;
$url = "http://twitter.com/" . $sxml->status[$n]->user->screen_name;

echo "<li><a href=\"" . $url . "\"><img src=\"" . $image . "\" alt=\"" . $name . " image\" />" . $name . " </a> " . $update . "</li>";

}

You accidentally were writing this:

<?php echo $sxml->$status[0]->user->name ?>

Where it was trying to us $status[0] as a variable variable and of course, that doesn't exist and is thus undefined/null.

Chacha102
Doggonit, this is the week of silly errors. Thanks so much for the help :)
Joshua Cody
+2  A: 

If you had something that works, why overcomplicate things by changing everything? Just limit the processing to the first N entries.

$i = 0;
foreach ($sxml->status as $status) {
    if (++$i > 5) {
        // stop after 5 loops
        break;
    }
    // the rest is identical
}
Josh Davis
Man, great solutions from everyone. I appreciate this **second** method of getting it done. All this stuff makes so much sense once I see it, but the solutions don't come to me immediately. I guess that will come with time, though. Thanks, Josh, great idea here.
Joshua Cody
+1  A: 

Btw, $n = 0; $n <= 5; $n++ will limit to the first 6 entries, not 5.

$n = 0; $n < 5; $n++ will do what you asked for.

Karsten
Realized that once I implemented it; thanks so much for the reminder here.
Joshua Cody
A: 

Don't you mean $n = 0; $n < 4; $n++

emcgfx
A: 

I've also tried this, and it works great :-)

foreach ($xml->item as $item) { if (++$i > 5) { break; } $item->title . '
'; } //foreach()

Note I'm not using $i = 0; it seems to know that by default ;-)

I hope this helps some one.

emcgfx