2017-07-14 132 views
0

我目前有一個PHP函數,我需要從不同的文件調用來填充一個Javascript變量。我可以很好地填充JavaScript變量。調用PHP函數然而,當我得到一個錯誤消息,指出如何正確調用PHP函數?

使用未定義的常量getEstGrid的 - 假設 'getEstGrid' 在 phpcom/DB/estimatesCom.php </b>標籤上線29

這是我包括具有的功能文件:

<?php include 'phpcom/db/estimatesCom.php';?> 

這裏是我打電話的功能:

<?php echo getEstGrid($resultsE); ?> 

功能代碼:

function getEstGrid($resultsE){//<--This is the variable passed to the function 
    if ($resultsE->num_rows > 0) { //<--- Check to see if the variable is greater than zero 
    $rows = array(); //<--- Create an empty array calls "rows" 
     while ($r = $resultsE->fetch_assoc()){//<--- While the database records are being returned create a variable called "r" and assign DB record. 
      $rows[] = $r; //<-- assign each DB record row to the array. 
     } 
     $data = array('Estimates' => $rows);//<--- Create a variable an assing the array to it. 
    } 
    //header("Content-Type: application/json;charset=utf-8"); 
    echo json_encode($data, true);//<--- Endode the "data" variable to a JSON format. 
    return getEstGrid;//<--- Return the created fucntion. 
} 

如果任何人都可以在這方面幫助我將不勝感激。

+2

函數定義是什麼樣的? – Kai

+0

看起來你沒有一個名爲'getEastGrid()'的函數。 –

+1

那麼guessCom.php中的第29行是什麼? – Liren

回答

0

我會改變這樣的:

echo json_encode($data, true);//<--- Endode the "data" variable to a JSON format. 
return getEstGrid;//<--- Return the created fucntion. 

通過這樣的:

return json_encode($data, true); 

這樣,函數只是返回數據,並且您可以選擇是否對它進行回顯,以及是否使用其他任何方法。

+1

感謝它看起來像這個工作! – Reya276

0

剛剛從你的函數刪除此return語句:

return getEstGrid;//<--- Return the created fucntion. 

你爲什麼用呢?不需要。

此外,如果您在功能中使用echo,則在調用您的函數打印輸出時不需要使用echo

0

您不必爲了在另一個文件中使用它而返回函數。

相反,只返回你的函數需要的值:

當你試圖返回「getEstGrid」,PHP是尋找一個常量與該名稱,犯規存在 回報json_encode(...。

+0

使用<?php echo getEstGrid($ resultsE); ?修復函數返回json_encode($ data,true)後工作? – Reya276