1

I'm wanting to load list of names from SQL DB. I then want to click the name and have corresponding image load. The first image should load when page loads. Names are loading. Image is not loading and ng-click does not load image. Made similar code work using JSON, but not able to make work using DB. Cats is the name of table. ImagePath and CatName are the fields in DB. New to PHP. Thank you.

<!doctype html>
<html>
    <head>
        <title>Load Images from DB</title>
        <script src="angular.min.js"></script>
    </head>

    <body ng-app='myapp'>

        <div ng-controller="userCtrl">


            <div ng-repeat="user in Cats">
                <h3 ng-click = "$scope.selectCat(cat)">{{user.CatName}}</h3>
            </div>

            <img  ng-src="{{$scope.selectedCat.ImagePath}}"/>

            <!-- Script -->
            <script>
                var fetch = angular.module('myapp', []);
                fetch.controller('userCtrl', ['$scope', '$http', function ($scope, $http) {

                    $http({
                    method: 'get',
                    url: 'getData.php'
                    }).then(function successCallback(response) {
                        $scope.Cats = response.data;

                        $scope.selectCat=selectCat;

                         $scope.selectedCat=$scope.Cats[0];


                        function selectCat(pos) {
                           $scope.selectedCat = pos;

                        };
                    });
                }]);
            </script>
        </div>
    </body>
</html>

Here is PHP code

    include 'config.php';

    $sel = mysqli_query($con,"select * from Cats");
    $data = array();

    while ($row = mysqli_fetch_array($sel)) {
        $data[] = array("CatName"=>$row['CatName'],"ImagePath"=>$row['ImagePath']);
    }

    echo json_encode($data);

3 Answers 3

1

Change you controller script by moving the selectCat outside of promise. Also assign it scope.

fetch.controller('userCtrl', ['$scope', '$http',

function ($scope, $http) { 
$scope.selectCat = selectCat;
function selectCat(pos) { $scope.selectedCat = pos; }; }); 
}
$http({ method: 'get', url: 'getData.php' }).then(function successCallback(response) { $scope.Cats = response.data;  $scope.selectedCat=$scope.Cats[0]; }]);
Sign up to request clarification or add additional context in comments.

6 Comments

The js controller would not recognize the PHP code. Anyway,PHP code is in separate file imported with 'get' method. Do you have a code snippet of how this would work?
The php will be executed server side and render the data as json array. Anyways i will update my answer.
Thx for reply, but still getting same result. The names load, but not image.
Change cat to user such that <h3 ng-click = "$scope.selectCat(user)">{{user.CatName}}</h3> </div>
Getting closer. Now I see place for image but image is empty. If I MOVE the image to the ng-repeat and display like <img ng-src ={{user.ImagePath}}/>, all of the images display. So something is not working with either <img ng-src="{{$scope.selectedCat.ImagePath}}"/> or $scope.selectedCat=$scope.Cats[0] from the controller. Thank you much for helping with this.
|
0

The php will be executed server side and render the data as json array. Anyways i will update my answer.

Comments

0
I finally got it to work by changing the HTML to: 

<div ng-controller = "MainController as **vm**">

  <div ng-repeat = "cat in Cats">

    <h3 ng-click = "vm.selectCat(cat)">{{cat.CatName}}</h3>

    </div>
    <hr>
<h3>{{vm.selectedCat.CatName}}</h3>
  <img ng-src="{{vm.selectedCat.ImagePath}}"</img>


I then changed the controller to : 

angular.module('myApp').controller('MainController', ['$scope','$http',function($scope,$http) {
**var vm = this;**

vm.selectCat=selectCat;

function selectCat(pos) {
  vm.selectedCat = pos;

};

$http({
            method: 'get',
            url: 'getData.php'
            }).then(function successCallback(response) {
                **$scope.Cats = response.data;
                vm.selectedCat=$scope.Cats[0];**
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.