2016-01-24 64 views
0

因此,我正在设置我的第一个“幻想”页面系统,并且遇到了问题。我使用下面的代码,它没有任何问题地加载“配置文件”页面,并且默认页面也可以正常工作。然而,其他两个页面根本没有显示,我似乎无法在URL中请求它们。所有的文件都在那里。任何帮助在这里将不胜感激! :)php array page system,not working

if(isset($_SESSION['user_id'])){ 
    require('user.php'); 
    $player = new user($_SESSION['user_id'], $database); 

    $default_page = 'profile'; 
    $pages = array(
     'profile' => array(
      'name' => 'Profile', 
      'file' => 'profile.php', 
      'function' => 'profile', 
      ), 
     'create_monster' => array(
      'name' => 'Create Monster', 
      'file' => 'monsterPages.php', 
      'function' => 'createMonster', 
      ), 
     'create_attack' => array(
      'name' => 'Create Attack', 
      'file' => 'attackPages.php', 
      'function' => 'createAttack', 
     ), 
    ); 

    if(!empty($_GET['page'])){ 
     $page = strtolower(trim($_GET['page'])); 

     if(isset($pages[$page])){ 
      require($pages[$page]['file']); 
      echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>"; 
      $pages[$page]['function'](); 
    } 
    else{ 
      require($pages[$default_page]['file']); 
      echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>"; 
      $pages[$default_page]['function'](); 
    } 
} 
else{ 
    require($pages[$default_page]['file']); 
    echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>"; 
    $pages[$default_page]['function']();  
    } 

} 
+0

你是怎么称呼'Create Monster'页面的?是'profile.php'的同一目录中的'monsterPages.php'?你有'error_porting(-1)'启用'display_errors'; ini_set('display_errors',1);'? – Federkun

+0

是的,monsterPages.php与profile.php位于同一目录。我还没有玩过error_reporting tho。我将阅读如何正确使用它。谢谢您的意见! :) – Naxor

+0

你可以添加var_dump($ _ SESSION ['user_id'])作为第一行,并告诉我们它打印什么?我想你不会开始会话... – Gavriel

回答

0

你有一些冗余代码。你可以缩写它。

$page = isset($_GET['page']) ? $_GET['page'] : $default_page; 

// You should check that the page exists here. 

require($pages[$page]['file']); 
echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>"; 
$pages[$page]['function'](); 

我建议你把页面加载代码放在一个函数中。我在这里创建了一个简单的课程:

<?php 

$pages = array(
    'profile' => array(
     'name' => 'Profile', 
     'file' => 'profile.php', 
     'function' => 'profile', 
     ), 
    'create_monster' => array(
     'name' => 'Create Monster', 
     'file' => 'monsterPages.php', 
     'function' => 'createMonster', 
     ), 
    'create_attack' => array(
     'name' => 'Create Attack', 
     'file' => 'attackPages.php', 
     'function' => 'createAttack', 
    ), 
); 


class PageLoader 
{ 

    public $pages; 
    public $base_path; 

    public function __construct($pages, $base_path) 
    { 
     $this->pages = $pages; 
     $this->base_path = $base_path; 
    } 

    public function run($name) { 
     if(! isset($this->pages[$name])) { 
      throw new Exception('Page not found'); 
     } 

     $path = $this->base_path . DIRECTORY_SEPARATOR . $this->pages[$name]['file']; 
     $func = $this->pages[$name]['function']; 

     require_once $path; 
     call_user_func($func); 
    } 

} 


$page = isset($_GET['page']) ? $_GET['page'] : 'profile'; // If no page given default to profile. 

$loader = new PageLoader($pages, '/tmp'); 
$loader->run($page); 
+0

谢谢你,我会玩这个,看看我能不能把它工作! :) – Naxor

+0

我没有得到这个工作,它为我加载页面的一种新方法,并且即时只是一两个星期到编码。即使它看起来更干净,我觉得我理解它。我只是为了让菜鸟运作起来。非常感谢您抽出宝贵时间为我写信,感谢您的努力! :) – Naxor

+0

哇,在这么多小时之后,拼写错误很难找到!对不起,你的代码工作得很好!但是我仍然有同样的问题! :/它仍然只加载我的profile.php nomatter什么页面我在我的网站搜索栏请求。 – Naxor

0

之前,你可以使用$ _SESSION,你需要启动会话:

session_start(); # this should be the very 1st line in your php 
if(isset($_SESSION['user_id'])){ 
    // ... 
} 
else{ 
    require($pages[$default_page]['file']); 
    echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>"; 
    $pages[$default_page]['function'](); 
    } 
} 

,并确保$pages[$default_page]['function']();$_SESSION['user_id']

+0

是的,我设置了一个会话,我的第一行代码是session_start(); index.php超过了100行代码,所以我只是抓住了页面被调用和设置的部分。 – Naxor