SimpleXML get next/prev node
I'm building a photo gallery, building an object based on an xml file.
How can I grab the next and previous nodes? Here's what my base code looks like:
$xmlData = new SimpleXMLElement(file_get_contents("data.xml"));
foreach($xmlData->row as $item) {
if ($item->url == $_GET['id']) {开发者_如何学JAVA
// show photo
$title = $item->title;
}
}
Only usable if the next/prev nodes are of the same type. If you want more complex processing use DOM.
$xmlData = new SimpleXMLElement(file_get_contents("data.xml"));
$index = 0;
foreach($xmlData->row as $item) {
if ($item->url == $_GET['id']) {
// show photo
$title = $item->title;
$prev = $xmlData->row[$index-1];
$next = $xmlData->row[$index+1];
}
$index++;
}
精彩评论