dsaf32131 2018-01-15 01:28
浏览 52
已采纳

请求特定PHP函数的问题

I am using PHP, AngularJS and HTML for a website. The website is to request data from the DB and display it on the website.

What I am trying to do is if I have lets say 3 functions in a PHP file and I have 3 controllers on a page I would like each controller to be able to request a specific function in the PHP file and return the results of the query.

Below is my code. I have test the queries and I know that they are working.

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 FILE

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

I would like the Assists controller to be able to display the results from the getAssists function in the PHP file and the Leaders controller to display the results from the getPlayerStats function.

What am I missing to make this work?

  • 写回答

1条回答 默认 最新

  • doushi2902 2018-01-15 01:38
    关注

    First of, remove the parentheses in your URLs; it should be action=getPlayerStats and not action=getPlayerStats()

    Now, don't call both functions in your PHP File:

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

    Instead, find out what's the action being requested:

    // 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!
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?