Jump to content

Get and parse data from MySQL


voliseq

Recommended Posts

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.