Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7747061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:26:14+00:00 2026-06-01T10:26:14+00:00

How can I modify my existing JQuery code to loop through a JSON array

  • 0

How can I modify my existing JQuery code to loop through a JSON array of arrays? For example, when PHP returns a JSON result from MySQL database containing more than 1 row.

Here is my code. It only works with a single row result.

$(function(){

    $('#btn_select_account').live('click', function() {

    // URL...
    $.getJSON('api.php?',
    // Parameters...
    { call: 'select_account', p0: 'suchislife801' },

     function(result){

        // For each item inside array...
        $.each(result, function(index, value) { 

          // Append to this html element
          $('#output').append(index + ': ' + value + '<br />').fadeIn(300); 

        });

     });

  });

});

The following PHP code….

  function qry_select_account($pk_account) {

  // Global variables
  Global $db_host, $db_user, $db_pass, $db_name;  

  // Connect to database server
  $dbc = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
  // Select target database
  mysql_select_db($db_name) or die(mysql_error());

  // suchislife801 <--- Selects account information
  // Run query
  $sql_qry = mysql_query("SELECT * FROM tblaccount
                         WHERE pk_account = '$pk_account'") or die(mysql_error());

  // SQL Criteria  AND acc_confirmed = 'y' AND acc_locked = 'n'

  // Fetch table row for this user
  $row = mysql_fetch_row($sql_qry);

  print json_encode($row);

  mysql_free_result($sql_qry);

  // Close Connection
  mysql_close($dbc);

  }

Generates the following response:

["20","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:38","n","0"]

I am trying to change it so that it works with the following PHP code…

  function qry_select_last5_entries_for_user($ent_user) {

  // Global variables
  Global $db_host, $db_user, $db_pass, $db_name;  

  // Connect to database server
  $dbc = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
  // Select target database
  mysql_select_db($db_name) or die(mysql_error());

  // suchislife801 <--- Selects last 5 entries for user
  // Run query
  $sql_qry = mysql_query("SELECT * FROM tblentry 
                          WHERE ent_user = '$ent_user' ORDER BY ent_date DESC , ent_time DESC LIMIT 5") or die(mysql_error());

  // Fetch table rows for this user
  while ($row = mysql_fetch_array($sql_qry, MYSQL_NUM)) {

  print json_encode($row);

  } 

  mysql_free_result($sql_qry);

  // Close Connection
  mysql_close($dbc);

  }















["20","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:38","n","0"]["19","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:37","n","0"]["18","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:36","n","0"]["17","1","suchislife801","Ugly","My first entry title.","Body of my first entry.","2012-04-03","15:06:35","n","0"]["15","1","suchislife801","Lazy","My first entry title.","Body of my first entry.","2012-04-03","15:06:34","n","0"]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:26:15+00:00Added an answer on June 1, 2026 at 10:26 am

    In your php, modify your loop to create an array, and then echo the json_encode result of that array.

    The output should look like this:

    [["20","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:38","n","0"],["19","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:37","n","0"],["18","1","suchislife801","Happy","My first entry title.","Body of my first entry.","2012-04-03","15:06:36","n","0"],["17","1","suchislife801","Ugly","My first entry title.","Body of my first entry.","2012-04-03","15:06:35","n","0"],["15","1","suchislife801","Lazy","My first entry title.","Body of my first entry.","2012-04-03","15:06:34","n","0"]]
    

    Then, in jQuery, you would output it like this:

    var outHtml = "";
    $.each(result, function(index, value) { 
        outHtml += "Entry " + index + ":<br />";
        outHtml += $.map(value,function(i,value){
            return i + ": " + value + "<br />";
        }).join("");
        outHtml += "<br /><br />";
    });
    
    $("#output").html(outHtml);
    

    Demo: http://jsfiddle.net/R8wvn/

    Edit:
    php to get desired result(not tested):

    var $out = array();
    // Fetch table rows for this user
    while ($row = mysql_fetch_array($sql_qry, MYSQL_NUM)) {
      array_push($out,$row);
    } 
    print json_encode($out);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
I'm hoping someone can modify my code below to show me exactly how to
I'm trying to understand code that I bought so I can modify it. In
My problem is I have an existing table that I can not modify that
I want to know I can dynamically modify an existing Crystal Report (using C#
I have an existing JComboBox object. I can modify many of its properties using
I'm using node-jquery , and I'd like to use it to modify an existing
How can I modify an existing *.gem file? I want to modify a Rakefile
How can I modify this existing preg_replace to only allow numbers? function __cleanData($c) {
I'm trying to modify an existing Pages document from within my application using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.