简体   繁体   中英

Show multiple data with one id using php and oracle 11g

This is my query to view question_id and question from table questions and 4 multiple choice answer from table answers.

    select q.question,q.question_id,a.answer from questions q, answers a where q.question_id=a.question_id;

The result that i get is shown below,

    question_id   question   answer
    --------------------------------
       65          1+2=         2
       65          1+2=         4
       65          1+2=         3
       65          1+2=         1
     --------------------------------

Actually i need my data as shown below:

    question_id   question   answer
    --------------------------------
       65          1+2=         2
                                4
                                3
                                1
     --------------------------------

Can anyone help me to fix this? Below is my php code,

    <?php

    $conn = oci_connect("username", "password", "orcl");

$f=1;   
$sql1="select q.question,q.question_id,a.answer from questions q, answers a     where q.question_id=a.question_id and q.question_id='63'";
$result2=oci_parse($conn, $sql1);
oci_execute($result2); 
?> 
<title>e-TUITION</title>
<body>
<br>
    <table width="800" height="100" align="center" border="1" bgcolor="">
     <tr bgcolor = "">
        <td colspan="9" bgcolor=""><div align="center"><font face="Times New     Roman"color=""><strong> Question : 
        </strong></font></div>
        </td>
     </tr>
<tr>
<td width="13"><b>Question_ID</td>
<td width="38"><b>Question</td>
<td width="38"><b>Answer</td>
</tr>


<?php
if ($result2)
{
while ($row = oci_fetch_assoc ($result2))
{
?>
<tr>
<td><?php echo $row ["QUESTION_ID"];?></td>
<td><?php echo $row ["QUESTION"];?></td>
<td><input type="CHECKBOX" name="food"  value="ANSWER"> <?php echo $row ["ANSWER"];?></td>
</tr>   
<?php } ?>
<?php } ?>
</table>

I've rewritten the snippet of code that is displaying the data

$empty_if_same = array("QUESTION_ID", "QUESTION");
while ($row = oci_fetch_assoc ($result2))
{
    $row1 = $row;
    if (isset($prev_row))
    {
        foreach($empty_if_same as $key)
        {
            if ($prev_row[$key] == $row[$key])
            {
                $row1[$key] = "";
            }
        }
    }
    ?>
    <tr>
        <td><?php echo $row1["QUESTION_ID"];?></td>
        <td><?php echo $row1["QUESTION"];?></td>
        <td><input type="CHECKBOX" name="food"  value="ANSWER"> <?php echo $row1["ANSWER"];?></td>
    </tr>
<?php
    $prev_row = $row;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM