Forum Moderators: coopster
I am using a php array as part of a menu, to determine which section is currently being viewed and based on that serve up the correct submenu. I don't really know much about php myself, so I had help putting the code together, and now I have run into a few instances where the code fails.
To fix this, I need to add some further conditions to the array, so I cobbled together an example of what I'd like it to do:
$URL = $_SERVER['PHP_SELF'];
$Parts = explode("/",$URL);
Step 1: If $Parts[2] is NOT index.php or index.html, go ahead and define $CurrentSection. Not sure how I would get it to not to go onto step 2 and 3 if step 1 is as it should be, though.
if ($Parts[2]!= "index.php¦index.html")
{
$CurrentSection = "/".$Parts[2]."/";
}
Step 2: If step 1 wasn't true, check to see if $Parts[2] IS index.html. If so, terminate (the default menu should be displayed, so no values need to be set). Again, if this is true, it shouldn't go on to step 3.
elseif ($Parts[2] == "index.html")
{
die;
}
Step 3: If part 2 is index.php and $Parts[3] exists and $Parts[3] is within the array, set $CurrentSection. Otherwise, terminate.
elseif ($Parts[2] == "index.php") && ($Parts[3]!= "NULL") && (array_key_exists($WhichMainMenuItem,$Parts[3]))
{
$CurrentSection = "/".$Parts[3]."/";
}
else
{
die;
}
Step 4: The following variables should only be defined in those instances when $CurrentSection has been defined.
$MainMenuItem = $WhichMainMenuItem[$CurrentSection];
$MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];
$SubMenu = $WhichSubMenu[$CurrentSection];
$SubMenuItem = $WhichSubMenuItem[$CurrentSection];
If anyone could help me to get this to work as intended, I would really appreciate it. :)
your approach is quite "if then and if then then" which does not make it simple, but to code a solution you need to crush your "big" problem into simple ones you can solve easily one after the other and combine all solution to "the one" afterwards.
so, next to your general approach to the problem, most of your questions are already answered in your code:
Step 1: If $Parts[2] is NOT index.php or index.html, go ahead and define $CurrentSection. Not sure how I would get it to not to go onto step 2 and 3 if step 1 is as it should be, though.
this is about if/else/elseif and quite simple and you have already applied it in parts:
if ($Parts[2]!= 'index.php' ¦¦ $Parts[2]!= 'index.html')
{
// step 1: go ahead and define $CurrentSection.
} else {
// step 2 and 3 in here!
} as you can see, you can put multiple steps within the if or else part. this part is defined by the curly braces {}. so the only difference is to put all the decisions between step 2 and 3 into these braces:
if ($Parts[2]!= 'index.php' ¦¦ $Parts[2]!= 'index.html')
{
// step 1: go ahead and define $CurrentSection.
} else {
// step 2 and 3 in here!
if ($Parts[2] == "index.html")
{
// step 2
} else {
// step 3
}
} but this is only the general usage of if/else/elseif. checkout the php manual for this Chapter 16. Control Structures [php.net].
While typing I see that your code logically does not make any sense. step1 should be executed everytime because when the file is called index.html it won't be index.php so step one is executed and if it's index.php it won't be index.html and it is executed, too. so you don't need and OR comparison it's and AND comparison.
maybe that's your problem?
Some examples:
1) www.domain.com/Citadel/FAQ/ works fine, since $Parts[2] is /FAQ/ and that is found within the array.
2) www.domain.com/Citadel/SSM/ is actually www.domain.com/Citadel/index.php/SSM/ internally, but it also works fine, since $Parts[2] is index.php in this case and then I get $Parts[3] instead which is /SSM/ and that is found within the array.
3) www.domain.com/Citadel/index.html has also worked fine, even though $Parts[2] is index.html which isn't within the array. But for this page I have ended up with the default menu, without any problems at all.
4) www.domain.com/Citadel/index.php does not work. I do get the default menu, but I also get a bunch of errors, and I can't understand why this happens with index.php and not index.html.
With some more feedback on another forum, I've changed the conditionals to this, I've added some commentary for the areas that still cause problems.
if ($Parts[2] == "index.html")
{
In this case, I just need the default menu. However, I can't use 'die' here because then the rest of the page this is included into doesn't load. Can I set the $CurrentSection variable to /Default/ (if so, how?) and then have an entry in the array called /Default/ that results in an empty value (if so, how?)? Not setting &MainMenuItem and the other variables seems to cause errors too, so they need to be set, even if its to an empty value.
}
if ($Parts[2] == "index.php" && $Parts[3]!= "NULL" && array_key_exists($WhichMainMenuItem,$Parts[3]))
{
$CurrentSection = "/".$Parts[3]."/";
}
else
{
In this case, I just need the default menu. However, I can't use 'die' here because then the rest of the page this is included into doesn't load. Can I set the $CurrentSection variable to /Default/ (if so, how?) and then have an entry in the array called /Default/ that results in an empty value (if so, how?)? Not setting &MainMenuItem and the other variables seems to cause errors too, so they need to be set, even if its to an empty value.
}
if ($Parts[2]!= "index.php" && $Parts[2]!= "index.html")
{
$CurrentSection = "/".$Parts[2]."/";
}
$MainMenuItem = $WhichMainMenuItem[$CurrentSection];
$MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];
$SubMenu = $WhichSubMenu[$CurrentSection];
$SubMenuItem = $WhichSubMenuItem[$CurrentSection];
<?php $WhichMainMenuItem = Array (
'FAQ' => 'VolumeI',
'SSM' => 'VolumeI',
'Concordance' => 'VolumeII',
'Encyclopaedia' => 'VolumeII',
'Heraldry' => 'VolumeII',
'History' => 'VolumeII',
'Characters' => 'VolumeIII',
'Prophecies' => 'VolumeIII',
'Artwork' => 'VolumeIV',
'Books' => 'VolumeIV',
'Images' => 'VolumeIV',
'Default' => ''
);
$WhichMainMenuItemID = Array (
'FAQ' => 'main01',
'SSM' => 'main01',
'Concordance' => 'main02',
'Encyclopaedia' => 'main02',
'Heraldry' => 'main02',
'History' => 'main02',
'Characters' => 'main03',
'Prophecies' => 'main03',
'Artwork' => 'main04',
'Books' => 'main04',
'Images' => 'main04',
'Default' => ''
);
$WhichSubMenu = $WhichMainMenuItem;
$WhichSubMenuItem = Array (
'FAQ' => 'FAQ',
'SSM' => 'SSM',
'Concordance' => 'Concordance',
'Encyclopaedia' => 'Encyclopaedia',
'Heraldry' => 'Heraldry',
'History' => 'History',
'Characters' => 'Characters',
'Prophecies' => 'Prophecies',
'Artwork' => 'Artwork',
'Books' => 'Books',
'Images' => 'Images',
'Default' => ''
);
$URL = $_SERVER['PHP_SELF'];
$Parts = explode("/",$URL);
if ($Parts[2]!= "index.php" && $Parts[2]!= "index.html")
{
$CurrentSection = $Parts[2];
}
elseif ($Parts[2] == "index.html")
{
$CurrentSection = 'Default';
}
elseif ($Parts[2] == "index.php" && $Parts[3]!= "NULL" && array_key_exists($Parts[3],$WhichMainMenuItem))
{
$CurrentSection = $Parts[3];
}
else
{
$CurrentSection = 'Default';
}
$MainMenuItem = $WhichMainMenuItem[$CurrentSection];
$MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];
$SubMenu = $WhichSubMenu[$CurrentSection];
$SubMenuItem = $WhichSubMenuItem[$CurrentSection];
?>
The only remaining problem is when there isn't a $Parts[3]. In those cases, I get this error:
Notice: Undefined offset: 3 in <filename>.
I imagine the problem lies with this part:
elseif ($Parts[2] == "index.php" && $Parts[3]!= "NULL" && array_key_exists($Parts[3],$WhichMainMenuItem))
{
$CurrentSection = $Parts[3];
}
I am not sure what the problem might be, however.
first of all i would cut down the menue definition:
<?$MainMenu = Array(
'FAQ' => Array('VolumeI', 'main01'),
'SSM' => Array('VolumeI', 'main01'),
'Concordance' => Array('VolumeII', 'main02'),
/* ... */
'Default' => Array('', '', )
);
?>
using a multi dimensional array here (an array with arrays within) makes it look much nicer, isn't it?
so now, let's take a look at your decision again:
, right?
so first of all, all these 3 possibilities which are for the same thing should be normalized before the decision which menüentry should be used here. because you're using
$Parts = explode("/",$URL); i suggest to completely remove each index.phpand
index.htmlentry from the
$Partsarray:
<?$Parts = explode("/",$URL);
$Parts = [url=http://www.php.net/manual/en/function.array-flip.php]array_flip[/url]($Parts);
[url=http://www.php.net/foreach]foreach[/url](array('index.php', 'index.html') as $value)
if (isset($Parts[$value])) unset($Parts[$value]));
$Parts = array_flip($Parts);
?>
after this, $Parts should (if i made no mistake) contain in all three cases the following values:
Array(
[0] => "Citadel"
[1] => "FAQ"
); hmm, i think you should base your menue decision on this, because after normalising the array, this creates a unique key for each resource. implode() [php.net] can create that key which can be used as reference into your $MenuArray:
<?$MenuKey = implode('/', $Parts);
$MenuArray = Array(
"Default" => Array('', ''),
"Citadel/FAQ" => Array('VolumeI', 'main01')
);
/* use default menue if it does not exists */
if (!isset($MenuArray[$MenuKey]))
$MenuKey = Default;
$MenuData = $MenuArray[$MenuKey];
?>
$MenuData would then contain everything you ever wanted. Maybe this approach leads to something that will help you?
For example, the variables set at the end of my code ($MainMenuItem, etc) are used like this within the menu itself:
if ($MainMenuItem == "VolumeI")
display menu element in 'active' state
else
display menu element in 'inactive'state
If all of the values for the FAQ URL (VolumeI, main01, FAQ) were to be contained within a single variable, how would I modify the menu to check for this varible?
If all of the values for the FAQ URL (VolumeI, main01, FAQ) were to be contained within a single variable, how would I modify the menu to check for this varible?
$MainMenuItem = $MenuData[0];
in that particular case or just use it along:
if ($MenuData[0] == "VolumeI")
display menu element in 'active' state
else
display menu element in 'inactive'state
--hakre
<?php $WhichMenu = Array (
'Digest' => Array('Changes','main01','Digest'),
'Updates' => Array('Changes','main01','Updates'),
'Academia' => Array('AboutUs','main02','Academia'),
'Bios' => Array('AboutUs','main02','Bios'),
'Memoirs' => Array('AboutUs','main02','Memoirs'),
'Projects' => Array('AboutUs','main02','Projects'),
'Horsetales' => Array('Horses','main03','Horsetales'),
'Essays' => Array('BooksEtc','main04','Essays'),
'Favourites' => Array('BooksEtc','main04','Favourites'),
'News' => Array('BooksEtc','main04','News'),
'Quickies' => Array('BooksEtc','main04','Quickies'),
'Reviews' => Array('BooksEtc','main04','Reviews'),
'Annals' => Array('MUing','main05','Annals'),
'Characters' => Array('MUing','main05','Characters'),
'Logs' => Array('MUing','main05','Logs'),
'MUsings' => Array('MUing','main05','MUsings'),
'Art' => Array('Galleries','main06','Art'),
'Photos' => Array('Galleries','main06','Photos'),
'Default' => Array('','','')
);
$URL = $_SERVER['PHP_SELF'];
$Parts = explode("/",$URL);
if (array_key_exists($Parts[1],$WhichMenu))
{
$CurrentSection = $Parts[1];
}
elseif ($Parts[1] == "index.php" && isset($Parts[2]) && array_key_exists($Parts[2],$WhichMenu))
{
$CurrentSection = $Parts[2];
}
else
{
$CurrentSection = 'Default';
}
$Menu = $WhichMenu[$CurrentSection];
?>
Do you think I should tweak the conditionals further, or is this about as efficient as it gets? I didn't quite follow how I would use the other method that you described for obtaining the right information from the URL.
I really appreciate the help, and the suggestion for how to rework the array was excellent. :)
Here's the proper one:
<?php $WhichMenu = Array (
'FAQ' => Array('VolumeI','main01','FAQ'),
'SSM' => Array('VolumeI','main01','SSM'),
'Concordance' => Array('VolumeII','main02','Concordance'),
'Encyclopaedia' => Array('VolumeII','main02','Encyclopaedia'),
'Heraldry' => Array('VolumeII','main02','Heraldry'),
'History' => Array('VolumeII','main02','History'),
'Characters' => Array('VolumeIII','main03','Characters'),
'Prophecies' => Array('VolumeIII','main03','Prophecies'),
'Artwork' => Array('VolumeIV','main04','Artwork'),
'Books' => Array('VolumeIV','main04','Books'),
'Images' => Array('VolumeIV','main04','Images'),
'Default' => Array('','','')
);
$URL = $_SERVER['PHP_SELF'];
$Parts = explode("/",$URL);
if (array_key_exists($Parts[2],$WhichMenu))
{
$CurrentSection = $Parts[2];
}
elseif ($Parts[2] == "index.php" && isset($Parts[3]) && array_key_exists($Parts[3],$WhichMenu))
{
$CurrentSection = $Parts[3];
}
else
{
$CurrentSection = 'Default';
}
$Menu = $WhichMenu[$CurrentSection];
?>