当我使用switch语句时,我的页面不会更改Web内容

时间:2014-06-13 13:05:50

标签: php html

我的代码出错了,我会选择一个链接,然后我将被重定向到同一个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语句中使用以查看应该输出哪个页面时应该发生什么。

实际发生的情况是页面会在网址的末尾正确显示,但主页会显示而不是关于内容。

3 个答案:

答案 0 :(得分:2)

你写道:

$page = isset($_GET['page']);

所以$page只会是truefalse,并且不包含指定的内容。

做类似的事情:

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');
        ?>