Alan
Alan

Reputation: 279

problems displaying my array via JSON object

Hi I am having a problems displaying my array via JSON object. I passed two variables to PHP which returns an array. I then wish to loop through the array and append the result to a div

The PHP works fine as I have tested this before adding the JQuery. When I use google chrome to inspect the console, I dump out data which displays as [] not an object, is this correct?

the contents of the array do not have a key, only a collection of list items with an image path for example

<li><img src="'.$image_path.'"</li>

I encode the array back to the listener,

echo json_encode($result);

code for JQuery

$.post('Look_Controller.php', {look: look, account: account}, function(data) {                              
    $.each(data, function(i, item) {
        $('#carousel-ul').empty();
        content = item;
       $(content).appendTo('#carousel-ul');
});
    }, "json");

How do I append each individual result to the div (#carousel-ul)?,

I have also tried

content = item.this;
content = (this).item;
content = $(this).item;

I am not sure if it because the console.log(data) displays [] instead of object?

Hope someone can advise!

Thanks

Upvotes: 0

Views: 119

Answers (2)

mamadrood
mamadrood

Reputation: 757

What happen when you try this ?

$.post('Look_Controller.php', {look: look, account: account}, function(data) {                              
$('#carousel-ul').empty();
console.log(data);
$.each(data, function(i, item) {
    console.log(item);
    content = item;
    // If "carousel-ul" is a <ul> tag uncomment the next line
    // content = $('<li/>').html(content);
   $(content).appendTo('#carousel-ul');
});
}, "json");

Upvotes: 1

Terry
Terry

Reputation: 14219

[] is an empty array. If that's what's being shown you have no data.

So it's probably not coming back from the server. Check the Network tab in the Chrome debugger and see what your response looks like.

Upvotes: 0

Related Questions