Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
in Q2A Core by

I hope someone can answer me this question. This will substantially answer my other question (https://www.question2answer.org/qa/87862/how-to-display-avatar-when-you-have-userid).

Let's say we have an array of USERIDs after doing some MySQL query

SELECT userid from ^posts WHERE ....

So the $userids will have values like 13, 29, 173, 544.

How do you turn this array into a horizontal list of Usernames like:

Xavier, antmant125, Huckleberry Finn, Elton_Jim

Q2A version: 1.8.4
Can you modify the original query to return more data than just the user IDs? That should allow you to query all the data you need in one go.
In ^posts table there is only userid that is related to user information.
But you could join it with the users and optionally the blobs table to get that information: "SELECT ... FROM ^posts p JOIN ^users u ON p.userid = u.userid JOIN ^blobs b ON p.userid = b.userid WHERE ..."
That's a good approach. Thanks.

Well, I actually create a new function that returns the avatar (HTML form with its image and link) from userid. So, I can display the avatar whenever there is a userid. I can use it in every plugin or widget.

1 Answer

0 votes

For future reference,

First, the query to get userid must return a flat array, not a nested one. You must use qa_db_read_all_values(qa_db_query_sub()). In the future 1.9 version, the core developers may change this.

To list the items, you can use this to list the first 3 values, the rest can be expressed as "and # others",

list($first, $second, $third) = $userids;

echo $first. ' '. $second.' '.$third. 'have answered this question';

That's why you  need a flat array, not nested.

To return the username, use this Q2A function, which is missing from official DOCS.

  • qa_userid_to_handle($first)
  • qa_userid_to_handle($second)
  • qa_userid_to_handle($third)

Similarly, to display avatars, you do MySQL queries, or exploit the Q2A functions to return avatars from $handle (ie, username).