0

I am using a PHP script to get values from database. This is the json result obtained from the PHP script.

{"subcategories":
 [
  {"category":"Grocery",
   "products":
    [              
     {"name":"Mixture"},
     {"name":"Chocolates"},
     {"name":"Noodles"}
    ]
  },
  {"category":"Evening Strolls",
   "products":
    [
     {"name":"Shawarma"},
     {"name":"Momos"},
     {"name":"Sandwiches"},
     {"name":"Sizzling Kebabs"},
     {"name":"Desi Delights"}
    ]
  }
 ]
}

This is my App and controller.

var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
$http.get("testing.php")
.then(function (response) 
{ 
$scope.result = response.data.subcategories; 
});
});

This is the HTML code

<div class="w3-row">
  <div ng-repeat="x in result" class="w3-col s12 m12 l12">
    <table>
      <tr>
        <td>
          <h3>{{ x.category }}</h3>
        </td>
        <td>
          <h3 style="font-family:'Droid Sans'" id="title_cat"></h3>
        </td>
      </tr>
    </table>
    <div id="sub" class="w3-row">
      <div ng-repeat="y in x.products" class="w3-col s4 m4 l4">
        <table>
          <tr>
            <td>
              <p>{{ y.name }}
                <br />
              </p>
            </td>
          </tr>
          <tr>
            <td>
              <h3 style="font-family:'Droid Sans';text-align:center" id="title_cat">{{ y.name }}</h3>
            </td>
          </tr>
        </table>
      </div>
    </div>
  </div>
</div>

I want to display categories and then the array of products. What should i do?

7
  • Well what's the problem/issue? Commented Mar 13, 2016 at 9:36
  • I am not getting any output in the HTML page Commented Mar 13, 2016 at 9:40
  • can you setup fiddle/plnkr with hardcoded json? Commented Mar 13, 2016 at 9:44
  • did you add ng-app and ng-controller? Commented Mar 13, 2016 at 9:46
  • I think there is some problem in my ng-repeat statement , can u suggest a change for that? Commented Mar 13, 2016 at 9:54

1 Answer 1

1

do you have similar this?

var app = angular.module('myApp', []);
app.controller('mainCtrl', function ($scope) {
  $scope.result = 
    
    {"subcategories":
 [
  {"category":"Grocery",
   "products":
    [              
     {"name":"Mixture"},
     {"name":"Chocolates"},
     {"name":"Noodles"}
    ]
  },
  {"category":"Evening Strolls",
   "products":
    [
     {"name":"Shawarma"},
     {"name":"Momos"},
     {"name":"Sandwiches"},
     {"name":"Sizzling Kebabs"},
     {"name":"Desi Delights"}
    ]
  }
 ]
};
               
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="mainCtrl">
<div class="w3-row">
    <div class="w3-col s12 m12 l12">
        <table>
            <tr>
                <td ng-repeat="x in result.subcategories">
                    <h3>{{ x.category }}</h3>
                    <table>
                        <tr ng-repeat="y in x.products">
                            <td>{{y.name}}</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</div>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Yes thats what i wanted.
you can put this in your php script.

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.