Hi Barand,
Thank you.
I play around with added another column which is "Points".
Each question have their own points.
Am i doing the correct thing?
<?php
/*****************************************************
* first you need to get the column headings
* and create array to store responses for
* each question
******************************************************/
$sql = "SELECT DISTINCT name
FROM kelvin
ORDER BY name";
$names = array();
$res = $db->query($sql);
while ($row = $res->fetch_assoc()) {
$names[] = $row['name'];
}
$tableHeads = "<tr><th>Category</th><th>Question</th><th>" . join('</th><th>', $names) . "</th></tr>\n";
$newArray = array_fill_keys($names,''); // create blank array
/*****************************************************
* then you process the table data
* storing the answers for each name in the array.
* indexed by categor, question, name
******************************************************/
$sql = "SELECT category, question, Points, name, answer
FROM kelvin
ORDER BY category, question";
$res = $db->query($sql);
while (list($c,$q,$p,$n,$a) = $res->fetch_row()) {
if (!isset($data[$c][$q][$p])) {
$data[$c][$q][$p] = $newArray;
}
$data[$c][$q][$p][$n] = $a; // store the answer by name
}
Expected Output:
+-------------------------+----------------------------------+---------------------------+--------------------------+--------------------------+
| Category | Question | Points | Foo | Ben |
+-------------------------+----------------------------------+---------------------------+--------------------------+--------------------------+
| Personal question | How are you? | 5 | i'm fine | i'm fine |
| | What is Your name? | 4 | my name is foo | my name is Ben |
+-------------------------+----------------------------------+---------------------------+--------------------------+--------------------------|
| General question | Do you like php? | 5 | Yes | Yes |
| | PHP Freaks is awesome? | 5 | Absolutely | For Sure |
+-------------------------+-----------------------------------+---------------------------+-------------------------+--------------------------|