Recently I am using php to deal with a xml file.
But I don’t know how to add a ‘break’ in my code.
The link is : http://babystory.diandian.com/rss
I want to use php to get the article named ‘[x]快乐王子’.But “foreach()” give me the last article named ‘[1]狼和七只小山羊’.
I want to add a break in “foreach()” so that I can get the article I want.
Here is my php code:
<?php
$xml = simplexml_load_file("http://babystory.diandian.com/rss");
foreach($xml->children() as $child){}
foreach($child->children() as $child2)
foreach($child2->children() as $child3)
{
$element = $child3->getName();
if($element=='title')
{
echo "<h1>" . $child3 . "</h1>";
}
if($element=='description')
{
echo $child3;
}
}
?>
Could you help me to edit my code.Thank you very much.
If you need to get the first item from your feed you don’t need any foreach loops at all.
Now
$itemis array of the first article information. So$item['title']is [x]快乐王子,$item['link']is http://babystory.diandian.com/post/2012-01-15/14770976, etc.