Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Moving between items of an array - probably simple!

Posted on 2007-08-03
5
Medium Priority
?
213 Views
Last Modified: 2008-02-01
I've got a set of links for my menu in an array:

$aboutlinks = array (
      "About us" => "/aboutus/aboutus.$ext",
      "Client portfolio" => "/aboutus/clients.$ext",
      "Serious about quality" => "/aboutus/quality.$ext"
);

I have a function that tries to create the following string for links to the previous and next links (Assuming you're currently on the Client Portfolio page:

<- About us | Serious about quality ->

The function thus far is below:

      function printnextprevlinks ($arrayofitems) {
            $currentpage = explode("?", basename($_SERVER[PHP_SELF]));
            $currentpage = $currentpage[0];
            foreach ($arrayofitems as $linktext=>$link) {
                  $linkpage = explode("?", basename($link));
                  $linkpage = $linkpage[0];
                  if ($linkpage == $currentpage) {
                        $currentlink = array_search($link,$arrayofitems);
                        $prevlink = prev($arrayofitems);
                        $nextlink = next($arrayofitems);
                  }
            }
            echo "<div class=\"breadcrumbs\">&laquo; $prevlink | $nextlink &raquo;</div>";
      }

I was expecting array_search to return the item position, so I could just use prev() and next(), but it actually return the key name (i.e. the name of the page), so 'm a bit stuck. Any help would be greatly appreciated!

Cheers,

theskintman
0
Comment
Question by:theskintman
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:rschoenbach
ID: 19625436
If you want it to return the item position you n like this..

$aboutlinks = array(
                         array("About us" => "/aboutus/aboutus.$ext"),
                         array("Client portfolio" => "/aboutus/clients.$ext"),
                         array("Serious about quality" => "/aboutus/quality.$ext")
0
 
LVL 4

Expert Comment

by:rschoenbach
ID: 19625443
opps something happened to what i wrote.

If you want it to return the item position you will want to create the numeric array first then create a multidimensional array like this.

$aboutlinks = array(
                         array("About us" => "/aboutus/aboutus.$ext"),
                         array("Client portfolio" => "/aboutus/clients.$ext"),
                         array("Serious about quality" => "/aboutus/quality.$ext")
0
 

Author Comment

by:theskintman
ID: 19625496
Thanks, but I have loads of other function which rely on this array being as it is - would not fancy altering them all to work with multi-dimentional arrays!

Is there any way in which I can set the current item in the array and just use prev() and next(), like:

foreach ($arrayofitems as $linktext=>$link) {
      $prevlink = prev($arrayofitems);
}

This doesn't work for me.

Or just get the index of the item returned from array_search, rather than the key?
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 total points
ID: 19625590
HI theskintman

Try this

function printnextprevlinks ($arrayofitems) {

      $arrayvalues = array_values($arrayofitems);
      
      $arraykeys = array_keys($arrayofitems);
      
      $currentpage = explode("?", basename($_SERVER[PHP_SELF]));
      
      $currentpage = $currentpage[0];
      
      foreach ($arrayvalues as $key=>$link) {
      
            $linktext = $arraykeys[$key];
            
            $linkpage = explode("?", basename($link));
            
            $linkpage = $linkpage[0];
            
            if ($linkpage == $currentpage) {
            
                  $prevlink = $arrayvalues[$key-1];
                  
                  $nextlink = $arrayvalues[$key+1];
      
            }
      
      }
      echo "<div class=\"breadcrumbs\">&laquo; $prevlink | $nextlink &raquo;</div>";
}       
0
 

Author Comment

by:theskintman
ID: 19626127
Thanks, steelseth12 - works a treat!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question