Jump to content

Foreach no result


FooKelvin

Recommended Posts

I have a questions table and feedback table. I able to foreach the question header but not the feedback.

My code as above:

<?php
    $sql2 = "SELECT DISTINCT[question_id],[q_text] FROM [question]";
    $ques = array();
    $stmt2 = sqlsrv_query($conn, $sql2, $ques);
    while ($row = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
        $ques[$row['question_id']] = $row['q_text'];
    }

    $sql = "SELECT [question_id], [Expr3],[Expr2]
        FROM [feedback]
        ORDER BY [Expr2] ASC";
    $data = array();

    $stmt = sqlsrv_query($conn, $sql);
    while (list($qid, $a, $eid) = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
        if (!isset($data[$qid][$eid])) {
            $data[$qid][$eid] = $newArray2;
        }
        $data[$qid][$eid][] = $a;
    }
    ?>
    <div class="container">
           <?php
            // produce table header
            echo "<table border='1' id='table2excel'>\n";
            echo "<tr><th>Employee ID</th>";

// loop over array of questions for the header
            foreach ($ques as $question) {
                echo "<th>$question</th>";
            }
            echo "</tr>\n";
            foreach ($data as $qid => $question) {
                                foreach ($question as $question => $replies) {
                                    echo "<tr><td>$question</td>";
                foreach (array_keys($ques) as $q_id) {
                        echo "<td>";
                        echo $replies[$q_id];
                        echo "</td>";
                }
                echo "</tr>\n";
                echo "</tr>\n";
            }
            }
            echo "</table>\n";

Questions Table:

 

question_id q_text 1 Do you Like Red Color? 02A Do you Like Yellow Color? 02B Do you Like Blue Color? 3 Do you Like Green Color? 4 What color you like among them? 5 Do you Like Purple Color? 6 Do you Like Gold Color? 7 Do you Like Rose Gold Color? 8 Do you Like Black Color? 9 Do you Like Orange Color?

The question 4, might be multiple answer.

 

Feedback Table:

 

question_id Expr2 Expr3 1 EMP1001 Yes 02A EMP1001 No 4 EMP1001 Red 4 EMP1001 Yellow 4 EMP1001 Blue 5 EMP1001 No 6 EMP1001 No 3 EMP1001 Yes 02B EMP1001 Yes 7 EMP1001 Yes 8 EMP1001 Yes 9 EMP1001 Yes 1 EMP1002 Yes 02A EMP1002 No 4 EMP1002 Red 5 EMP1002 No 6 EMP1002 Yes 3 EMP1002 Yes 02B EMP1002 Yes 7 EMP1002 No 8 EMP1002   9 EMP1002 Yes

 

 

Result:

 

Employee ID Do you Like Red Color? Do you Like Yellow Color? Do you Like Blue Color? Do you Like Green Color? What color you like among them? Do you Like Purple Color? Do you Like Gold Color? Do you Like Rose Gold Color? Do you Like Black Color? Do you Like Orange Color? EMP1001                     EMP1002                     EMP1001                     EMP1002                     EMP1001                     EMP1002                     EMP1001                     EMP1002                     EMP1001                     EMP1002                     EMP1001                     EMP1002                     EMP1001                     EMP1002                     EMP1001                     EMP1002                     EMP1001                     EMP1002                     EMP1001                     EMP1002                    

 

Expected Result:

 

Employee ID Do you Like Red Color? Do you Like Yellow Color? Do you Like Blue Color? Do you Like Green Color? What color you like among them? Do you Like Purple Color? Do you Like Gold Color? Do you Like Rose Gold Color? Do you Like Black Color? Do you Like Orange Color? EMP1001 Yes No Yes Yes Red No No Yes Yes Yes EMP1001 Yes No Yes Yes Yellow No No Yes Yes Yes EMP1001 Yes No Yes Yes Blue No No Yes Yes Yes EMP1002 Yes No Yes Yes Red No Yes No   Yes

 

The question id no.4 will allow multiple choice to select, hence there will be multiple feedback for that particular question.

foreach.txt

Link to comment
Share on other sites

Hi mac, yes...it is similar questions, but due to 

echo implode('<hr>',$replies[$q_id]); // note: this works correctly if there is a single reply, the result is just the single array element by itself

when i export to excel, it automatically merge the table in excel, which is good.. some how i face problem when i Vloopup in excel file, so i am thinking to split them when it has multiple feedback.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.