我正在尝试在我的索引.php页面上使用此代码,以便我所要做的就是编写仅包含html的短文件,以用作站点的单独页面。 例如:
与其让索引、关于和联系页面都有相同的模板代码,我想让索引页面只有网站模板,并在主页导航上有一个指向关于和联系的链接,例如
<a href="index.php?id=about.html">About</a>
<a href="index.php?id=contact.html">Contact</a>
以及 PHP 包含代码。 诀窍是我还使用 php 新闻脚本在主页上显示更新和内容,因此链接的包含必须是我猜测的 else 语句?
这就是我到目前为止得到的,但它返回的错误说
'id' is an undefined index
.
我不确定这意味着什么。
<?php
$id = $_GET['id'];
if
( isset($_GET['id']))
{ $number=5; include("news/newsfilename.php"); }
else
{ include "$id"; }
?>
首先,关于使用查询字符串修改主页的免责声明:这对SEO一点好处都没有。您最好使用一种 .htaccess 魔术形式来创建别名子目录,这些子目录在后台传递到您的查询结构。例如 /about
可能是index.php?id=about.html
的隐藏别名。鉴于您没有问这个问题,我将把操作方法排除在我的答案之外。
如果未设置 id query 参数,$id = $_GET['id'];
也不起作用,尽管您正在检查它,但您也事先设置了 $id
变量。
尝试像这样速记:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : 0; //This is equivalent to a long-form if/else -- if $_GET['id'] is set, get the value, otherwise return 0/false
if ($id)
{
$number=5; include("news/newsfilename.php");
}
else
{
include($id.".html"); //also suggesting you strip out .html from your HTML and add it directly to the php.
}
?>
然后你的 html:
<a href="index.php?id=about">About</a>
<a href="index.php?id=contact">Contact</a>
我认为
这可能是您第一行代码的结果。由于您在检查它是否设置之前尝试获取"id",如果未设置,您将收到错误。