2016-05-29 108 views
0

我已經寫了一個函數,應該先輸出一個導航條,並在包含頁面內容後,我試圖把include包含在一個變量中,但它不起作用,不知道該怎麼做,你能幫助我嗎?返回包含在PHP中

這裏是代碼:

class myclass { 
function test() { 
    if($_SESSION['logged_in'] == TRUE) { 
     $pages = array('news', 'main', 'email', 'switch', 'comments'); 

     $page = "main"; 
     if(isset($_GET['page'])) { 
      $page = $_GET['page']; 
     } 

     //navigation bar 
     $blah = '<div id="shittynavigationbar"><ul>'; 
     foreach($pages as $url) { 
      $out .= '<li><a href="/?id=admin&amp;action=$url"'; 
      if($url != $n){ 
       $out .= ' id="active"'; 
      } 
      $out .= '>' . ucfirst($url) . '</a></li>'; 
     } 
     $out .= '</ul></div><div id="main">'; 


     //content 
     if(!in_array(basename($action), $pages)) { 
      $out .= include('admin/inc/' . $action . '.php'); 
     } else { 
      $out .= 'page not found'; 
     } 

     return $out; 
    } else { 
     return 'you are not logged in'; 
    } 
} 
} 
$a = new myclass(); 
echo $a->test(); 

在此先感謝

+1

你正在包含的文件需要返回一個值。 – Rainner

回答

0

實際上你需要獲取你想包括PHP文件的內容,或者使用cURLfile_get_contents()如下:

//content 
if(!in_array(basename($action), $pages)) 
{ 
    $out.= file_get_contents('admin/inc/' . $action . '.php'); 
} 
else 
{ 
    $out .= 'page not found'; 
} 
+0

但在我想包含的文件中有一些PHP代碼,並且使用'file_get_contents'將不會運行 –

+0

那麼爲什麼有一個函數可以包含它呢?用單個函數生成頁眉並在頁面上調用它會更好。 – BenM

0

我有一個解決方案,但它會改變更多

在文件$ action中。 「.PHP」創建

class ActionName { 
    function run(){ 
      return ''; 
    } 
} 

然後在你的代碼

include('admin/inc/' . $action . '.php'); 
$actionName = new $action(); 
$out .= $actionName->run(); 

感謝閱讀。希望它有幫助