当节ID更改时,在单独的div中显示查询结果

时间:2012-06-18 11:44:32

标签: php while-loop

我有一个查询

mysql_select_db($database_auditing, $auditing);
$query_sections3 = sprintf("SELECT tblanswer.questionid AS answerqid,
                                tblanswer.answerid AS answerid, 
                                tblanswer.answer AS answer, 
                                tblquestion.questionid AS questionid, 
                                tblquestion.questiontext AS questiontext, 
                                tblquestion.questionid AS quesid, 
                                tblquestion.questiontypeid AS questiontype, 
                                tblquestion.sectionid AS sectionid, 
                                tblscore.score1 AS score1, 
                                tblscore.score2 AS score2, 
                                tblscore.score3 AS score3, 
                                tblscore.score3 AS score3, 
                                tblscore.score4 AS score4, 
                                tblhelptext.helptext AS help, 
                                tblsection.sectionname AS sectionname 
                            FROM tblanswer 
                            LEFT JOIN tblquestion ON tblanswer.questionid = tblquestion.questionid 
                            LEFT JOIN tblscore ON tblquestion.questionid = tblscore.questionid 
                            LEFT JOIN tblhelptext ON tblquestion.questionid = tblhelptext.questionid 
                            LEFT JOIN tblsection ON tblquestion.sectionid=tblsection.sectionid 
                            WHERE tblanswer.auditid=%s 
                            ORDER BY tblquestion.sectionid",  
                        GetSQLValueString($_SESSION['auditid'], "int"));

$sections3 = mysql_query($query_sections3, $auditing) or die(mysql_error());

$totalRows_sections3 = mysql_num_rows($sections3);

从数据库中提取相关问题并构建与网站相关的问卷。

每个问题都与某个部分有关。

所以我正在尝试做的事情(并且悲惨地失败)是将上面的查询结果显示在我用于调查问卷的手风琴的相关部分,而不必在内部使用嵌套查询每个div(这导致我输入给定结果进入数据库的问题)。

感谢所有为this post做出贡献的人。

我设法让手风琴在每个问题的自己选项卡中输出,但我想要达到的目的是在同一个标​​签中获取所有相关问题。

这是手风琴代码:

<?php $prevsub='';
$purpleronnie=0;
 while ($row_sections3=mysql_fetch_assoc($sections3)) {

     if ($row_sections3['sectionid'] !=$prevsub) {
         ?>

<div class="AccordionPanel">
<div class="AccordionPanelTab"><?php echo $row_sections3['sectionname'];?></div>
<div class="AccordionPanelContent">

<br />
<h5>Please answer the questions below to the best of your ability. For help, hover over the help icon, help text will appear in the box to the right of the screen.</h5>
<?php }?>

<table class="table2" align="center">
<tr>

<th><?php echo $row_sections3['questiontext'];?> <input type="hidden" name="qnumber[]" value="<?php echo $row_sections3['questionid'];?>" />
</th>
<td>
<input type="text" name="answerid[<?php echo $purpleronnie;?>]" value="<?php echo  $row_sections3['answerid'];?>" size="1" />

<?php if ($row_sections3['questiontype']=="1") { ?> 
<input type="text" size="25" name="answer[<?php echo $purpleronnie;?>]" value="<?php echo $row_sections3['answer'];?>" />

<?php } ?>

</table>    

</div>
</div>
<?php $purpleronnie++;
 if ($prevsub!=''&&$prevsub!=$row_sections3['sectionid'])
 $prevsub=$row_sections3['sectionid']; }?>

有关如何更改上述内容以使每次分段ID增加时手风琴div更改而不是每个问题的任何建议吗?

非常感谢 戴夫

1 个答案:

答案 0 :(得分:0)

如果你能够通过section id对东西进行排序,那么你可以先构建一个由section_id划分的数据数组,然后循环遍历它并构建你的div等。下面的代码基于你的示例代码并且不是最优雅的,但它会做你想要的数据,假设数据进入。如果这不是你需要的100%,它至少会给你一个开始的地方。另外,请注意结束标记。您错过了在您发布的示例代码中关闭表列和行。

$purpleronnie=0;
$data = array();

// sort your questions into "buckets" first
while ($row_sections3=mysql_fetch_assoc($sections3)) {
    // store that row of data in the correct sectioned off container
    $data[$row_sections3['sectionid']][] = $row_sections3;
}

// do nothing if we have no data! 
if(empty($data)) exit('woops! no results in this section!');

// now loop through these results and build your divs
echo '<div class="AccordionPanel">';

foreach($data as $sectionid => $rows){
    // grab the section name of the first row to use for the main section tabs
    $section_name = $rows[0]['sectionname'];
    echo 
        '<div class="AccordionPanelTab">'.$section_name.'</div>
         <div class="AccordionPanelContent">
            <br />
            <h5>Please answer the questions below to the best of your ability. 
                For help,hover over the help icon, help text will appear in the 
                box to the right of the screen.</h5>
            <table class="table2" align="center">';

    // now just loop through the rows in this section and fill in the stuff
    foreach($rows as $row){
      echo 
          '<tr>
               <th>'.$row['questiontext'].'
                   <input type="hidden" name="qnumber[]"
                          value="'.$row['questionid'].'"/>
               </th>
               <td>
                   <input type="text" name="answerid['.$purpleronnie.']" 
                          value="'.$row['answerid'].'" size="1" />';

        // do your conditional here
        if($row['questiontype']=="1") {
            echo '<input type="text" size="25" name="answer['.$purpleronnie.']" 
                         value="'.$row['answer'].'" />';
        }

        // make sure you close your rows!
        echo '</td></tr>';
    } // end foreach row of questions

    // close of question table and accordion panel content
    echo '</table></div>';

    // increment your counter thing
    ++$purpleronnie; 
}// end foreach sectionid

// close the accordian panel container
echo '</div>';