Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Parsowanie tekstu na obiekt
Forum PHP.pl > Forum > Przedszkole
voliseq
Witam. Proboje sparsowac dane pobrane z bazy danych tak abym mogl sie odwolywac do kazdego pola indywidualnie. Chce zamienic tekst ktory otrzymuje na rzeczywista tablice/obiekt

Kod
    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();
        echo (json_encode($q));

    }

Oto co zwraca "echo"
Kod
0: {,…}
answers: "[{"text":"bbb","votes":0,"id":0},{"text":"cccc","votes":0,"id":1},{"text":"aaaa","votes":0,"id":2}]"


Prosze o pomoc smile.gif
herbhouse666
Jak generujesz JSON do zapisu w bazie danych?

aby odwoływać się do każdego pola indywidualnie wystarczy zdekodować JSON do standardowego obiektu.
Przykład:
Kod
<?php
    $json = '{"menu": {
   "id": "file",
   "value": "File",
   "popup": {
     "menuitem": [
       {"value": "New", "onclick": "CreateNewDoc()"},
       {"value": "Open", "onclick": "OpenDoc()"},
       {"value": "Close", "onclick": "CloseDoc()"}
     ]
   }
}}';
    $stdObject = json_decode($json);
    //print_r($stdObject);
    $stdObject->menu->id; //przykład
?>
voliseq
This is how i generate data
Kod
$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);


This is my approach get it back from database in the form i want
Kod
    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 print_r result
Kod
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
        )

)

BUT when I want to get $q ->answers[0] it doesn't work, any suggestions ?
Kod
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.