Finding Previous Array Record

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Finding Previous Array Record

Post by millsy007 »

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 within the sitemap:

Code: Select all

      <?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?
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Finding Previous Array Record

Post by millsy007 »

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?
cavemaneca
Forum Commoner
Posts: 59
Joined: Sat Dec 13, 2008 2:16 am

Re: Finding Previous Array Record

Post by cavemaneca »

I would have set it so that it stores all in an array, then accesses it like this

Code: Select all

$page = $array[$current]
$previous = $array[$current - 1]
$next = $array[$current + 1]
Then have code that checks to make sure that is valid, so that if previous = -1 it returns no link or the last one, and that if next = more than the number of pages, there is no link, or it goes to the first page. Is this what you need?
Post Reply