Solved

php - stop printing associative array loop at key

Posted on 2004-10-25
337 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
Question by:bradnoble
    4 Comments
     
    LVL 27

    Accepted Solution

    by:
    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
    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
    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
    Glad to help :)

    Best Wishes

    |)iablo
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now