无法获取$ view变量

时间:2014-03-02 23:36:16

标签: php

我有一点问题,我无法在附带的文件中获得$view。请帮助我,我不知道自己该做什么。 Tamplates正在变化,因为else条件运作良好。 var_dump($view);结果:

Notice: Undefined variable: view in C:\xampp\htdocs\Accessories\adminpanel\tpl\main.php on line 26
NULL

的index.php:

<?php    
    $view = empty($_GET['view']) ? 'indexPage' : $_GET['view'];  

    switch ($view)
    {

    }

    if($view != "indexPage")
    {
        include "/tpl/main.php";
    }
    else
    {
        include "/tpl/indexPage.php";
    }
?>

main.php:

<?php session_start(); ?>
<!doctype html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Admin Panel</title>
    <link rel="stylesheet" href="../css/style.css"/>
</head>
<body>    
    <div id="wrapper">
        <nav>
            <ul id="category">
                <span>Hello, <?=$_SESSION['username'];?></span>
                <li><a href="?view=addItem">Add Item | </a></li>
                <li><a href="../lib/logout.php">Exit</a></li>
            </ul>
        </nav>
        <article>
            <?php
                if(isset($view))
                {
                    include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/adminpanel/tpl/pages/'. $view . '.php');
                }
                else
                {
                    include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/adminpanel/tpl/pages/defaultPanel.php');
                }
                // var_dump($view);
            ?>    
        </article>    
    </div>      
</body>
</html>

更新: 我用了同样的东西,写了另一个网站It Worked: 的index.php:

<?php
    include 'catalog.php';

    $view = empty($_GET['view']) ? 'generalPage' : $_GET['view'];
    $cat_name = empty($_GET['cat_name']) ? '' : $_GET['cat_name'];

    switch ($view)
    {
        case('generalPage'):
            $rand_goods = get_random_good();
            break;
        case('categoryPage'):
            if ($ids) $products = get_products($ids);
            else $products = null;
            break;
        case('productPage'):
            $product = $get_one_product;
            break;
    }

    switch ($cat_name)
    {
        case('about'):
            $about = get_about();
            break;
    }

    include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/tpl/main.php');    
?>

main.php:

<!doctype html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Accessories</title>
    <link rel="stylesheet" href="css/style.css"/>
</head>
<body>    
    <div id="wrapper">
        <header></header>    
        <div id="breadCrumbs">
            <?= $breadcrumbs; ?>
        </div>    
        <nav>
            <ul class="category">
                <li><a href="index.php">General</a></li>
                <?php echo $categories_menu; ?>
        </nav>

        <article>
            <?php
                if ($cat_name == '')
                    include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/tpl/pages/' . $view . '.php');
                else 
                {
                    include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/tpl/pages/' . $cat_name . 'Page.php');
                }
            ?>
        </article>
        <div class="clear"></div>    
        <footer>
            <p id="copy">&copy 2014</p>
        </footer>
    </div>
    <script type="text/javascript" src="js/jquery-2.1.0.min.js"></script>
    <script type="text/javascript" src="js/jquery.accordion.js"></script>
    <script type="text/javascript" src="js/jquery.cookie.js"></script>
    <script>
        $(document).ready(function () {
            $(".category").dcAccordion();
        });
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

我认为问题是变量$ view的scope

试试这个:

的index.php

<?php    
    $view = empty($_GET['view']) ? 'indexPage' : $_GET['view'];  



    if($view != "indexPage")
    {
        include "/tpl/main.php";
    }
    else
    {
        include "/tpl/indexPage.php";
    }
?>

main.php

<?php session_start(); ?>
<!doctype html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Admin Panel</title>
    <link rel="stylesheet" href="../css/style.css"/>
</head>
<body>

<div id="wrapper">
    <nav>
        <ul id="category">
            <span>Hello, <?=$_SESSION['username'];?></span>
            <li><a href="?view=addItem">Add Item | </a></li>
            <li><a href="../lib/logout.php">Exit</a></li>
        </ul>
    </nav>
    <article>
            <?php
            $view = $_GET['view'];
            if(isset($view)){
                include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/adminpanel/tpl/pages/'. $view . '.php');
            }else{
                include($_SERVER['DOCUMENT_ROOT'] . '/Accessories/adminpanel/tpl/pages/defaultPanel.php');
            }
           echo var_dump($view);
            ?>

    </article>

</div>


</body>
</html>