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.
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.
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
}
Btw, $n = 0; $n <= 5; $n++
$n = 0; $n <= 5; $n++
$n = 0; $n <= 5; $n++
will limit to the first 6 entries, not 5.
$n = 0; $n < 5; $n++
$n = 0; $n < 5; $n++
will do what you asked for.
Don't you mean $n = 0; $n < 4; $n++
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.