Variable of a JSON undefined

1

good to all, again, JSON em is something Troll because it is easy to deal with the data in this way but I do not understand in this case the reason why it does not work, I have this JavaScript code (not yet complete)

function ver_bookmark(id_sub_categoria){

    console.log(id_sub_categoria);

    if( $("#add").length > 0 ) {

        $.post('.show_bookmarks.php',{id_sub_categoria:id_sub_categoria},
            function(response){
                response = JSON.parse(response);
                console.log(response);

                for(var i in response){
//hago un console.log(response[i]) y lo hace 
//bien pero cuando le aññado en este caso que me muestre el id o algun otro dato
// es donde falla
                    console.log(response[i].id);
                    $('#add').html(
                        $('<div/>',{'id':'my_card','class':'col s12 m6'}).append(
                            $('<div/>',{'class':'card'}).append(
                                $('<div/>',{'class':'card-image'}).append(
                                    $('<img/>',{'src':'ficheros/bookmark/'+response[i].fichero})
                                )
                            )));
                }

            });
    }else{
        console.log("no existe");
    }
}

the data in the PHP file is here

$lista_bookmark =lista_bookmark('',$conn);
$lista_book = [];

for ($i=0; $i < $lista_bookmark['count']; $i++) { 
    if ($_POST['id_sub_categoria'] == $lista_bookmark[$i]['id_book_subcategoria']) {
        $lista_book = array(
            'id' => $lista_bookmark[$i]['id'],
            'id_book_subcategoria'=> $lista_bookmark[$i]['id_book_subcategoria'],
            'url'=> $lista_bookmark[$i]['url'],
            'titulo' => $lista_bookmark[$i]['titulo'],
            'usuario' => $lista_bookmark[$i]['usuario'],
            'password' => $lista_bookmark[$i]['password'],
            'notas' => $lista_bookmark[$i]['notas'],
            'fichero' => $lista_bookmark[$i]['fichero'],
            'favorito' => $lista_bookmark[$i]['favorito']);
    }
}
echo json_encode($lista_book, JSON_FORCE_OBJECT);

in the javaScript code when I put it to show what it collects from the file .PHP does it well, I do console.log(respose) when it is already in JSON format and it does it well, would someone know what my error is ?, it is not the first time I work with this, in fact I already have several doubts resolved in this medium, fixed that it will be some nonsense and I'm sorry but I do not see it because when I have it printed to me response[i]<cualquier parametro> appears as undefined. a greeting. and thanks for the attention :) I leave images of what shows exactly.

    
asked by juank 31.08.2018 в 11:05
source

1 answer

2

As you say, your intention is to have X lists in the variable $lista_book , you are currently always crushing its value in each iteration of your for in case you enter the if .

You should change the allocation of the array $lista_book = array( to $lista_book[] = array( in your PHP, if you do not always have only 1 position in your array and that's why your javascript for does not work as you want, since you're navigating the positions of the array (id, id_book_subcategoria ...) instead of each whole list.

$lista_bookmark =lista_bookmark('',$conn);
$lista_book = [];

for ($i=0; $i < $lista_bookmark['count']; $i++) { 
    if ($_POST['id_sub_categoria'] == $lista_bookmark[$i]['id_book_subcategoria']) {
        $lista_book[] = array( // Así generarás X arrays en tu $lista_book
            'id' => $lista_bookmark[$i]['id'],
            'id_book_subcategoria'=> $lista_bookmark[$i]['id_book_subcategoria'],
            'url'=> $lista_bookmark[$i]['url'],
            'titulo' => $lista_bookmark[$i]['titulo'],
            'usuario' => $lista_bookmark[$i]['usuario'],
            'password' => $lista_bookmark[$i]['password'],
            'notas' => $lista_bookmark[$i]['notas'],
            'fichero' => $lista_bookmark[$i]['fichero'],
            'favorito' => $lista_bookmark[$i]['favorito']);
    }
}
echo json_encode($lista_book, JSON_FORCE_OBJECT);
    
answered by 31.08.2018 / 11:46
source