名称数组 = 获取正确的文件

发布于 2024-12-11 20:09:35 字数 831 浏览 0 评论 0原文

我正在尝试编写一个文件列表并将它们放入一个数组中。 必须更改调用外部内容的页面上的内容。

我找到了这段小代码并试图使其工作。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2024-12-18 20:09:35

您必须使用 in_array() 来测试允许的值。 array_key_exists 要求页面名称是数组键。

其次, $_GET["page"] 将为空,因为您没有该名称的 get 参数。您必须调整您的链接:(

   <li><a href="template.php?page=main">Main content</a></li>
   <li><a href="template.php?page=page_two">Second content</a></li>
   <li><a href="template.php?page=page_three">Third content</a></li>

或者使用$_SERVER["QUERY_STRING"]。但是可能不想这样做。)

您的第三个问题是使用:

include('subfolder/folder/'.$_GET['$page'].'.html');
                            ^^^    ^^^

您应该在此处仅使用 $page 变量,该变量已从 $_GET 中读取。再次使用该名称作为密钥将不起作用。而且您还在单引号中使用了它(双重错误,但幸运的是没有效果)。

所以正确的是:

$all_pages = array("main", "page_two", "page_three");    
$page = $_GET['page'];
if ( in_array($page, $all_pages) )
{
    include('subfolder/folder/'.$page.'.html');
}

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:

   <li><a href="template.php?page=main">Main content</a></li>
   <li><a href="template.php?page=page_two">Second content</a></li>
   <li><a href="template.php?page=page_three">Third content</a></li>

(Or otherwise use $_SERVER["QUERY_STRING"]. But you probably don't want to do that.)

Your third problem was using:

include('subfolder/folder/'.$_GET['$page'].'.html');
                            ^^^    ^^^

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:

$all_pages = array("main", "page_two", "page_three");    
$page = $_GET['page'];
if ( in_array($page, $all_pages) )
{
    include('subfolder/folder/'.$page.'.html');
}
自由如风 2024-12-18 20:09:35

array_key_exists 更改为 in_array。另外,变量 $page 可以设置但为空,请使用 if( !empty( $page) && ... 等检查以确保它不为空。

Change array_key_exists to in_array. Also, the variable $page could be set but be empty, check to make sure it's not empty with if( !empty( $page) && ... etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文