Reputation: 300
I'm having a problem printing the array response in my site in codeigniter. I'm trying to loop an angular response array in the select option. While I printed the response array the array seems to be in proper format but the looping is not happening.
<body>
<div ng-app="app" ng-controller="ctrl">
<select ng-model="selectedname" ng-options="x.id for x in names"></select>
<table>
<tr ng-repeat="x in names">
<td>{{ x.id }}</td>
</tr>
</table>
<!--<p>You selected : {{ selectedname.id }}</p><p>with color {{selectedname.id }}</p>-->
<p>{{ names }}</p>
</div>
</body>
<script>
var app = angular.module('app', []);
app.controller('ctrl', function($scope, $http) {
$http.get("<?php echo base_url().'ap/load_data' ?>").then(function(response) {
$scope.names = response.data.rec;
});
});
</script>
public function load_data(){
$this->load->model('record');
$data = $this->record->load_all();
$out = '';
foreach($data as $d){
if($out){ $out .= ","; }
$out .= "{'id': '".$d['id']."',";
$out .= "'subname': '".$d['subname']."'}";
}
//$test = "{'rec' : [$out]}";
$test = "[$out]";
echo json_encode(array('rec' => $test));
}
As you can see the below {{ names }}
prints the following array
[
{'id': '1','subname': 'melbourne'},
{'id': '2','subname': 'sydney'},
{'id': '3','subname': 'mumbai'},
{'id': '4','subname': 'pune'},
{'id': '5','subname': 'tokyo'},
{'id': '6','subname': 'osaka'}
]
But the array is not getting used to create the select picker with the values. I'm new to angularjs so the solution might be simple but I'm not getting it. Any help is appreciated. Thanks.
Upvotes: 0
Views: 221
Reputation: 1077
Just Try
$out =array();
foreach($data as $d){
array_push($out, $d);
}
echo json_encode(array('rec' => $out));
Upvotes: 2