0

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

asked Apr 2, 2012 at 20:47
4
  • It is better to move the append/empty outside .each function. Commented Apr 2, 2012 at 20:51
  • I have moved this outside the .each loop as suggested, thanks SKS but still nothing just [] console output! Commented Apr 2, 2012 at 20:57
  • $('#carousel-ul') exists? What happens if you console.log($('#carousel-ul')); ? Commented Apr 2, 2012 at 20:58
  • ...And what if you console.log(item)? Commented Apr 2, 2012 at 20:58

2 Answers 2

1

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");
answered Apr 2, 2012 at 21:24
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice marmadrood I will have a look at this
0

[] 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.

answered Apr 2, 2012 at 21:08

1 Comment

yes I had a variable in the wrong place when I copied the code across from testing, Thanks for your advice,

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.