Moving to Next Record in an Array?

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

Moving to Next Record in an Array?

Post by millsy007 »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


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:

Code: Select all

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

Code: Select all

 
<?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 ???


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Moving to Next Record in an Array?

Post by papa »

next();
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Moving to Next Record in an Array?

Post by millsy007 »

I tried doing:

Code: Select all

if ((string) $url->loc == $currenturl) {
 
next($url);
To move to the next record but it doesnt work?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Moving to Next Record in an Array?

Post by papa »

$url is not an array.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Moving to Next Record in an Array?

Post by millsy007 »

I thought it was ??? What should I be putting into the next() to go to the next record?
Sorry I am a beginner
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Moving to Next Record in an Array?

Post by papa »

$xml->url is the array you are getting $url from, so try:

$next_page = next($xml->url);

Bare in mind that this moves the internal pointer for that array even if you assign it to a variable.
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Moving to Next Record in an Array?

Post by millsy007 »

Okay I am a little confused to be honest. When I set $next_page = next($xml->url); should I in theory be moving one record down my sitemap?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Moving to Next Record in an Array?

Post by papa »

Yup.

url[0][loc] = "...home.php";

next

url[1][loc] = "...about.php";
invertrio
Forum Newbie
Posts: 3
Joined: Tue Dec 09, 2008 6:07 pm
Location: Blue Planet
Contact:

Re: Moving to Next Record in an Array?

Post by invertrio »

Hopefully this will clear up some confusion.

$xml is an object.

$xml->url is an array property in the $xml object.

For more information about Object Oriented Programming (OOP - some of which you are using), you can visit PHP's documentation here:

http://php.net/oop
Post Reply