Jump to content

Previous Record in an Array


millsy007

Recommended Posts

I have some code that Creates 'PREVIOUS' and 'NEXT' links for my page depending on the positions within a sitemap. The code works by reading the sitemap into an array and then setting the links to the previous and next records from the sitemap:

 

<?php
$domain = $_SERVER['HTTP_HOST'];
// find out the path to the current file:
$path = $_SERVER['SCRIPT_NAME'];
// put it all together:
$currenturl = "http://" . $domain . $path;             
include '/home/beachh/public_html/sample.php';

$xml = new SimpleXMLElement($xmlstr);

if(list($next) = $xml->xpath("/urlset/url[loc='$currenturl']/following-sibling::url")){
echo '<li class="next">
<a href="'.(string)$next->loc.'">NEXT</a>
</li>';
}

if(list($prev) = $xml->xpath("/urlset/url[loc='$currenturl']/preceding-sibling::url")){
echo '<li class="previous">
<a href="'.(string)$prev->loc.'">PREV</a></li>';
}
?>  

 

The 'NEXT' link is being assigned the correct value, but the 'PREV' is always linking to the homepage?

Any ideas as to why?

Link to comment
https://forums.phpfreaks.com/topic/137015-previous-record-in-an-array/
Share on other sites

I think I may be assuming (incorrrectly) that "preceding-sibling" and "following-sibling" work in the same way.  The "following-sibling" is getting out the next record.  However I read that "preceding-sibling" will select all the sibling elements before the context node. Wheras I would only want the immiedately preceding/previous record, how could I do this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.