使用数据库中的数据填充数组(Angular + PHP / MySQL)

用户名

我正在使用一个简单的待办应用程序,该应用程序使用Angular,然后使用PHP / MySQL作为后端。

我现在有一个简单的应用程序,可以在其中运行,可以添加新的待办事项,并使用simple每天将“已完成的百分比”添加到数据库中$http post

但是现在我要做的是$scope.historicalDailyPercentages用数据库中的数据填充数组。

在脚本开始时,我将对象初始化为:

$scope.historicalDailyPercentages = []; //TODO, this should be initialised with data from the database.

我知道我需要在其中有某种$ http get循环,以检查数据并填充对象,但是我不清楚如何开始于此。

整个goalzy.js脚本在下面供参考。提前致谢!

angular.module('goalzy', [])

.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
}])

.controller('TodoController', ['$scope', '$http', function($scope, $http) {

    $scope.todos = [];
    $scope.historicalDailyPercentages = []; //TODO, this should be initialised with data from the database.

    $scope.addTodo = function() {
        if ($scope.todoText != "") {
            if ($scope.todos.length < 3) {
                $scope.todos.push({
                    text: $scope.todoText,
                    done: false
                });
                $scope.todoText = '';
                //Save to DB
            } else {
                alert("You can only have 3 todos per day!");
                $scope.todoText = '';
            }
        } else {
            alert("you must write something");
        }
    };

    $scope.remaining = function() {
        var count = 0;
        angular.forEach($scope.todos, function(todo) {
            count += todo.done ? 0 : 1;
        });
        return count;
    };

    $scope.percentComplete = function() {
        var countCompleted = 0;
        angular.forEach($scope.todos, function(todo) {
            countCompleted += todo.done ? 1 : 0; //Simply calculates how many tasks have been completed
            console.log(countCompleted);
        });
        var totalCount = $scope.todos.length;
        var percentComplete = countCompleted / totalCount * 100;
        return percentComplete;
    }

    $scope.finaliseDay = function(percentComplete) {
        alert("You're finalising this day with a percentage of: " + percentComplete);
        var today = new Date();
        var alreadyPresent = $scope.historicalDailyPercentages.some(function(item) {
            return item.date.getFullYear() === today.getFullYear() &&
                item.date.getMonth() === today.getMonth() &&
                item.date.getDate() === today.getDate();
        });

        //Confirm that nothing has alreayd been posted for today
        if (!alreadyPresent) {

            // Simple POST request example (passing data)
            $http.post('/postDailyPercentage.php', {
                user_id: 1,
                percent: percentComplete,
                date: today
            }).
            success(function(data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                if (data) {

                    $scope.historicalDailyPercentages.push({
                        user_id: 1,
                        percent: percentComplete,
                        date: today
                    });

                } else {
                    alert("Something went wrong" + data);
                }

            }).
            error(function(data, status, headers, config) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                console.log("Post failure");
            });
        } else {
            alert("You're all set for today - see you tomorrow!");
        }

        //console.log($scope.historicalDailyPercentages);
    }
}]);
兰德斯汀

要用$ http.get填充该对象,您可以按如下操作:

function getHistoricalDataSuccess(data) {
  $scope.historicalDailyPercentages = data;
}

function getHistoricalDataError(error) {
  //handle the error
}

$http.get('path/to/api')
.success(getHistoricalDataSuccess)
.error(getHistoricalDataError);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用php从mysql数据库中循环多维数组

来自分类Dev

如何从Mysql数据库创建和填充php数组

来自分类Dev

使用MySQL数据库填充数据表

来自分类Dev

如何使用 php 脚本从 MySQL 数据库中获取数据到嵌套的 JSON 数组中?

来自分类Dev

使用PHP从MySQL数据库中获取记录以填充下拉列表

来自分类Dev

尝试使用 php ajax mysql 填充数据表

来自分类Dev

如何使用php从mysql数据库中获取数据

