Стандартный способ использовать HTTP-запрос для вызова нескольких функций внутри PHP, который возвращает объект - PullRequest
0 голосов
/ 11 октября 2019

Я пытался вызвать функцию из php, возвращающую объект. Но это не сработало для меня. В чем проблема? это правильный подход ? А также я хочу иметь несколько функций внутри Student.php . Как мне этого добиться?

studentService.js

app.service('StudentService', function ($http) {
    let urlBase = "app/model/student/student.php/"; // path of student.php`

    return {
        student_fortes: function (user_id) {
            return $http({
                url: urlBase + "student_fortes",
                method: "POST",
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: {
                    'user_id': user_id
                }
            });
        },
        student_events: function (user_id) {
            return $http({
                url: urlBase + "student_events()",
                method: "POST",
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: {
                    'user_id': user_id
                }
            });
        }
    }
});

У меня есть PHP внутри этого вызывает функции из StudentService через $http request

student.php

<?php
include_once("../conn.php");
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$req = convert();

class studentData {

    // I want to call this function `student_fortes()` right below and get the object echo-ed by it
    // This is triggered by the `student_fortes http request

    public function student_fortes() {
        global $req;
        global $conn;
        $data = array();
        $user_id = $req->user_id;

        try {
            $sql = "query";

            $stmt = $conn->prepare($sql);
            $stmt->bindParam("user_id", $user_id);

            if(!$stmt->execute()) $data['message'] = false;
            else {
                $data['rows'] = $stmt->rowCount();
                $data['message'] = $stmt->fetchAll();
            }
        } catch(PDOException $e) {
            $data['message'] = false;
        }

        echo json_encode($data); <- gets all the rows from the database
    }

    // I want to call this function `student_events()` right below and get the object echo-ed by it
    // This is triggered by the `student_events http request

    public function student_events() {
        global $req;
        global $conn;
        $data = array();
        $user_id = $req->user_id;

        try {
            $sql = "query...";
            $stmt = $conn->prepare($sql);
            $stmt->bindParam("user_id", $user_id);

            if (!$stmt->execute()) $data['message'] = false;
            else {
                $results = $stmt->fetchAll();
                $data['rows'] = $stmt->rowCount();
                $data['message'] = $results;
            }
        } catch (PDOException $e) {
            $data['message'] = false;
        }

        echo json_encode($data);
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...