2015-08-14 54 views
0

所以我有2個文件,一個是mainvariables.php & load_more.php。 load_more.php是一個php文件,因爲我對它做了一個ajax調用。 mainvariables.php文件有1個函數。我如何包含一個php文件並獲取另一個php文件中的函數值?

這很奇怪,因爲在我的header.php文件中包含mainvariables.php並獲取返回的值,但是當我使用load_more.php文件嘗試它時,它不起作用。

mainvariables.php

<?php 
function mainVariables(){ 
    global $pdo; 

    //getting school id for switch statment 
    $schoolId = (isset($_GET['id']) ? $_GET['id']:NULL); 
    if ($schoolId) { 
     $sql = 'SELECT * FROM schools WHERE id = :id'; 
     $query = $pdo->prepare($sql); 
     $query->execute(array(':id' => $schoolId)); 
    }else { 
     echo "Not Working"; 
    } 
    //all values of current school 
    return $query->fetchAll(PDO::FETCH_ASSOC); 
} 

?> 

load_more.php

<?php 
// including the config file 
require('config.php'); 
//pdo connct for config.php file 
$pdo = connect(); 
//Include main variables function 
include('php/mainvariables.php'); 
//return of main variables function 
$specificSchool = mainVariables(); 

//School Variables 
$shcoolOfficialId = $specificSchool[0]["id"]; 

switch ($shcoolOfficialId) { 
    case 1: 
     echo "yes"; 
     break; 

    case 2: 
     echo "no"; 
     break; 

    default: 
     echo "There are no more stories to load."; 
     break; 
} 

?> 

剛剛例如我的header.php文件是這樣的:

<?php 
// including the config file 
require('config.php'); 

//pdo connct for config.php file 
$pdo = connect(); 

//Include main variables function 
include('php/mainvariables.php'); 
//return of main variables function 
$specificSchool = mainvariables(); 

$shcoolOfficialId = $specificSchool[0]["id"]; 


?> 

的header.php中正常工作,但load_more.php沒有。我得到錯誤:在mainvariables.php的第15行調用null上的成員函數fetchAll()。這是返回查詢的行。還有它說Undefined變量:也查詢同一行。

感謝您

+0

那麼,你是否檢查過'load_more.php'中有'$ _GET ['id']'變量? –

+0

此外,問題是不mainvariables.php,因爲它返回它應該返回的,但相反,load_more.php導致所有這個錯誤,因爲load_more.php是一個獨立的PHP文件,所以我認爲它不能得到$ _GET ['' ID']像header.php這將導致mainvariables.php – Gianni

+0

錯誤@u_mulder是實際上我需要幫助你說什麼。我怎麼能從mainvariables.php得到這樣的說法。我認爲我搞砸了,因爲mainvariables.php和load_more.php都在它們自己的文件中,所以它在header.php中的工作原因是它是真正的dom而不僅僅是一個php文件。 – Gianni

回答

4

問題是這樣的語句:

return $query->fetchAll(PDO::FETCH_ASSOC);

如果前面if語句評估爲假,$query將永遠不會被定義。如果$schoolId不存在,則返回不同的值,並在使用腳本的其餘部分之前檢查結果。

+0

該死的格式化讓我失望! – Script47

+1

我也會將'return'語句移到'if'塊中,刪除'else'塊和'return null;'在函數結尾。 –

+0

@Sculper好,所以我現在的問題是因爲mainvariables.php是在另一個文件,但包含到header.php它的作品,因爲它能夠得到$ _GET ['id']但如果我把它包含到load_more.php我想它不抓取頁面的$ _GET ['id'],因爲load_more.php是它自己的文件沒有包含在dom中。我如何獲得當前頁面的$ _GET ['id'],但在load_more.php中,包括header.php? – Gianni

2

您在if statement的範圍界定$query,使變量尚未在if statement之外創建的,所以當你運行它,

return $query->fetchAll(PDO::FETCH_ASSOC); 

它會失敗。你爲什麼不試試這個,

function mainVariables(){ 
    global $pdo; 

    $query = null; 

    //getting school id for switch statment 
    $schoolId = (isset($_GET['id']) ? $_GET['id']:NULL); 
    if ($schoolId) { 
     $sql = 'SELECT * FROM schools WHERE id = :id'; 
     $query = $pdo->prepare($sql); 
     $query->execute(array(':id' => $schoolId)); 
    }else { 
     echo "Not Working"; 
    } 
       //all values of current school 
    return $query->fetchAll(PDO::FETCH_ASSOC); 
} 

編輯1

我把結果取在if statement,所以只將在該部分只取,這樣你的錯誤並且您不需要在if statement之外定義$query

function mainVariables(){ 
    global $pdo; 

    //getting school id for switch statment 
    $schoolId = (isset($_GET['id']) ? $_GET['id']:NULL); 
    if ($schoolId) { 
     $sql = 'SELECT * FROM schools WHERE id = :id'; 
     $query = $pdo->prepare($sql); 
     $query->execute(array(':id' => $schoolId)); 

     //all values of current school 
     return $query->fetchAll(PDO::FETCH_ASSOC); 
    }else { 
     echo "Not Working"; 
    } 
} 
+0

@ scipt47所以我嘗試了你發佈的內容,但它仍然沒有解決整體問題,爲什麼它在load_more.php中,如果我包含mainvariables.php可能無法獲得$ _GET ['id'],因爲它不是一個包含任何地方的文件,只是一個獨立的php文件被ajax調用。我如何將在header.php中工作的$ _GET ['id']與load_more.php匹配? – Gianni

+0

此外,問題是不mainvariables.php,因爲它返回它應該返回的東西,而是load_more.php導致所有這些錯誤,因爲load_more.php是一個獨立的PHP文件,所以我認爲它不能得到$ _GET ['' id']像header.php這將導致mainvariables.php – Gianni

+0

@ user4589398中的錯誤,你不能訪問像這樣的函數中的變量。您可能需要着眼於傳遞變量。 – Script47

相關問題