php foreach值分配到while循环行

时间:2011-09-30 05:52:08

标签: php

我有两个存储问题和答案的表,因为下面的脚本用于获取序列化答案,并将每个表反序列化为一个数组,如下所示:

while($ans_row = mysql_fetch_array($q_chkans)){
$answer = unserialize($ans_row["a_answered"]);

foreach($answer as $val){
    for($i=0; $i<=3; $i++){
    $r[$i] = $answer[$i];
}

}

例如,在反序列化过程之后,我说得到每个数组元素如下:

$r[0] = a
$r[1] = b
$r[2] = c

使用那些a,b和c,我想将它们分配到另一个表循环中,这就是获取它的问题,如下所示:

1) question a?
   answer a.

2) question b?
   answer b.

3) question c?
   answer c.

但我的代码总是返回所有答案,如

1) question a?
   answer a b c.

2) question b?
   answer a b c.

3) question c?
   answer a b c.

完整代码如下:

$ques_data = array();
$q_ques = mysql_query("SELECT * FROM ".$tb03." WHERE ques_section='".$sectid."' AND ques_status='1' ORDER BY ques_id") or die(mysql_error());

while($rows = mysql_fetch_assoc($q_ques)){
$ques_data[] = $rows;
}

$q_chkans = mysql_query("SELECT * FROM ".$tb08." WHERE a_usrid='".$_COOKIE["loggedId"]."' AND a_section='".$sectid."'") or die(mysql_error());
$numrows = mysql_num_rows($q_chkans);    

$q_sect = mysql_query("SELECT * FROM ".$tb02." WHERE sect_id='".$sectid."' LIMIT 0, 1") or die(mysql_error());
$r_sect = mysql_fetch_array($q_sect);
$section = $r_sect["sect_id"];

echo "<div class='sect_list'><b style='color:#000;'>".sprintf("%1\$.1f",$no)."&nbsp;&nbsp;".ucwords($r_sect["sect_title"])."</b></div>".$staff_txt;

foreach($ques_data as $ques_rows){

    echo "<div class='ques_list'>
            <div><b>".$sectid.".".$no."</b>&nbsp;&nbsp;".ucwords($ques_rows["ques_title"])."</div>
            <div class='ques_rmk'>".ucwords($ques_rows["ques_rmk"])."</div>
            <div class='answer'>";
                if($numrows <= 0){

                    $q_ans = mysql_query("SELECT * FROM ".$tb04." WHERE input_ques_id='".$ques_rows["ques_id"]."'") or die(mysql_error());

                    if($ques_rows["ques_type"] == 1){
                        echo "<select name='txtOpt_".$section."_".$no."'>";

                        while($ans_rows = mysql_fetch_array($q_ans)){
                            echo "<option value='".$ans_rows["input_mark"]."'>".$ans_rows["input_title"]."</option>";
                        }

                        echo "</select>";

                    }else{

                        echo "<input type='text' name='txtbox_".$section."' value='' style='width:500px;height:18px;' />";
                    }

                }else{

                    while($ans_row = mysql_fetch_array($q_chkans)){
                        $answer = unserialize($ans_row["a_answered"]);

                        foreach($answer as $val){
                            for($e=0; $e<=count($answer); $e++){
                                $r[$e] = $answer[$e];
                            }
                        }
                    }


                }

    echo "  </div>
          </div>";

        $no += 1;
    }

如何才能使每个答案都适当地分配到自己的问题中?

请指教,谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

while($ans_row = mysql_fetch_array($q_chkans)){
    $answer = unserialize($ans_row["a_answered"]);

    foreach($answer as $i => $val){
        $r[$i] = $val;
    }

}