请求特定PHP函数的问题

时间:2018-01-15 01:28:17

标签: javascript php angularjs

我正在为网站使用PHP,AngularJS和HTML。该网站将从数据库请求数据并将其显示在网站上。

我想要做的是如果我在PHP文件中说3个函数并且我在页面上有3个控制器我希望每个控制器能够在PHP文件中请求特定函数并返回结果查询。

以下是我的代码。我测试了查询,我知道它们正在工作。

HTML / AnguarJS

            <div class="row">
            <div class="col-md-8">text</div>
            <div class="col-md-4">
                <div class="row">
                    <div class="panel panel-info">
                        <div class="panel-heading"><b>Scoring Leaders</b></div>
                        <div class="panel-body">
                            <table class="table" ng-controller="leaders">
                                <thead>
                                    <th>#</th>
                                    <th>Name</th>
                                    <th>Points</th>
                                </thead>
                                <tbody class="table table-stripped">
                                    <tr ng-repeat="x in leaders">
                                        <td>{{ x.Num }}</td>
                                        <td>{{ x.Name }}</td>
                                        <td>{{ x.Points}} </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="panel panel-info">
                        <div class="panel-heading"><b>Goals Leaders</b></div>
                        <div class="panel-body">
                            query here
                        </div>

                    </div>
                </div>
                <div class="row">
                    <div class="panel panel-info">
                        <div class="panel-heading"><b>Assists Leaders</b></div>
                        <div class="panel-body">
                            <table class="table" ng-controller="assists">
                                <thead>
                                    <th>#</th>
                                    <th>Name</th>
                                    <th>Points</th>
                                </thead>
                                <tbody class="table table-stripped">
                                    <tr ng-repeat="x in assists">
                                        <td>{{ x.Num }}</td>
                                        <td>{{ x.Name }}</td>
                                        <td>{{ x.Points}} </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
        </div>
    </div>
</div>
<script>
    var app = angular.module('myStats', []);
        app.controller("leaders", function($scope, $http)
        {
            $http({
                method : "GET",
                url : './php/stats.php?action=getPlayerStats()',
            }).then(function mySuccess(response){
                $scope.leaders = response.data.records;
            }, function myError(response) {
                $scope.leaders = response.statusText;
            });
        });
        app.controller("assists", function($scope, $http){
            $http({
                method : "GET",
                url : './php/stats.php?action=getAssists()',
            }).then(function mySuccess(response){
                $scope.assists = response.data.records;
            }, function myError(response) {
                $scope.assists = response.statusText;
            });
        });
</script>

PHP文件

<?php
getPlayerStats();
getAssists();
function getPlayerStats()
{
    include_once("connect.php");

    $connect = connect();
    $seasonID = 21;

    $i = 1;
    $y = 0;
    $result = $connect->query("SELECT TP.TeamPlayerID, TP.Sweater, TP.Position, P.FirstName, P.LastName, 0 + TP.Sweater as SweaterNo
                                          , T.TeamID, T.TeamName
        from `tbl_Teams` T
        inner join `tbl_TeamPlayers` TP on (TP.TeamID=T.TeamID)
        inner join `tbl_Players` P on (P.PlayerID=TP.PlayerID)
        WHERE T.SeasonID=$seasonID
        order by SweaterNo, TP.TeamPlayerID  LIMIT 15");

    $output = "";

    while($rs = $result->fetch_assoc())
    {
        if ($output != "") {$output .= ",";}
        $output .= '{"Num":"' . $i .'",';
        $output .= '"Name":"' . $rs["FirstName"] .' '.$rs["LastName"]. '",';
        $output .= '"Points":"' .$y .'"}';
        $i++;
    }
    $output ='{"records":['.$output.']}';
    error_log($output);
    $connect->close();
    echo($output);
}


function getAssists()
{
    include_once("connect.php");

    $connect = connect();

    $result = $connect->query("SELECT FirstName, LastName FROM `tbl_Players`");

    $output = "";
    $i = 1;
    $y=0;
    while($rs = $result->fetch_assoc()) {
        if ($output != "") { $output .=",";}
        $output .= '{"Num":"' . $i .'",';
        $output .= '"Name":"' . $rs["FirstName"] .' '.$rs["LastName"]. '",';
        $output .= '"Points":"' .$y .'"}';
        $i++;
    }

    $output ='{"records":['.$output.']}';
    error_log($output);
    $connect->close();
    echo($output);
}
?>

我希望Assists控制器能够显示PHP文件中的getAssists函数和Leaders控制器的结果,以显示getPlayerStats函数的结果。

我缺少什么让这项工作?

1 个答案:

答案 0 :(得分:1)

首先,删除网址中的括号;它应该是action=getPlayerStats而不是action=getPlayerStats()

现在,不要在PHP文件中调用这两个函数:

// Incorrect way
getPlayerStats();
getAssists();

相反,找出所要求的行动:

// Correct way
$supported_actions = array('getPlayerStats', 'getAssists');
$action = isset($_GET['action']) ? $_GET['action'] : false;
if (!in_array($action, $supported_actions)) {
    // No valid action found, you might want to send a 404 status here, or something like that
}
$action(); // Execute the action!