Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php - stop printing associative array loop at key

Posted on 2004-10-25
4
Medium Priority
?
344 Views
Last Modified: 2008-03-03
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?  
0
Comment
Question by:bradnoble
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 2000 total points
ID: 12401854
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
 
LVL 3

Expert Comment

by:andYetAnotherLoginName
ID: 12402076
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
 

Author Comment

by:bradnoble
ID: 12402755
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12403183
Glad to help :)

Best Wishes

|)iablo
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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