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

  • Home
  • SEARCH
  • 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 6542937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:17:10+00:00 2026-05-25T11:17:10+00:00

Im having trouble formatting my json array in jquery and I cant seem to

  • 0

Im having trouble formatting my json array in jquery and I cant seem to find a suitable answer.

I have a mysql database of users that is outputting thier user id and name in json format.

$result = mysql_query("SELECT id,info FROM users WHERE info like '$keyword%'");
while($row=mysql_fetch_assoc($result))
  $return[] = $row;

echo json_encode($return);

Resulting in this output:

[{"id":"1","info":"Mike"},{"id":"2","info":"Sally"},{"id":"3","info":"Ben"}]

my jquery success looks like this:

$.each(data, function()
{
  $.each(this, function(k, v) 
  {
    $('.results').append('<li id='+k+'>'+v+'</li>');
  });
});

resulting in this output:

<li id='id'>1</li>
<li id='info'>Mike</li>
<li id='id'>2</li>
<li id='info'>Sally</li>
etc...

But I would like it to read:

<li id='1'>Mike</li>
<li id='2'>Sally</li>
<li id='3'>Ben</li>

Ive been trying to wrap my brain around how to get this working but cant come up with anything, does anyone have any suggestions?

  • 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-05-25T11:17:11+00:00Added an answer on May 25, 2026 at 11:17 am

    Try:

    $.each(data, function() {
        $('.results').append('<li id="' + this.id + '">' + this.info + '</li>');  
    });
    

    However, DOM manipulation is slow, and whilst this won’t be noticable for a small number of results, if you’re adding a lot of <li>‘s at the same time, you’ll find the following code more efficient:

    var str = '';
    $.each(data, function() {
        str += '<li id="' + this.id + '">' + this.info + '</li>';  
    });
    $('.results').append(str);
    

    Much easier than you thought, eh? 🙂

    Edit: A brief description of why this works, and yours didn’t:

    What you’ve got is an array of objects. When you call $.each over data, it iterates through the array. Inside the callback function you pass to each, this is equal to the current element in the array; which is the object. So we access the id and the info attributes of the object (this.id and this.info), and build a <li> out of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble on formatting my json. Basically I have this script structure,
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I having trouble in dividing the HTML frames. I have been using the following
I am having trouble with IE7. I have a header, which is an IMG.
I am designing the database for a shopping cart and I am having trouble
Having trouble with AR 2.3.5, e.g.: users = User.all( :select => u.id, c.user_id, :from
I just started learning Java and I'm having trouble formatting string. In the problem
I am having trouble properly formatting CSS to display in IE the way I
I'm having trouble with the number formatting in Apex 3.2.1. NLS_TERRITORY is set to
having trouble coming up with search parameters for this issue, so I can't find

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.