php - stop printing associative array loop at key

Hi EE,

I'm using a class to create interaction screens for a content mgmt system.  i want to add a 'locator' to each page, so the user can see where in the system they are.  each area of the system has a logical order -- in the web pages area, a user goes from the 'index of pages' to the page-specific 'edit metadata' screen, then to the 'preview' screen, for example.

on the preview screen, i want to show the user they have moved from the index through the metadata page to the preview, using a breadcrumb.  so, the breadcrumb would look like this:
page index > metadata > preview

i thought the easiest way to create this representation (with links) would be to create an array containing the title and filename of screens in the page editing process, where the key is the screen title and the value is the filename of that screen.  here's that array, contained in a function to be called later:

function pagesnav($pageID){
      $var = NULL;
      $string = ($pageID <> '') ? '?pageID='.$pageID : '';
      
      $var = array(
      'index' => 'auditor.php',
      'metadata' => 'metadata.php'.$string,
      'preview' => 'preview.php'.$string,
      );
      
      return $var;
}

this function creates the HTML of the unordered list that represents the user's location, and it calls the function above to generate the list:
function navmaker($area,$pageID){
      $var = NULL;
      if($area == 'pages'){
            $array = pagesnav($pageID);
      }
      if($array <> ''){
            $var .= '<ul>';
            foreach($array as $key => $value){
                  $var .= '<li class = "'.$key.'"><a href = "'.$value.'">'.$key.'</a></li>';
            }
            $var .= '</ul>';
      }
      return $var;
}

what doesn't work here is that the pagenav function prints the entire array, whereas i want it to print until it reaches the page the user is on.  so, on the metadata page, the call would be something like:
$locator = pagenav('pages',$pageID,'metadata');

the problem with this solution is i don't know how to stop the pagenav function from printing the pages array with the third parameter there ('metadata'), b/c this is an associative array.  

anyone have any thoughts on how to stop the array from printing at any point in the order above?  
bradnobleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Diablo84Commented:
you could make use of the control struc break

break;

http://us2.php.net/manual/en/control-structures.break.php


for example inside your foreach loop you would do something like:

if (basename($var) == basename($_SERVER['PHP_SELF'])) break;

Not sure if thats right according to your code, a little tight for time sorry.

Basically if the current page is the same as the page in the loop then break out of the loop.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andYetAnotherLoginNameCommented:
looks like your prob is here:
foreach($array as $key => $value){
              $var .= '<li class = "'.$key.'"><a href = "'.$value.'">'.$key.'</a></li>';
         }
         $var .= '</ul>';

you have to add the check, and break out of for, if it matched.
0
bradnobleAuthor Commented:
diablo, thanks for your help.  the if/then with the break did it.  i just added a '$limit' parameter to the variable that calls the function -- $limit is $key of the array that corresponds the user's location.  in the example below, i use 'metadata' as $limit, so the loop stops at metadata.  

function pagesnav($pageID){
      $var = NULL;
      $string = ($pageID <> '') ? '?pageID='.$pageID : '';
      
      $var = array(
      'index' => 'auditor.php',
      'metadata' => 'metadata.php'.$string,
      'preview' => 'preview.php'.$string,
      );
      
      return $var;
}

function navmaker($area,$pageID,$limit){
      $var = NULL;
      if($area == 'pages'){
            $array = pagesnav($pageID);
      }
      if($array <> ''){
            $var .= '<ul>';
            foreach($array as $key => $value){
                  $var .= '<li class = "'.$key.'"><a href = "'.$value.'">'.$key.'</a></li>';
if ($key == $limit) break;
            }
            $var .= '</ul>';
      }
      return $var;
}



$admintitle = navmaker('pages',$pageID,'metadata');
0
Diablo84Commented:
Glad to help :)

Best Wishes

|)iablo
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.