tags:

views:

52

answers:

3

I query in a database and loop the result using while and heres the sample data retrieve:

echo $db->f("FirstName")."===".$db->f("Question")."=".$db->f("Answer")."<br>";

Michael===Q2=allergy
Michael===Q2=Hives
Michael===Q6=A lot
Michael===Q8_A=Daktacort
Michael===Q1=Itch
Michael===Q5=Smoke
Michael===Q8_A=Diprogenta
Christian===Q1=Stuffy
Christian===Q6=A lot
Christian===Q1=Clear
Christian===Q5=Pollen

How can I group them according to name and to their Q value? I want something like this result:

Name          Q1           Q2                         ..... so on and so fort.
Michael      Itch          Hives, Allergy
Christian    Stuffy 
A: 

Use an array rather than a loop of discrete calls to a function. Then you could have something like:

$data[0]["FirstName"] = "Michael";
$data[0]["Q1"] = "Itch";
$data[0]["Q2"] = "Hives, Allergy";
... etc

You can loop through this array when you want to display the data and present a table with discrete rows of associated information.

EAMann
A: 

You can use MySQL's GROUP_CONCAT() function for that.

But keep the limitations of group_concat_max_len in mind.

VolkerK
+1  A: 

You could use GROUP_CONCAT to do this in MySQL

SELECT FirstName,Question,GROUP_CONCAT(Answer) AS Answers
FROM <tables>
GROUP BY FirstName,Question
Paul Dixon
I cannot group them by name because answers are coming from different table also the Qvalues. Grouping them can only retrieve one answer and Q Value.
christian
In that case please provide the table structure and some example data in your question (preferably the results of `SHOW CREATE TABLE` and some `INSERT INTO ...` lines).
VolkerK