bradnoble
asked on
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,'m etadata');
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?
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><
}
$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,'m
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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,$li mit){
$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') ;
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,$li
$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><
if ($key == $limit) break;
}
$var .= '</ul>';
}
return $var;
}
$admintitle = navmaker('pages',$pageID,'
Glad to help :)
Best Wishes
|)iablo
Best Wishes
|)iablo
foreach($array as $key => $value){
$var .= '<li class = "'.$key.'"><a href = "'.$value.'">'.$key.'</a><
}
$var .= '</ul>';
you have to add the check, and break out of for, if it matched.