开发者

Trouble with function in PHP not being recognized

开发者 https://www.devze.com 2023-04-10 18:34 出处:网络
I have a few simple php scripts that I am writing for a website and I am having an issue that I can\'t figure out the cause of.

I have a few simple php scripts that I am writing for a website and I am having an issue that I can't figure out the cause of.

Error:

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

which I imagine is the reason this also happens:

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

This is the code of the index page:

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

This is the GetVotePosts function which is in the includes file

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;
}

this is line23 that is the source of the error:

foreach ($votePosts as $post)

Any idea what is causing this problem.


You are including file before you are have declared variable. try like this

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

精彩评论

暂无评论...
验证码 换一张
取 消