voliseq Posted July 25, 2015 Share Posted July 25, 2015 Hi. I can not manage to parse the data I get from database into object sa I can reach each property. This is how i generate data for database $scope.entry = {'votes': 0}; $scope.formSubmit = function(entry) { var numItems = $('.answer').length; var i = 0; $scope.entry.answers = []; $('.answer').each(function(){ $scope.entry.answers[i] = {}; $scope.entry.answers[i] = { 'text': $(this).val(), 'votes': 0, 'id': i }; i++; }); $scope.entry.answers = JSON.stringify($scope.entry.answers); Here is how I fetch it from database public function update() { $postId = $this->input->post('postId'); $this->db->select('answers'); $this->db->from('posts'); $this->db->where('id', $postId); $q = $this->db->get()->result(); $q = json_decode(json_encode($q[0])); print_r (json_decode($q->answers)); } Here is result of "print_r" Array ( [0] => stdClass Object ( [text] => gfdgfd [votes] => 0 [id] => 0 ) [1] => stdClass Object ( [text] => cccc [votes] => 0 [id] => 1 ) [2] => stdClass Object ( [text] => dfgdfg [votes] => 0 [id] => 2 ) ) And here the error I get when I try to print $q->answers[0] A PHP Error was encountered Severity: Notice Message: Undefined offset: 0 What I want to be able to do: $q->answers[0]->id; Maybe it will be helpful, result of echo json_encode($q); 0: {id: "452", question: "aaaaaa",…} answers: "[{"text":"bbb","votes":0,"id":0},{"text":"cccc","votes":0,"id":1},{"text":"aaaa","votes":0,"id":2}]" id: "452" posttime: "1437692222628" question: "aaaaaa" votes: "0" Please help Quote Link to comment Share on other sites More sharing options...
blacknight Posted July 26, 2015 Share Posted July 26, 2015 $q->answers is in json format you need to decode it as well in order for php to use it Quote Link to comment Share on other sites More sharing options...
maxxd Posted July 27, 2015 Share Posted July 27, 2015 Why are you using json at all? You encode the query results, immediately decode the results, then decode the results again when you print. If you just get the result set, what does that look like? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.