millsy007 Posted December 9, 2008 Share Posted December 9, 2008 Hello I have some xml (a sitemap). I pass this into my php code and use the current server url to find the relevant record within the sitemap. I then want to use the array made from the sitemap to find what the next record in the sitemap is and use this for a next link on my page. So I would have: <?php $domain = $_SERVER['HTTP_HOST']; $path = $_SERVER['SCRIPT_NAME']; $currenturl = "http://" . $domain . $path; include 'sitemap.php'; $xml = new SimpleXMLElement($xmlstr); foreach ($xml->url as $url) { if ((string) $url->loc == $currenturl) { //TO DO: Go to the next record in the sitemap/array , get this url and set it to = &nexturl echo '<div class="navigation-div"><ul class="navigation"><li class="next"> <a href="', &nexturl ,'">NEXT</a></li> </li></ul></div>'; } } ?> <?php $xmlstr = <<<XML <?xml version='1.0' standalone='yes'?> <urlset> <url> <loc>http://www.page.nl/home.php</loc> <priority>1.00</priority> <changefreq>daily</changefreq> </url> <url> <loc>http://www.page.nl/about.php</loc> <priority>0.80</priority> <changefreq>daily</changefreq> </url> </urlset> XML; ?> So for example if the current page was http://www.page.nl/home.php I would want the NEXT link to bet set to http://www.page.nl/about.php Thanks ??? Link to comment https://forums.phpfreaks.com/topic/136204-moving-around-an-array/ Share on other sites More sharing options...
rhodesa Posted December 9, 2008 Share Posted December 9, 2008 $matched = false; foreach ($xml->url as $url) { if($matched){ echo '<div class="navigation-div"><ul class="navigation"><li class="next"> <a href="', &nexturl ,'">NEXT</a></li> </li></ul></div>'; break; } if ((string) $url->loc == $currenturl) { $matched = true; continue; } } Link to comment https://forums.phpfreaks.com/topic/136204-moving-around-an-array/#findComment-710469 Share on other sites More sharing options...
rhodesa Posted December 9, 2008 Share Posted December 9, 2008 that should be $url instead of &nexturl ...also, this is easier and probably faster: $xml = new SimpleXMLElement($xmlstr); $currenturl = 'http://www.page.nl/home.php'; if(list($next) = $xml->xpath("/urlset/url[loc='$currenturl']/following-sibling::url")){ echo '<div class="navigation-div"><ul class="navigation"><li class="next"> <a href="'.(string)$next->loc.'">NEXT</a></li> </li></ul></div>'; } Link to comment https://forums.phpfreaks.com/topic/136204-moving-around-an-array/#findComment-710479 Share on other sites More sharing options...
millsy007 Posted December 9, 2008 Author Share Posted December 9, 2008 Your a star! Got it to work for the previous too! Cheers Link to comment https://forums.phpfreaks.com/topic/136204-moving-around-an-array/#findComment-710535 Share on other sites More sharing options...
millsy007 Posted December 14, 2008 Author Share Posted December 14, 2008 Turns out the previous code I tried didnt do what I thought, it is setting the link to the homepage all the time. I want the link to be the previous url of the current page: <?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' works, but the 'PREV' doesnt? Link to comment https://forums.phpfreaks.com/topic/136204-moving-around-an-array/#findComment-715339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.