使用php和oracle 11g显示一个id的多个数据

时间:2016-05-27 13:37:01

标签: php oracle

这是我查询表格问题中的question_id和问题以及表格答案中的4个多项选择答案。

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

我得到的结果如下所示,

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

实际上我需要我的数据如下所示:

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

任何人都可以帮我解决这个问题吗? 下面是我的PHP代码,

    <?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>

1 个答案:

答案 0 :(得分:0)

我已经重写了显示数据的代码片段

$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;
}
?>