Jump to content

php for each help


FooKelvin

Recommended Posts

Hello,

 

I currently have two tables. question and question_option table.

 

each question might have more than one question_option.

Here is my excel sheet.

 

https://onedrive.live.com/edit.aspx?cid=b28ee3cd15a16520&page=view&resid=B28EE3CD15A16520!1937&parId=B28EE3CD15A16520!1932&app=Excel

 

So, here is my php code:

$conn = sqlsrv_connect($serverName, $connectionInfo);

        $data = array();
        $trows = '';
        $sql = " SELECT q.[question_id], q.[q_text],qopt.[option_label],qopt.[opt_text] FROM [RSA].[dbo].[question] q LEFT JOIN [RSA].[dbo].[question_options] qopt ON q.[question_id]=qopt.[question_id] ORDER BY [question_id] ASC";
        $stmt = sqlsrv_query($conn, $sql);
        if ($stmt === false) {
            die(print_r(sqlsrv_errors(), true));
        }
        while (list($qid, $qtxt, $qoptlabel, $qopttxt) = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
        if (!isset($data[$qid])) {
            $data[$qid]['question_id'] = $qid;
            $data[$qid]['q_text'] = $qtxt;
            //$data[$qid]['option_label'] = $qoptlabel;
            //$data[$qid]['opt_text'] = $qopttxt;

            }
        }
        
        foreach ($data as $qid => $qdata) {
        $trows .= "<tr><td class='cn'>{$qdata['question_id']}</td>"
                . "<td>{$qdata['q_text']}</td>";
                //. "<td>{$cdata['option_label']}</td>"
                //. "<td>{$cdata['opt_text']}</td>";
            foreach ($qdata as $option) {
                $trows .=  "<td class='dt'>{$option['$qopttxt']}</td>";
            }
        $trows .= "</tr>\n";
        }
?>
<table border='1'>
    <?php echo $trows?>
</table>

I able to for each all the question, but i have stuck over the option value there. The option value most of them is check boxes.

Edited by FooKelvin
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.