SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display JSON results

    I have a JSON function that works fine but I want to know how to display all of the results without using a for loop my code is below:

    Code:
    function newarticlescallback(rtndata) 
    { 
    	$('#pagetitle').html("New Articles");
    	var data="";
    	for(var j=0;j<9;j++)
    	{
     
             data = data + "<li><a href='#' onclick=\"History.pushState({state:null},'article,"+rtndata[j].id+"','article'); return false;\">" + rtndata[j].title + "</a></li>";
    	
    	}
    	$('#testing').html(data);
    }

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You can't, it has to be a loop. Any particular reason you don't want a loop?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know it has to be a loop but i am looking for a loop that can go through the JSON without knowing how much results are in the JSON object for instances if i want to do select all from my db i would like my loop to handle that query without modification

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skelleex View Post
    I know it has to be a loop but i am looking for a loop that can go through the JSON without knowing how much results are in the JSON object for instances if i want to do select all from my db i would like my loop to handle that query without modification
    It's less expensive to send all of the required data at once, than to loop around multiple request/wait processes.

    Send a request to your server letting your server know that you want to get all of the relevant data, and then handle that data when it's send to the client page.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    It's less expensive to send all of the required data at once, than to loop around multiple request/wait processes.

    Send a request to your server letting your server know that you want to get all of the relevant data, and then handle that data when it's send to the client page.
    Hi paul,

    rtndata has all the data within it within the for loop I just add html to each datarow, but I know the amount of rows of data I have. But what happens if I have more or less rows of data? I want a function that handles any number of rows of data. Like a while EOF or something to that extent.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skelleex View Post
    Hi paul,

    rtndata has all the data within it within the for loop I just add html to each datarow, but I know the amount of rows of data I have. But what happens if I have more or less rows of data?
    You can use rtndata.length in the for loop instead, so that it loops over whatever number of items are in the array.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    really thanks a lot paul


Bookmarks

Posting Permissions

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