请求特定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?

查看全部
dsaf32131
dsaf32131
2018/01/15 01:28
  • php
  • angularjs
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复