Jump to content

FooKelvin

Members
  • Posts

    138
  • Joined

  • Last visited

Everything posted by FooKelvin

  1. foreach ($qdata as $q => $ans) { if ($k > 0) $tableData .= '<tr>'; $tableData .= "<td>$q</td><td>".join('</td><td>',$ans)."</td></tr>"; $dump = var_dump($ans["kelvin foo"]); echo array_sum($dump); ++$k; } My output is: int(2) int(2) int(2) int(2) int(2) int(2) int(2) int(2) int(2) int(2)
  2. Thansk Barand. This is super good explanation. if let say i the answer is a number, i want to total up each individual answer to get the total no. , the good behavior is to use jquery to sum or use php sum array? i not sure whether php allow me to sum array or not.
  3. I don't get your meaning of data belonging to a question. After i try for few hours. it's quite confusing, i keep get Array as my result. Sorry for keep annoying you.
  4. 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 | +-------------------------+-----------------------------------+---------------------------+-------------------------+--------------------------|
  5. Hi, in sqlsrv fetch_row() is sqlsrv_get_field ?
  6. Hi Barand, Thank You. I'm not sure this is correct or not. $stmt2 = sqlsrv_query($conn, $sql2); while (list($c, $q, $n, $a) = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) { if (!isset($data[$c][$q])) { $data[$c][$q] = $newArray; } $data[$c][$q][$n] = $a; // store the answer by name
  7. Is *Barand*. Sorry for the over exited. By the way, it is possible to have category with sub question? for example, +-------------------------+----------------------------------+---------------------------+--------------------------+ | Category | Question | Foo | Ben | +-------------------------+----------------------------------+---------------------------+--------------------------+ | Personal question | How are you? | i'm fine | i'm fine | | | What is Your name? | my name is foo | my name is Ben | +-------------------------+----------------------------------+---------------------------+--------------------------+ | General question | Do you like php? | Yes | Yes | | | PHP Freaks is awesome? | Absolutely | For Sure | +-------------------------+-----------------------------------+---------------------------+------------------------+
  8. Hi Baran, My apologies to you. I overlook one line of the code. is working now..Thank You so much!!!
  9. Hi Baran, Firstly, i would say thank you so much! i have try the code that you provide me. it's look good! But i have still facing one problem. have a look here. $sql2 = "SELECT DISTINCT Employee_Name FROM SubmittedFormView ORDER BY Employee_Name"; $names = array(); $stmt2 = sqlsrv_query($conn, $sql2, $names); while ($row = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) { $names[] = $row['Employee_Name']; } $tableHeads = "<tr><th>Question</th><th>" . join('</th><th>', $names) . "</th></tr>\n"; $newArray = array_fill_keys($names,''); // create blank array $sql = "SELECT DISTINCT question, Employee_Name, answer FROM SubmittedFormView ORDER BY question"; $qarray = $newArray; // new array to store answers to question $currq = ''; // store the current question $tableData = ''; $stmt = sqlsrv_query($conn, $sql); while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { if ($row['question'] != $currq) { // change of question? if ($currq) { // have we a question yet? $tableData .= "<tr><td>$currq</td><td>"; $tableData .= join('</td><td>', $qarray) . "</td></tr>\n"; } $currq = $row['question']; // store new question $qarray = $newArray; // reset the array } } // output the stored array for the final question $tableData .= "<tr><td>$currq</td><td>"; $tableData .= join('</td><td>', $qarray) . "</td></tr>\n"; ?> <table border='1'> <?php echo $tableHeads?> <?php echo $tableData?> </table> Here is the output: http://www.photobox.co.uk/my/photo/full?photo_id=20828350194
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.