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 8316333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:20:22+00:00 2026-06-08T21:20:22+00:00

I am working on a comment system, and I am trying to build a

  • 0

I am working on a comment system, and I am trying to build a function that queries and displays 10 newest comments from database using ajax.

The problem I am having is that, I just can’t figure out how to display multiple rows using ajax and json_encode.

When I display multiple rows with php, I use .=, which is so simple, but with ajax and json_encode, I can only display a single result with my knowledge so far.

Below is the pure PHP version of my code to display multiple rows

public function display_comment(){
    $query = "SELECT * FROM 'comment' WHERE 'user_id' = $user_id ORDER BY 'save_time' DESC LIMIT 10";
    $result = $database->query($query);

    foreach($result as $row){
        $user_id = $row['user_id'];
        $user_name = $row['user_name'];
        $comment = $row['comment'];

        $all_comments .= '<div id="' . $user_id . '"><span>' . $user_name . '</span><span>' . $comment . '</span></div>';
    }
}

Using ajax and json_encode, this is how I return a single result

public function display_comment(){
    $query = "SELECT * FROM 'comment' WHERE 'user_id' = $user_id ORDER BY 'save_time' DESC LIMIT 10";
    $result = $database->query($query);

    foreach($result as $row){
        $user_id = $row['user_id'];
        $user_name = $row['user_name'];
        $comment = $row['comment'];

        $json_array = array("user_id" => $user_id, "user_name" => $user_name, "comment" => $comment);
        header('Content-type:application/json');
        exit(json_encode($json_array)); // I am using exit instead of echo, because somehow if I use echo, it returns entire html page. 
    }
}

jQuery part

$(document).on("click","#view_comment",function(e){
    e.preventDefault();
    $("#view_comment").text("loading..");
    var view_comment = $(this).val(); // Don't worry about this

    $.ajax({
        type:"post",
        data:{"view_comment":view_comment},
        dataType:"json",
        success:function(data){
            $('#ajax_comment').html('<div id="' + data.user_id + '"><span>' + data.user_name + '</span><span>' + data.comment + '</span></div>');
        },
        error:function(data){
            // Error code
        }
    });
});

How should I change my code in order to display multiple rows using json_encode?

Thank you so much in advance!

  • 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-08T21:20:23+00:00Added an answer on June 8, 2026 at 9:20 pm

    The reason why you’re only able to view a single row is because you’re existing the script after the first iteration of the while() loop:

    exit(json_encode($json_array));
    

    Change the php to:

    public function display_comment(){
      $query = "SELECT * FROM 'comment' WHERE 'user_id' = $user_id ORDER BY 'save_time' DESC LIMIT 10";
      $result = $database->query($query);
      $results = array();
      foreach($result as $row){
        $user_id = $row['user_id'];
        $user_name = $row['user_name'];
        $comment = $row['comment'];
    
        $results[] = array("user_id" => $user_id, "user_name" => $user_name, "comment" => $comment);
    
      }
      header('Content-type:application/json');
      exit(json_encode($results)); 
    }
    

    Javascript

    $(document).on("click","#view_comment",function(e){
        e.preventDefault();
        $("#view_comment").text("loading..");
        var view_comment = $(this).val(); // Don't worry about this
    
        $.ajax({
            type:"post",
            data:{"view_comment":view_comment},
            dataType:"json",
            success:function(data){
                var html = '', comment;
                for(var i = 0; i < data.length; i++) {
                    comment = data[i];
                    html += '<div id="' + comment.user_id + '"><span>' + comment.user_name + '</span><span>' + comment.comment + '</span></div>'
                }
                $('#ajax_comment').html(html);
            },
            error:function(data){
                // Error code
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PHP 4.3.9, Apache/2.0.52 I'm trying to get a login system working that
Basically i'm trying to build a comment system. The user looks at a photo
I am trying to do a comment system with jquery and ajax, sometimes I
I am making an inline comment reply system It's mostly working except that I
I'm working on a picture select/crop program and using this code below: http://www.androidworks.com/crop_large_photos_with_android/comment-page-1#comment-969 As
I am currently working on a project that lets users post comments with jquery
I am working on a module that allows users to post comments on a
Am currently working on an application that requires users to submit posts and comments
Working with an undisclosed API, I found a function that can set the number
OK - so am working on a system that uses a custom datepicker control

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.