我的代码出错了,我会选择一个链接,然后我将被重定向到同一个index.php
页面,但结尾页面上的变量= home不同。这适用于我的所有项目,但它不适用于此项目。
的index.php
<?php include( 'pages/header.php'); ?>
<?php
$page = isset($_GET[ 'page']);
switch($page){
case 'home':
$title = "Home" ;
include( 'pages/home.php');
break;
case 'about':
$title = "About";
include( 'pages/about.php');
break;
case 'features':
$title = "features";
include( 'pages/features.php');
break;
case 'pricing':
$title = "Pricing";
include( 'pages/pricing.php');
break;
case 'login':
$title = "Login";
include( 'pages/login.php');
break;
case 'register':
$title = "Register";
include( 'pages/register.php');
break;
default:
echo 'Error';
break;
}
?>
<?php include( 'pages/footer.php'); ?>
的header.php
<nav id="mainmenu">
<ul>
<li>
<img src="images/homr.png" width="30" height="30" alt="Home" /> <a href="index.php?page=home">Home</a>
</li>
<li>
<img src="images/about.png" width="30" height="30" alt="" /> <a href="index.php?page=about">About Us</a>
</li>
<li>
<img src="images/features.png" width="30" height="30" alt="" /> <a href="index.php?page=features">Features</a>
</li>
<li>
<img src="images/pricing.png" width="30" height="30" alt="" /> <a href="index.php?page=pricing">Pricing</a>
</li>
<li>
<img src="images/register.png" width="30" height="30" alt="" /> <a href="index.php?page=register" target="_blank">Register</a>
</li>
<li>
<img src="images/login.png" width="30" height="30" alt="" /> <a href="index.php?page=login" target="_blank">Log In</a>
</li>
</ul>
<div id="whitesmallmenu"><a class="button" href="#">Contact Us</a>
</div>
</nav>
当我选择about链接它应该通过变量页面发送内容然后页面变量将在switch语句中使用以查看应该输出哪个页面时应该发生什么。
实际发生的情况是页面会在网址的末尾正确显示,但主页会显示而不是关于内容。
答案 0 :(得分:2)
$page = isset($_GET['page']);
所以$page
只会是true
或false
,并且不包含指定的内容。
做类似的事情:
if(isset($_GET['page'])) {
$page = $_GET['page'];
switch($page){
case 'home':
$title = "Home";
include('pages/home.php');
break;
case 'about':
$title = "About";
include('pages/about.php');
break;
//...
} else {
//... ie. include the startpage
}
答案 1 :(得分:0)
无需在同一<?php
文件中使用多个?>
和php
标记。你还switch
布尔变量。好的,改变了:
$page = isset($_GET['page']);
要
$page = isset($_GET['page']) ? $_GET['page'] : '';
答案 2 :(得分:0)
<?php
include('pages/header.php');
?>
<?php
$page = isset($_GET['page']);
if($page === true){
switch($_GET['page']){
case 'home':
$title = "Home";
include('pages/home.php');
break;
case 'about':
$title = "About";
include('pages/about.php');
break;
case 'features':
$title = "features";
include('pages/features.php');
break;
case 'pricing':
$title = "Pricing";
include('pages/pricing.php');
break;
case 'login':
$title = "Login";
include('pages/login.php');
break;
case 'register':
$title = "Register";
include('pages/register.php');
break;
default:
echo'Error';
break;
}
}
?>
<?php
include('pages/footer.php');
?>