2

得到了以下函数,并且之前没有在任何地方声明 getteampoints 值。试图遵循其他重新声明错误问题,但没有一个有效。我怎样才能解决这个问题?

function getTeamPoints($team)
    {
        $query = mysql_query("SELECT * FROM Team_comp WHERE t_id='$team'");
        $team_array = array();

        while($a = mysql_fetch_array($query))
        {
            $team_array = array(            'home_won'  =>  $a['home_win'],
                                            'home_draw' =>  $a['home_tie'],
                                            'home_lost' =>  $a['home_lost'],
                                            'away_won'  =>  $a['away_win'],
                                            'away_draw' =>  $a['away_tie'],
                                            'away_lost' =>  $a['away_lost'],
                                            'home_games'=>  $a['home_games'],
                                            'away_games'=>  $a['away_games']);
        }

        return $team_array;
    }

    function calculateTeamPoints($team, $type)
    {
        $teamPts = getTeamPoints($team);

        if($type == 'home')
        {
            $homem = $teamPts['home_games'];
            $homew = $teamPts['home_won'];
            $percent = ($homew * 100) / $homem;

            $remaining = $homem - $homew;

            $per = ($remaining * 100) / $homem;
            $percent += $per / 2;
        }
        elseif($type == 'away')
        {
            $homem = $teamPts['away_games'];
            $homew = $teamPts['away_won'];
            $percent = ($homew * 100) / $homem;

            $remaining = $homem - $homew ;

            $per = ($remaining * 100) / $homem;
            $percent += $per / 2;
        }

        return $percent;
    }

    function getpercent($hometeamid, $awayteamid)
    {
        $hometeampts = calculateTeamPoints($hometeamid, 'home');
        $awayteampts = calculateTeamPoints($awayteamid, 'away');


        $homepercent = floor(($hometeampts - $awayteampts) + 50);
        $awaypercent = 100-$homepercent;


    }

    //demo
    getpercent($hometeamid, $awayteamid);
    ?>
4

2 回答 2

3

将函数 getTeamPoints 放入 IF 条件中。

if(!function_exists('getTeamPoints')){ 
    function getTeamPoints()....

}

也不可能多次声明 1 个函数!

如果您声明它超过 1 次,则必须编写不同的名称,如果您只包含该文件超过 1 次(这是错误的..)此 IF 函数存在检查将正常工作。

于 2013-04-10T10:40:20.180 回答
0

看到函数重新声明错误表明您第二次直接或间接地包含了同一个文件。

除了错误消息本身之外,这表明您的应用程序工作流程中存在问题。

最直接的解决方案是找出函数第二次加载的时间和原因,然后修复工作流程。

您可以通过以下方式了解更多信息:

if(!function_exists('getTeamPoints')) {
    throw new Exception('Function getTeamPoints() already declared.');
}

include('getteampoints.php');

抛出异常不仅可以让您稍后捕获它(不可能出现致命错误),而且它还会显示回溯,以便您可以更轻松地找到第二次执行文件的位置(以及原因)。

如果您不确定该结果,您也可以暂时否定条件并在第一次加载函数时抛出异常。

于 2013-04-10T11:00:36.290 回答