使用Angular和PHP RestFull Api查询多个MySql表

时间:2016-03-18 16:55:00

标签: javascript php mysql angularjs slim

我正在尝试获取每个帖子的平台列表。

我有三张MySql表 1)帖子
2)平台
3)platform_xrf

我的平台表有两列:

[id]  
[name]

我的platform_xrf表有3列:

[id]  
[postid]  
[platformid]

我使用php Slim作为我的RESTapi

以下是我的控制器中的代码来获取我的帖子。

app.controller('postsCtrl', function ($scope, $log, $http, $timeout, Data, $uibModal) {
    Data.get('posts').then(function(data){
        $scope.posts = data.data;
        $scope.currentPage = 1; //current page
        $scope.filteredItems = $scope.posts.length; //Initially for no filter  
        $scope.totalItems = $scope.posts.length;
        $scope.list_pages = [
                {
                    id: '5',
                    name: '5'
                }, {
                    id: '10',
                    name: '10'
                }, {
                    id: '20',
                    name: '20'
                }, {
                    id: '50',
                    name: '50'
                }, {
                    id: '100',
                    name: '100'
                }
            ];
        $scope.maxSize = 5;
    });
    $scope.setPage = function(pageNo) {
        $scope.currentPage = pageNo;
    };
    $scope.filter = function() {
        $timeout(function() { 
            $scope.filteredItems = $scope.filtered.length;
        }, 10);
    };
    $scope.sort_by = function(predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };
    $scope.changePostStatus = function(post){
        post.approved = (post.approved=="1" ? "0" : "1");
        Data.put("posts/"+post.id,{approved:post.approved}).then(function (result) {
            Data.toast2(result);
        });
    };
    $scope.changePostAnnounce = function(post){
        post.announce = (post.announce=="1" ? "0" : "1");
        Data.put("posts/"+post.id,{announce:post.announce}).then(function (result) {
            Data.toast2(result);
        });
    };

    $scope.trashPost = function(post){
        //$log.log(post);
        if(confirm("Are you sure to remove the post")){
            Data.delete("posts/"+post.id).then(function(result){
                $scope.posts = _.without($scope.posts, _.findWhere($scope.posts, {id:post.id}));
            });
        }
    };
    $scope.open = function (p,size) {
        var modalInstance = $uibModal.open({
          templateUrl: 'views/postsEdit.html',
          controller: 'postsEditCtrl',
          size: size,
          resolve: {
            item: function () {
              return p;
            }
          }
        });
        modalInstance.result.then(function(selectedObject) {
            if(selectedObject.save == "insert"){
                $scope.posts.push(selectedObject);
                $scope.posts = $filter('orderBy')($scope.posts, 'id', 'reverse');
            }else if(selectedObject.save == "update"){
                p.title = selectedObject.title;
                // p.price = selectedObject.price;
                // p.stock = selectedObject.stock;
                // p.packing = selectedObject.packing;
            }
        });
    };

});

这是我对函数的调用:

// Posts
$app->get('/posts', function() { 
  global $db;
  $rows = $db->select3("posts","id,publishdate,guid,platform,title,redirect,approved,announce,img,description,dynamic_content",array());
  echoResponse(200, $rows);
});

这是我的查询功能:

function select3($table, $columns, $where){
    try{
        $a = array();
        $w = "";
        foreach ($where as $key => $value) {
            $w .= " and " .$key. " like :".$key;
            $a[":".$key] = $value;
        }
        $stmt = $this->db->prepare("select ".$columns." from ".$table." where 1=1 and trash != 1 ". $w);
        $stmt->execute($a);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if(count($rows)<=0){
            $response["status"] = "warning";
            $response["message"] = "No data found.";
        }else{
            $response["status"] = "success";
            $response["message"] = "Data selected from database";
        }
            $response["data"] = $rows;
    }catch(PDOException $e){
        $response["status"] = "error";
        $response["message"] = 'Select Failed: ' .$e->getMessage();
        $response["data"] = null;
    }
    return $response;
}

我正在尝试获取与每个帖子相关联的所有平台,并将其显示在我的帖子行中。非常感谢任何帮助。

更新的 我尝试将其添加到我的控制器,但我得到了一个无限循环

$scope.getPlatforms = function(id){
     Data.get('platforms/'+id).then(function(data){
          $log.log(data);
     });
};

更新2 进一步说明:当向用户显示一行时,有一个Platform列。我需要在此列中显示与帖子关联的所有平台作为列表。即facebook,twitter

0 个答案:

没有答案