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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化