Consider this:
sidebar.php
PHP Code:
<?php
$menu = array();
$menu['default'] = array(1);
$menu['oranges'] = array(1,2,3);
$menu['grapes'] = array(2,4);
// and so on
function getMySidebarItems($page){
global $menu;
if( array_key_exists($page, $menu)){
return $menu[$page];
}
else {
return $menu['default'];
}
}
oranges.php
PHP Code:
<?php
$this_page = 'grapes';
include 'sidebar.php';
foreach(getMySidebarItems($this_page) as $item){
// using echo to illustrate, but you could include files here
echo "include 'sidebar" . $item . ".php'";
// uncomment out this example
// include 'sidebar' . $item . '.php';
}
For the sake of of illustration oranges.php, sidebar.php and sidebar1.php etc are all in the same folder, or as should be, the include files are well above your /html directory and are where PHP can find them one of the php include paths.
So, each file 'knows' its name and that is set in the file eg 'grapes' - but control of which sidebar appears where is maintained centrally in the $menu array.
There are lots of ways of elaborating and/or simplifying this - but ostensibly you have to maintain your list of pages <-> menu items in some kind of static fashion, here I am just using PHP arrays.
If you suddenly add a new page, 'grapefruit' but fail to assign an array for it in $menu, then you should get $menu['default'].
Bookmarks