Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    .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.

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #4
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    thanks, how would I implement $.each into my code?

    thanks

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    jarv (01-29-2011)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •