Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2011, 11:02 PM   PM User | #1
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
Cool Show all Array? .each()?

hi,

I am sooo close to getting my project done,

I can now output a town name, using Jquery to output mySQL as JSON

the only problem I have now... In my code I have [16] which = 'Andover'

Is there a way to output my whole array? can I use .each()? if so, where?

thanks

here is my code
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {

	$.getJSON('json.php', function(data){
	
		$("#content").html(data[16].Town);
	
	});

});
</script>
</head>
<body>

<div id="content"></div>
</body>
</html>
http://www.mypubspace.com/dashtest/newjson.html
jarv is offline   Reply With Quote
Old 01-27-2011, 11:15 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
.each() will iterate through jQuery objects ... to iterate simple arrays, you can use something like this:
Code:
$.getJSON('json.php', function(data){
   var myhtml = '<ul>';
   for(i=0; i<data.length; i++) {
      myhtml += '<li>' + data[i].Town + '</li>';
   }
   myhtml += '</ul>';
   $("#content").html(myhtml);
});
This will give you a complete unordered list with the towns as list items. But you can compose the string in any way you want.
devnull69 is offline   Reply With Quote
Old 01-27-2011, 11:36 PM   PM User | #3
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by devnull69 View Post
.each() will iterate through jQuery objects ... to iterate simple arrays, you can use something like this:
Code:
$.getJSON('json.php', function(data){
   var myhtml = '<ul>';
   for(i=0; i<data.length; i++) {
      myhtml += '<li>' + data[i].Town + '</li>';
   }
   myhtml += '</ul>';
   $("#content").html(myhtml);
});
This will give you a complete unordered list with the towns as list items. But you can compose the string in any way you want.
or use $.each which works with both objects and arrays,

http://api.jquery.com/jQuery.each/

best regards
oesxyl is offline   Reply With Quote
Old 01-27-2011, 11:43 PM   PM User | #4
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
thanks, how would I implement $.each into my code?

thanks
jarv is offline   Reply With Quote
Old 01-27-2011, 11:59 PM   PM User | #5
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by jarv View Post
thanks, how would I implement $.each into my code?

thanks
using devnull69 is something like this but is not tested:
Code:
$.getJSON('json.php', function(data){
   var myhtml = '<ul>';
   $.each(data, function(ind, val){
      myhtml += '<li>' + val.Town + '</li>';
   }
   myhtml += '</ul>';
   $("#content").html(myhtml);
});
best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
jarv (01-29-2011)
Reply

Bookmarks

Tags
array, jquery, json

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:32 AM.


Advertisement
Log in to turn off these ads.