2015-07-02 52 views
0

找不到解決我的代碼問題的答案。包含的文件讓我取消未定義的變量

file.php:

if(isset($_GET['season1'])) { 

    $season1 = $_GET['season1']; 
    $url = 'http://www.imdb.com/title/'.$season1.'/episodes?season=1'; 

    $imdb_content = file_get_contents($url); 
    $html = str_get_html($imdb_content); 

    //Grabbed Content 
    $resultarray = array(); 
    foreach($html->find('strong a[itemprop="name"]') as $results) { 
     $resultarray[] = $results; 
    } 

    echo $resultarray[0]; 
    echo $resultarray[1]; 

} 
else { 
     header("Location: ../"); 
} 

的index.php:

include("http://example.com/file.php?season1=".urlencode($imdbid)); 
(...) 
<div class="row"> 
<div class="tech-spec-element col-xs-20 col-sm-10 col-md-5"> <span title="Episode 1"></span>1 - <?= $resultarray[0]; ?> 
<div></div> 
</div> 
<div class="tech-spec-element col-xs-20 col-sm-10 col-md-5"> <span title="Episode 2"></span>2 - <?= $resultarray[1]; ?> 
<div></div> 
</div> 
<div class="tech-spec-element col-xs-20 col-sm-10 col-md-5"> <span title="Episode 3"></span>3 - <?= $resultarray[2]; ?> 
<div></div> 
</div> 
<div class="tech-spec-element col-xs-20 col-sm-10 col-md-5"> <span title="Episode 4"></span>4 - <?= $resultarray[3]; ?> 
<div></div> 
</div> 
</div> 

我知道file.php被加載罰款,它給了我它的輸出,但是當我嘗試使用$resultarray[0];它只是不起作用,並說它是未定義的。

+0

什麼' $ html-> find'給你? –

+0

基本上匹配的HTML,如在插曲名稱 – Kyubeh2435436

+0

我不認爲問題是數組,因爲當我直接去它的網址工作正常 – Kyubeh2435436

回答

0

您是否在包含file.php的網址?爲什麼?他們在不同的服務器上嗎?如果是這樣,你只接收該文件的輸出,你將無法訪問它的任何變量。要訪問包含腳本的變量,您必須包含它以便在本地執行。

因此,舉例來說,如果兩個腳本是在同一臺服務器上,在同一個目錄,在您的index.php你可以這樣做:

$season = urlencode($imdbid); 
include 'file.php'; 

而在你file.php

if(isset($season)) { 
    $season1 = $season; 
    //...the rest of your code 
+0

是的,但是當我做../網址由於某種原因沒有找到它。就像這是確切的網址,我有不同的目錄名稱,並且是導演的確切數量: Index.php位於http://example.com/1/2/tt1865718/index.php 然後file.php是在http://example.com/1/2/file.php 所以我嘗試做包括('../../ file.php');但顯然它不會被發現。 – Kyubeh2435436

+0

對於你在評論中給出的路徑,你需要遍歷1個級別,而不是兩個級別,所以你應該從'include'中刪除一個'../',它應該是:'include('../ file。 php');' – pisamce

+0

哎呀在file.php我打電話2的目錄是非常其他如此喜歡3所以回去../一旦不會工作 – Kyubeh2435436

相關問題