来自分类Dev

使用php从mysql数据库中获取数据

来自分类Dev

无法使用PHP在MySQL数据库中插入数据

来自分类Dev

使用PHP将数组插入MySQL数据库?

来自分类Dev

无法使用PHP,Mysql和Angular.js检查数据库中的重复数据

来自分类Dev

在没有PHP的情况下,使用mysql数据库中的数据填充HTML DropDown列表

来自分类Dev

防止使用php将空数组记录提交到MYSQL数据库中

来自分类Dev

使用PHP将数组保存在mysql数据库中(json_encode)

来自分类Dev

如何使用 php 显示 MySQL 数据库中的十进制和日期数组

来自分类Dev

在php中显示mysql数据库

来自分类Dev

使用PHP在MySQL数据库中自动加密?

来自分类Dev

如何使用PHP在MySQL数据库中输入日期

来自分类Dev

使用PHP来计数MySQL数据库中的行?

来自分类Dev

使用PHP在MySQL数据库中搜索多行

来自分类Dev

无法使用php更新mysql数据库中的值

来自分类Dev

php mysql:从数据库中选择并填充表格

来自分类Dev

PHP MySQL数据库

来自分类Dev

如何将PHP数组保存到MySQL数据库,以便可以搜索数据库中的数组?

来自分类Dev

PHP:在表中显示mysql数据库中的数据

来自分类Dev

如何使用Node.js设置从MySQL数据库填充数据的JSON文件的名称?

来自分类Dev

使用PHP和MySQL从数据库中获取数据时,无法避免重复数据

来自分类Dev

如何从MySQL数据库在PHP中的多维数组中添加数组?

来自分类Dev

数据未插入php的mysql数据库中

Related 相关文章

  1. 1

    使用php从mysql数据库中循环多维数组

  2. 2

    如何从Mysql数据库创建和填充php数组

  3. 3

    使用MySQL数据库填充数据表

  4. 4

    如何使用 php 脚本从 MySQL 数据库中获取数据到嵌套的 JSON 数组中?

  5. 5

    使用PHP从MySQL数据库中获取记录以填充下拉列表

  6. 6

    尝试使用 php ajax mysql 填充数据表

  7. 7

    如何使用php从mysql数据库中获取数据

  8. 8

    使用php从mysql数据库中获取数据

  9. 9

    无法使用PHP在MySQL数据库中插入数据

  10. 10

    使用PHP将数组插入MySQL数据库?

  11. 11

    无法使用PHP,Mysql和Angular.js检查数据库中的重复数据

  12. 12

    在没有PHP的情况下,使用mysql数据库中的数据填充HTML DropDown列表

  13. 13

    防止使用php将空数组记录提交到MYSQL数据库中

  14. 14

    使用PHP将数组保存在mysql数据库中(json_encode)

  15. 15

    如何使用 php 显示 MySQL 数据库中的十进制和日期数组

  16. 16

    在php中显示mysql数据库

  17. 17

    使用PHP在MySQL数据库中自动加密?

  18. 18

    如何使用PHP在MySQL数据库中输入日期

  19. 19

    使用PHP来计数MySQL数据库中的行?

  20. 20

    使用PHP在MySQL数据库中搜索多行

  21. 21

    无法使用php更新mysql数据库中的值

  22. 22

    php mysql:从数据库中选择并填充表格

  23. 23

    PHP MySQL数据库

  24. 24

    如何将PHP数组保存到MySQL数据库,以便可以搜索数据库中的数组?

  25. 25

    PHP:在表中显示mysql数据库中的数据

  26. 26

    如何使用Node.js设置从MySQL数据库填充数据的JSON文件的名称?

  27. 27

    使用PHP和MySQL从数据库中获取数据时,无法避免重复数据

  28. 28

    如何从MySQL数据库在PHP中的多维数组中添加数组?

  29. 29

    数据未插入php的mysql数据库中

热门标签

归档