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