I am using PHP to layout a navigation menu and display content according to the URL.
I have reached the IF-ELSE stack that determines what content to show (by loading the necessary classes and/or methods). But there has to be a better way of writing this.. any suggestions?
BreadCrumbs::getCrumb() is a static method used to retrieve an element saved from the URL based on the index value (URI Request, split by ‘/’, then saved in array).
…(ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavPrimary()) checks the URI element against the array containing the navigation list from the Config class.
BreadCrumbs::setEmptyCrumb(1, “home”) runs a method which sets a default value if the value is either not present or not valid (in the nav list array)
<?php
// set bread crumbs
BreadCrumbs::setCrumbs($_SERVER['REQUEST_URI']);
BreadCrumbs::setEmptyCrumb(1, "home");
BreadCrumbs::setEmptyCrumb(2, "all");
if (BreadCrumbs::getCrumb(1) == 'about') {
echo 'This is the <b>About</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'contact') {
echo 'This is the <b>Contact</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'search') {
echo 'This is the <b>Search</b> Page';
}
else if (BreadCrumbs::getCrumb(1) == 'home') {
if (BreadCrumbs::getCrumb(2) == 'all') {
echo 'This is the <b>Home</b> Page';
}
else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavSecondary())) {
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
}
else {
echo 'change filter value and go to the <b>home</b> page';
}
}
else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(1), Config::getNavPrimary())) {
if (BreadCrumbs::getCrumb(2) == 'all') {
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is <b>all countries</b>';
}
else if (ArrayHelp::recValueSearch(BreadCrumbs::getCrumb(2), Config::getNavSecondary())) {
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
}
else {
echo 'change filter value to all and go to category: <b>' . BreadCrumbs::getCrumb(1) . '</b>';
}
}
else {
echo 'redirect page to home/all';
}
?>
EDIT: Changed to Switch Statement as below, a little better..
<?php
$array_helper = new ArrayHelp;
$valid_primary = $array_helper->recValueReturn(BreadCrumbs::getCrumb(1), Config::getNavPrimary());
$valid_secondary = $array_helper->recValueReturn(BreadCrumbs::getCrumb(2), Config::getNavSecondary());
switch (BreadCrumbs::getCrumb(1)) {
case 'about' :
case 'contact' :
case 'search' :
echo 'This is the <b>' . BreadCrumbs::getCrumb(1) . '</b> Page';
break;
case 'home' :
switch (BreadCrumbs::getCrumb(2)) {
case 'all' :
echo 'This is the <b>Home</b> Page';
break;
case ($valid_secondary[1]) :
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
break;
default:
echo 'change filter value and go to the <b>' . BreadCrumbs::getCrumb(1) . '</b> page';
break;
}
break;
case ($valid_primary[1]) :
switch (BreadCrumbs::getCrumb(2)) {
case ($valid_secondary[1]) :
echo 'This is the list page for category: <b>' . BreadCrumbs::getCrumb(1) . '</b> and the country filter is: <b>'. BreadCrumbs::getCrumb(2) . '</b>';
break;
default:
echo 'change filter value and go to the <b>' . BreadCrumbs::getCrumb(1) . '</b> page';
break;
}
break;
default:
echo 'redirect page to home/all';
break;
}
?>
If your example code shows your complete requirements some of it could be greatly simplified by just using the actual string returned by
getCrumb:(
ucfirstis used to make the first character of the string uppercase.)