我不知道如何简化我的问题,但我试图用一些细节来解释我的问题。如何将我的一个数组更改为二维或多维数组(底部的示例(,检查数组中一个变量(等于当前类别(和另一个变量(等于当前页面(,并检查页面是否属于类别?
工作代码:
我有下面的工作代码,它允许我获取我将要进入的页面并显示正确的内容(通过包含,此处未显示(。
$path = '';
$category = $_GET['category'];
$page = $_GET['page'];
$content = array('home','about','work','contact','404','team','services');
if (!empty($page)) {
if(in_array($page,$content)) {
if (!empty($category)) {
$path = $category . '/' . $category . '_' . $page;
} else {
$path = 'content_' . $page;
}
}else {
$page = '404';
$url = '/' . $page;
header('Location: ' . $url, true, 404); exit();
}
}
else {
$page = 'home';
$path = 'content_' . $page;
}
我的问题:
我的问题是它在哪里检查是否在数组 $content
中找到$page
。
我需要做这样的事情:
$main = array('home','about','work','contact','404');
$secondary = array('team','services');
。然后在这个:
if (in_array($category,$main) && in_array($page,$secondary)) {
$path = $category . '/' . $category . '_' . $page;
}
即使这有效,仍然存在缺陷。如果$category == $main
和$page == $secondary
但$secondary
不等于当前类别下的页面,则该页面仍将打开,但不会显示任何内容,因为它实际上并不存在。
- 假设"关于"类别有两个页面,即团队和服务,联系人类别也有两个页面,"报价"和"支持"。
- 如果
$category == $main
和$page == $secondary
,$category
/about
,$page
/team
,结果将是URL/about/team
这是正确的。 - 但是,如果
$category
/about
并且$page
/contact
则根据代码,结果在技术上也是正确的,但网址将被/about/contact
,这将显示空白页。
我的想法:
我在想二维数组会更好吗?这就是我卡住的地方!
像这样:
$content = array(
array('Home'),
array('About','Team','Services'),
array('Work'),
array('Contact','Quote','Support'),
array('404')
);
或者这个:
$main = array('home','about','work','contact','404');
$secondary = array(
array('About','Team','Services'),
array('Contact','Quote','Support')
);
如果您需要我的信息,请询问。感谢所有的帮助,最好的方法是什么,如果你能让我走上正确的道路,至少我会不胜感激!
编辑:这是我们到目前为止所拥有的。
<?php
$path = '';
$category = $_GET['category'];
$page = $_GET['page'];
function getUrl($category, $page) {
$pages = array(
'home',
'about' => array('team', 'services'),
'work',
'contact' => array('quote', 'support'),
'404'
);
if (!is_null($category)) {
if(array_key_exists($category, $pages) && in_array($page, $pages[$category])) {
return $category . '/' . $category . '_' . $page;
}
return '404';
} else if (in_array($page, $pages)) {
return 'content_' . $page;
}
return '404';
}
if($page == 'home'){
// stuff for home here
}else if($page == 'about'){
$page_title = 'About';
}else if($page == 'work'){
$page_title = 'Work';
}else if($page == 'contact'){
$page_title = 'Contact';
}else if($page == '404'){
$page_title = 'Oops! (404)';
$path = 'content_404';
}
$page_class = 'page-' . $page;
$web_url = "http://mrobertsdesign.ca";
// Page Start
include('includes/header.php');
include('content/' . $path . '.php');
include('includes/footer.php');
?>
是的,您可以使用二维数组。解决问题的关键是认识到PHP支持关联数组 - 类似于来自Python的Dictionary
或来自Java的Map
数组(除了与Map
不同,PHP的数组不受类型限制(。
从本质上讲,如果您愿意,您可以为每个数组条目分配一个$key => $value
配对。现在停止阅读片刻,想想如何用这些知识解决问题。如果你能得到它,太棒了;如果没有,请继续前进!
因此,我们要设置一个数组,其中$key
是允许的类别名称,$value
s是该类别允许的页面集(子数组(。下面是它的外观(尝试在WriteCodeOnline上运行此代码(:
function getUrl($category, $page) {
$pages = array(
'home',
'about' => array('team', 'services'),
'work',
'contact' => array('quote', 'support'),
'404'
);
if (!is_null($category)) {
if(array_key_exists($category, $pages) && in_array($page, $pages[$category])) {
return $category . '/' . $category . '_' . $page;
}
// Category doesn't exist or page for category not found
return '404';
} else if (in_array($page, $pages)) {
return 'content_' . $page;
}
return '404';
}
// Expected url: 'about/about_team'
var_dump(getUrl('about', 'team'));
// Expected url: 'contact/contact_quote'
var_dump(getUrl('contact', 'quote'));
// Expected url: 'content_home' - Page 'home' exists without category as a page
var_dump(getUrl(null, 'home'));
// Expected url: '404' - Page 'home' of category 'contact' does not exist.
var_dump(getUrl('contact', 'home'));
// Expected url: '404' - Page 'notexist' of category 'contact' does not exist.
var_dump(getUrl('contact', 'notexist'));
因此,您可以看到$category = 'about'
和$page = 'team'
指向现有页面...
而$category = 'about'
和$page = 'contact'
不会。
请参阅:http://php.net/array_key_exists
请参阅:http://php.net/in_array