PHP If-Else Stack似乎错综复杂
发布时间:2020-12-13 21:46:09 所属栏目:PHP教程 来源:网络整理
导读:我正在使用 PHP来布局导航菜单并根据URL显示内容. 我已经到达IF-ELSE堆栈,确定要显示的内容(通过加载必要的类和/或方法).但是必须有更好的方法来写这个……任何建议? BreadCrumbs :: getCrumb()是一个静态方法,用于根据索引值(URI请求,用’/’拆分,然后保存
我正在使用
PHP来布局导航菜单并根据URL显示内容.
我已经到达IF-ELSE堆栈,确定要显示的内容(通过加载必要的类和/或方法).但是必须有更好的方法来写这个……任何建议? BreadCrumbs :: getCrumb()是一个静态方法,用于根据索引值(URI请求,用’/’拆分,然后保存在数组中)检索从URL保存的元素. …(ArrayHelp :: recValueSearch(BreadCrumbs :: getCrumb(2),Config :: getNavPrimary())针对包含Config类中导航列表的数组检查URI元素. BreadCrumbs :: setEmptyCrumb(1,“home”)运行一个方法,如果该值不存在或无效(在导航列表数组中),则设置默认值 <?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'; } ?> 编辑:更改为Switch语句如下,好一点.. <?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; } ?> 解决方法
如果您的示例代码显示了您的完整要求,那么只需使用getCrumb返回的实际字符串就可以大大简化其中的一些:
$crumb = ucfirst(BreadCrumbs::getCrumb(1)); switch($crumb){ case 'home': case 'foobar': # etc,etc echo "This is the <b>$crumb</b> Page"; break; # or add some extra logic default: # handle invalid crumbs here break; } ( (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |