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
https://forums.phpfreaks.com/topic/297470-get-and-parse-data-from-mysql/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.