views:

13

answers:

1

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']) {
        // show photo
        $title = $item->title;
    }
}
A: 

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++;
}
Alin Purcaru
Alin, thanks for the edits to my post for clarity; you're awesome! This worked great.
RedLabor