2017-07-14 130 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