名称数组 = 获取正确的文件
我正在尝试编写一个文件列表并将它们放入一个数组中。 必须更改调用外部内容的页面上的内容。
我找到了这段小代码并试图使其工作。 LINK(最后评论)
$all_pages=array("main","page_two","page_three");
$page = $_GET['page'];
if( isset($page) and array_key_exists("$page", $all_pages) ) // added a missing ')'
{
include('subfolder/folder/'.$_GET['$page'].'.html');
}
好吧,这是行不通的。 只是无法找到正确的文件。 (内容始终显示为空)。
我(尝试)将这些文件称为:
<ul>
<li><a href="template.php?main">Main content</a></li>
<li><a href="template.php?page_two">Second content</a></li>
<li><a href="template.php?page_three">Third content</a></li>
</ul>
提前致谢!
I'm trying to write a list of files and drop them into an array.
Have to change the content on a page calling external contents.
I've found this little piece of code and trying to make it work.
LINK (last comment)
$all_pages=array("main","page_two","page_three");
$page = $_GET['page'];
if( isset($page) and array_key_exists("$page", $all_pages) ) // added a missing ')'
{
include('subfolder/folder/'.$_GET['$page'].'.html');
}
well, this does not work.
Just can't get to the right file. (the content shows always empty).
I (try to) call the files like:
<ul>
<li><a href="template.php?main">Main content</a></li>
<li><a href="template.php?page_two">Second content</a></li>
<li><a href="template.php?page_three">Third content</a></li>
</ul>
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用
in_array()
来测试允许的值。 array_key_exists 要求页面名称是数组键。其次,
$_GET["page"]
将为空,因为您没有该名称的 get 参数。您必须调整您的链接:(或者使用
$_SERVER["QUERY_STRING"]
。但是您可能不想这样做。)您的第三个问题是使用:
您应该在此处仅使用
$page
变量,该变量已从$_GET
中读取。再次使用该名称作为密钥将不起作用。而且您还在单引号中使用了它(双重错误,但幸运的是没有效果)。所以正确的是:
You would have to use
in_array()
to test for allowed values.array_key_exists
would require the page names to be array keys.Secondly,
$_GET["page"]
will be empty, as you didn't have a get parameter of that name. You must adapt your links:(Or otherwise use
$_SERVER["QUERY_STRING"]
. But you probably don't want to do that.)Your third problem was using:
You should have used just the
$page
variable here, which you already read from$_GET
. Using that name as key again would not work. And you additionally used it ín single quotes (doubly wrong, but luckily without effect).So correct would be:
将
array_key_exists
更改为in_array
。另外,变量 $page 可以设置但为空,请使用if( !empty( $page) && ...
等检查以确保它不为空。Change
array_key_exists
toin_array
. Also, the variable $page could be set but be empty, check to make sure it's not empty withif( !empty( $page) && ...
etc.