2011-10-05 117 views
0

我有一些簡單的PHP腳本,我正在寫一個網站,我遇到了一個問題,我找不出原因。PHP中的函數無法識別

錯誤:

Notice: Undefined variable: votePosts in C:\wamp\www\fec\displaytable.php on line 23 

我想象的是,這也發生的原因:

Warning: Invalid argument supplied for foreach() in C:\wamp\www\fec\displaytable.php on line 2 

這是索引頁的代碼:

<html> 
<?php 
include ("includes.php"); 
include ("displaytable.php"); 
$votePosts = GetVotePosts(); 
?> 
</html> 

這是GetVotePosts功能在包含文件中

function GetVotePosts($inId=null) 
{ 
if(isset($_POST['sort'])) 
{ 
    $sort = $_POST['sort']; 
} 
else 
{ 
    $sort = "district ASC"; 
} 
$query = mysql_query("SELECT reports.comm_name, reports.fec_id, reports.form_type, reports.file_id, reports.id, reports.line101, reports.line81, reports.line22, reports.line16, reports.line15, reports.line13a, reports.line11c, reports.line11aiii, campaigns.incumbency, campaigns.party, campaigns.candidate, campaigns.district FROM reports,campaigns WHERE reports.fec_id = campaigns.fecid ORDER BY " . $sort); 
$postArray = array(); 
while ($row = mysql_fetch_assoc($query)) 
{ 
    $myPost = new VotePost($row['comm_name'], $row['fec_id'], $row['form_type'], $row['file_id'], $row['id'], $row['line101'], $row['line81'], $row['line22'], $row['line16'], $row['line15'], $row['line13a'], $row['line11c'], $row['line11aiii'], $row['incumbency'], $row['party'], $row['candidate'], $row['district']); 
     array_push($postArray, $myPost); 
} 
return $postArray; 
} 

這是line23那是錯誤的來源:

foreach ($votePosts as $post) 

任何想法是什麼原因造成了這個問題。

+0

'$ votePosts'定義在哪裏?你嘗試過var_dump()嗎? –

+2

錯誤顯示在displaytables.php中,而不是您的includes.php。我的猜測是你需要重新排序你的語句爲'include('includes.php'); $ votePosts = GetVotePosts(); include('displaytable.php');' – jprofitt

+0

$ votePosts在索引 – jimstandard

回答

0

在包含聲明變量之前包含文件。 請試試這個

<html> 
<?php 
include ("includes.php"); 
$votePosts = GetVotePosts(); 
include ("displaytable.php"); 
?> 
</html> 
+0

displaytable.php您將需要在全局空間中聲明$ votePosts: global $ votePosts; – Mike