Reputation: 5
what's wrong here? I want load a list of 10 items, but they are the same. Why?
<?php
$xml = simplexml_load_file('test.xml');
$name = $xml->item->title;
foreach($xml -> item as $item){
echo "$name<br>";
}
?>
Upvotes: 0
Views: 52
Reputation: 41885
No, you're not accessing the values inside the loop:
$name = $xml->item->title; // NOT THIS!
foreach($xml->item as $item){
// access `$item` inside this loop
echo $item->title . '<br/>';
// echo "$name<br/>"; // you're accessing that item outside the loop
}
Additional question:
Just trim the title with the numbering:
$i = 1;
$xml = simplexml_load_file('http://store.steampowered.com/feeds/weeklytopsellers.xml', null, LIBXML_NOCDATA);
foreach($xml->item as $item){
// $trimmed_title = ltrim($item->title, "#$i - ");
$trimmed_title = str_replace("#$i - ", '', $item->title);
echo $trimmed_title. '<br/>';
$i++;
}
Upvotes: 2