我有以下代码,它与我的htaccess文件一起工作,通过PHP动态显示内容包含在同一个welcome.php页面中。这只是整个页面的一部分。第一个代码块是具有二维数组的工作代码。此代码查找包含文件的路径并创建网站的常规结构。
$category = $_GET['category'];
$page = $_GET['page'];
function getUrl($category, $page) {
$pages = array(
'home',
'design' => array('about', 'events', 'contact'),
'community' => array('about', 'events', 'contact'),
'philanthropy' => array('about', 'events', 'contact'),
'404'
);
if (!is_null($category)) {
if(array_key_exists($category, $pages) && in_array($page, $pages[$category])) {
return $category . '/' . $category . '_' . $page;
}
} elseif (in_array($page, $pages) || array_key_exists($page, $pages)) {
return 'content_' . $page;
}
return 'content_404';
}
$path = getUrl($category, $page);
我目前正在使用二维数组,我希望能够使用三维数组来创建另一层页面,但我无法让它工作但是......这就是我尝试过的到目前为止:
$category = $_GET['category'];
$subcat = $_GET['subcat'];
$page = $_GET['page'];
function getUrl($category, $subcat, $page){
$pages = array(
'home',
'design' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'community' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'philanthropy' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'404'
);
if (!is_null($category)) {
if(array_key_exists($category, $pages) && in_array($page, $pages[$category])) {
return $category . '/' . $category . '_' . $page;
}
} elseif (in_array($page, $pages) || array_key_exists($page, $pages)) {
return 'content_' . $page;
}
return 'content_404';
}
$path = getUrl($category, $subcat, $page);
这将是.htaccess文件:
RewriteRule ^([a-zA-Z0-9]+)/?$ welcome.php?page=$1 [NC]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ welcome.php?category=$1&page=$2 [NC]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ welcome.php?category=$1&subcat=$2&page=$3 [L,NC]
我想要的网站结构是这样的:
Home
Design
-- About
---- Services
---- Partners
-- Events
-- Contact
Community
-- About
---- Services
---- Partners
-- Events
-- Contact
Philanthropy
-- About
---- Services
---- Partners
-- Events
-- Contact
--------------------------
Example:
/design = $page
/design/about = $category/$page
/design/about/services = $category/$subcat/$page
多维数组是我仍然习惯的东西,不确定如何使其与三维数组一起工作......基本上我要做的是让代码与数组一起工作。
答案 0 :(得分:1)
以下是支持三个维度的函数版本:
function getUrl($category, $page, $subpage){
$pages = array(
'home',
'design' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'community' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'philanthropy' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'404'
);
if (!is_null($category) && !is_null($subpage)) {
// Handle subpage.
if (array_key_exists($category, $pages) && array_key_exists($subpage, $pages[$category]) && in_array($page, $pages[$category][$subpage])) {
return $category . '/' . $subpage . '/' . $category . '_' . $subpage . '_' . $page;
}
} elseif (!is_null($category)) {
// Handle page.
if (array_key_exists($category, $pages) && (in_array($page, $pages[$category]) || array_key_exists($page, $pages[$category]))) {
return $category . '/' . $category . '_' . $page;
}
} elseif (in_array($page, $pages) || array_key_exists($page, $pages)) {
return 'content_' . $page;
}
return 'content_404';
}
最难的部分是确保每个密钥都存在,并设置$ subpage和$ category。请注意,我不确定您的函数期望什么类型的参数,但为了以防万一,我添加了$ subpage的空检查。
答案 1 :(得分:0)
我希望这有帮助,
function getUrl($category = FALSE, $page = FALSE, $subpage = FALSE){
$pages = array(
'home',
'design' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'community' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'philanthropy' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'404'
);
if(!is_null($category) && !is_null($page) && !is_null($subpage)){ // validate category, page and subpage
foreach($pages as $keys=>$val){
if(is_array($pages[$keys])){
if($category == $keys && in_array($page, $pages[$keys])){
return $category . '/' . $category . '_' . $page;
}
}else {
}
if($keys == "design" && $category == "design"){
foreach($pages['design'] as $key=>$vall){
if($page == $key){
if(is_array($vall)){
if(in_array($subpage, $vall)){
return $category . '/' . $category . '_' . $page . '/' . $subpage;
}
}else {
return $category . '/'. $page;
}
}
}
}
}
}
else if(!is_null($category) && !is_null($page)) { // validate category and page only
if(array_key_exists($category, $pages) && in_array($page, $pages[$category])) {
return $category . '/' . $category . '_' . $page;
}
}
else if(!is_null($page)){ // validate page only
if (in_array($page, $pages) || array_key_exists($page, $pages)) {
return 'content_' . $page;
}
}else {
// no parameter set
}
}
建议您使用!empty而不是!is_null,因为如果在参数
上设置'',它将返回true答案 2 :(得分:0)
首先,您必须检查页面是否存在。
假设这是你的数组
$pages = array(
'home',
'design' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'community' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'philanthropy' => array(
'about' => array('services', 'partners'),
'events',
'contact'
),
'404'
);
然后检查页面是否存在
if(in_array($page, $pages)){
/* Page found here we have to check the structure of this page. */
if(is_array($pages[$page])){
if(in_array($category, $pages[$page])){
/* Category found here we have to check the structure of this category. */
if(is_array($pages[$page][$category])){
if(in_array($subpage, $pages[$page][$category])){
/* Sub Page found return it. */
return $category . '/' . $subpage . '/' . $category . '_' . $subpage . '_' . $page;
}
}
/* All done we don't have more items in the list. */
return $category . '/' . $category . '_' . $page;
}
}
/* All done we don't have more items in the list. */
return 'content_' . $page;
}else{
/* Page not found */
return 'content_404';
}