SimpleXML 获取下一个/上一个节点

发布于 2024-09-28 15:16:50 字数 297 浏览 1 评论 0原文

我正在构建一个照片库,基于 xml 文件构建一个对象。

如何获取下一个和上一个节点?这是我的基本代码:

$xmlData = new SimpleXMLElement(file_get_contents("data.xml"));
foreach($xmlData->row as $item) {
    if ($item->url == $_GET['id']) {
        // show photo
        $title = $item->title;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-10-05 15:16:50

仅当下一个/上一个节点属于相同类型时才可用。如果您想要更复杂的处理,请使用 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++;
}

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++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文