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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:26:07+00:00 2026-05-28T02:26:07+00:00

I am performing a query which returns a list of comments as shown below:

  • 0

I am performing a query which returns a list of comments as shown below:

$sQuery = "
        select cp_comments.*,users.user_login, users.user_url, users.display_name, users.ID as avatar, cp_comments.id as replies

         from ".$wpdb->prefix."cp_comments cp_comments
            left join ".$wpdb->prefix."users users on users.ID=cp_comments.uid
            where songid='$id'
            order by cp_comments.id asc
    ";
    $result1 = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());

I then loop through the results and encode a json array like so:

/*
 * Output
 */
$output = array(

    "comments" => array()

);


while ( $aRow = mysql_fetch_array( $result1, MYSQL_ASSOC ) )
{

    $row = array();

    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {

        if ( $aColumns[$i] == "playtime" )
        {
            /* Special output formatting for 'playtime' column */
            $row[$aColumns[$i]] = ($aRow[ $aColumns[$i] ]) / $duration * 100;
        }           
        else if ( $aColumns[$i] == "avatar" )
        {
            /* Special output to render Avatar by user id */
            $row[$aColumns[$i]] = commentplayer_get_user_avatar($aRow[ $aColumns[$i] ]);
        }
        else if ( $aColumns[$i] != ' ' )
        {
            /* General output */
            $row[$aColumns[$i]] = $aRow[ $aColumns[$i] ];
        }
    }

    $output['comments'][] = $row;

}

echo json_encode($output);

This produces a JSON response as shown:-

{ "comments" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
        "body" : "More tests....",
        "display_name" : "admin",
        "id" : "26",
        "playtime" : 36.206896551699998,
        "posttime" : "2011-10-08 11:11:55",
        "cid" : "26",
        "songid" : "30",
        "uid" : "1",
        "user_login" : "admin",
        "user_url" : "http://www.songbanc.com/members/admin/"

      },
      { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
        "body" : "test comment",
        "display_name" : "admin",
        "id" : "70",
        "playtime" : 29.597701149399999,
        "posttime" : "2011-10-13 15:51:01",
        "cid" : "70",
        "songid" : "30",
        "uid" : "1",
        "user_login" : "admin",
        "user_url" : "http://www.songbanc.com/members/admin/"
      },
      { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
        "body" : "Another genius comment....",
        "display_name" : "admin",
        "id" : "71",
        "playtime" : 48.8505747126,
        "posttime" : "2011-10-13 15:55:38",
        "cid" : "71",
        "songid" : "30",
        "uid" : "1",
        "user_login" : "admin",
        "user_url" : "http://www.songbanc.com/members/admin/"
      }
    ] }

However I need to retrieve the ‘replies’ to each comment (which are stored in a seperate table and nest them within the same JSON object.

So that the final encoded JSON is output like so:-

{ "comments" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
        "body" : "More tests....",
        "display_name" : "admin",
        "id" : "26",
        "playtime" : 36.206896551699998,
        "posttime" : "2011-10-08 11:11:55",
        "cid" : "26",
        "songid" : "30",
        "uid" : "1",
        "user_login" : "admin",
        "user_url" : "http://www.songbanc.com/members/admin/"
        "cid": "1",
        "replies" : [ { "cid" : "26",
                        "body" : "test reply",
                        "posttime" : "2011-10-08 11:11:55"
                      }]

      },

I can’t seem to work out how to nest the results of a second mysql query into each row (assuming any replies exist at all)

The query I would like to run to return the replies for a given comment (linked as ‘cid’ in the two tables) is:-

$sql = "select cp_replies.*,users.user_login, users.user_url, users.display_name, users.ID as avatar 

    from ".$wpdb->prefix."cp_replies cp_replies
                left join ".$wpdb->prefix."users users on users.ID=cp_replies.uid
                where cp_replies.cid= $cid
                order by cp_replies.id asc";

$result2 = mysql_query( $sql, $gaSql['link'] ) or die(mysql_error());

With the variable $cid needing to be passed dynamically to the query depending on which ‘cid’ of each row for th first query. (I hope that makes sense).

I tried creating a seperate function containing the query, passed it the ‘cid’ as variable but kept getting ‘null’ returned when I tried to retrieve the array.

I really am stuck here guys and truly hope someone can help me.

EDIt:

Having made the changes suggested by StuR, it is clear that I am getting closer but it’s still not as intended. My JSON (assuming I have implemented his suggestions correctly, is now as follows:-

{ "comments" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
        "body" : "comment at 0:48",
        "display_name" : "admin",
        "id" : "2",
        "playtime" : 17.977528089900002,
        "posttime" : "2011-09-28 14:38:41",
        "songid" : "24",
        "uid" : "1",
        "user_login" : "admin",
        "user_url" : "http://www.songbanc.com/members/admin/"
      },
      { "replies" : { "body" : "haha reply",
            "cid" : "2",
            "id" : "1",
            "posttime" : "2011-09-28 15:14:56",
            "uid" : "1",
            "user_login" : "admin"
          } },
      { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
        "body" : "asdasd",
        "display_name" : "admin",
        "id" : "3",
        "playtime" : 0.74906367041199995,
        "posttime" : "2011-09-28 14:43:11",
        "songid" : "24",
        "uid" : "1",
        "user_login" : "admin",
        "user_url" : "http://www.songbanc.com/members/admin/"
      },
      { "replies" : { "body" : "haha reply",
            "cid" : "2",
            "id" : "1",
            "posttime" : "2011-09-28 15:14:56",
            "uid" : "1",
            "user_login" : "admin"
          } },
      { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
        "body" : "test",
        "display_name" : "admin",
        "id" : "10",
        "playtime" : 36.329588014999999,
        "posttime" : "2011-10-06 14:15:12",
        "songid" : "24",
        "uid" : "1",
        "user_login" : "admin",
        "user_url" : "http://www.songbanc.com/members/admin/"
      },
      { "replies" : { "body" : "haha reply",
            "cid" : "2",
            "id" : "1",
            "posttime" : "2011-09-28 15:14:56",
            "uid" : "1",
            "user_login" : "admin"
          } }
    ] }

Any builds on this?

  • 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-28T02:26:08+00:00Added an answer on May 28, 2026 at 2:26 am
    $output['comments'][] = $row;
    

    This is creating a nested array under ‘comments’ for each row it loops, so before encoding it to JSON your array structure would look something like:

    $output['comments'][]['id'] = 26;
    $output['comments'][]['songid'] = 30;
    

    So you want to put your replies into:

    $output['comments'][]['replies'] = $replies;
    

    What I would do is put your second query within your while loop, and then do this:

    while ( $replies = mysql_fetch_array( $result2, MYSQL_ASSOC ) )
    {
        $output['comments'][$cid]['replies'][] = $replies;
    }
    

    And change in your first while:

    $output['comments'][] = $row;
    

    to

    $output['comments'][$cid] = $row;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if there is a good-performing query to select distinct dates (ignoring
While performing a conditional DELETE operation in one of my InnoDB tables, which apparently
I am building a fairly large statistics system, which needs to allow users to
I'm performing a query that is looking for values in one table that are
I have a SQL database and i am performing the following query on it.
I'd like to query a mongo collection for records which either don't have a
I've got a SQL query which is trying to find all the Neighbourhoods in
I am trying to run a query: INSERT INTO `ProductState` (`ProductId`, `ChangedOn`, `State`) SELECT
I'm performing the following MySQL query inside phpmyadmin. The found_rows() function is not returning
I have a TIBquery object on one of the forms, which performing a heavy

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.