在foreach循环中遍历数组的所有元素

时间:2019-07-26 06:56:22

标签: php html mysql

我正在创建一个包含问题和四个选项的测验。我想抓的是,我要制作55个问题的试卷,分别来自1到10个单元。每个单元都有自己的问题数量,总和为55。

目前,我仅收到一个单位问题。在单元1之后,循环以某种方式中断了。我正在做的事情是将所有单元放入数组中,并在foreach循环下进行迭代以生成问题和相关选项。

这是我的代码:

foreach($unit_id as $value){
 $unit_parameters = $db->query("select * from tbl_final_quiz_query_parameters where Unit_Id  = $value and Level_Id = $level_id ");
 while($row_unit_parameters = mysql_fetch_array($unit_parameters))
{

$unit_id   = $row_unit_parameters['Unit_Id'];
$secA_ques = $row_unit_parameters['No_of_Ques_SectionA'];
$secB_ques = $row_unit_parameters['No_of_Ques_SectionB'];
$secC_ques = $row_unit_parameters['No_of_Ques_SectionC'];
$total_ques_from_unit = $row_unit_parameters['Total_Questions'];
$total_marks_from_unit = $row_unit_parameters['Total_Marks']; 

$sq = $db->query("select * from (select * from tbl_question_levelexam where is_not_deleted = '0' order by rand() ) as t WHERE unit_id = $value and level_id = $level_id order by section ASC" );
while($row=mysql_fetch_array($sq))
{

?>          
<?php if($sectionOne <= $secA_ques && $row['section'] == 1) {?>  
         <ul class="list-group">
          <li class="list-group-item active">
            <div class="radio noMargin" id="<?php echo $sectionOne ; ?>info"><?php echo $no ; ?> <span class="glyphicon glyphicon-arrow-right"></span></div><div class="question"><?php echo $row['question'].$row['id'] ; ?></div>
<div style="float:right; margin-top:-20px;">Marks:<?php echo $row['marks'] ; ?></div></li>
<?php          
echo '<input type="hidden" name="question[]" id="questionId" value="'.$row['id'].'">';
echo '<input type="hidden" name="attempt" value="'.$attemptCount.'">';
echo '<input type="hidden" name="unit_id" value="'.$unit_id.'">';

$questionId     =   $row['id'] ;  

$sqO =$db->query("SELECT id, options from tbl_options_levelexam_question WHERE question_id = ".$questionId."");
while($rowO=mysql_fetch_array($sqO))
{
?>  
    <li class="list-group-item wrp_radio">
      <div class="radio noMargin">
        <label>
          <input type="radio" class="checkedInfo" alt="<?php echo $no ; ?>"  name="optionAns<?php echo $i ; ?>"  value="<?php echo $rowO['id'] ; ?>" id="question_id">
          <?php echo $rowO['options'] ; ?></label>
      </div>
    </li>
<?php } ?> 


   <li class="list-group-item">
      <div class="radio noMargin wrp_radio">
        <label>
          <input type="radio" class="checkedInfo" alt="<?php echo $no ; ?>" accesskey="notAttempt" checked name="optionAns<?php echo $i ; ?>"  value="NA">
          <p style="color:red;">I am not Attempting this question</p></label>
      </div>
    </li>

<li class="list-group-item">
<div class="wrp_review">
      <div class="radio noMargin  bg-danger">
        <label>
          <input type="checkbox" class="checkedInfo" alt="<?php echo $no ; ?>" accesskey="revision" name="optionAnss<?php echo $i ; ?>"  id="optionAns<?php echo $no  ; ?>" value="NA">
          Review</label>
      </div>
</div> 
    </li>

        </ul>        
<?php $i++;$no++; ?>
<?php $sectionOne++;} ?>


<?php if($sectionTwo <= $secB_ques && $row['section'] == 2) {?> 
 <ul class="list-group">        
          <li class="list-group-item active">
            <div class="radio noMargin"  id="<?php echo $no ; ?>info"><?php echo $no ; ?> <span class="glyphicon glyphicon-arrow-right"></span></div><div class="question"><?php echo $row['question'].$row['id'];?></div>
          <div style="float:right; margin-top:-20px;">Marks:<?php echo $row['marks'] ; ?></div></li>
<?php          
echo '<input type="hidden" name="question[]" id="questionId" value="'.$row['id'].'">';
echo '<input type="hidden" name="attempt" value="'.$attemptCount.'">';
echo '<input type="hidden" name="unit_id" value="'.$unit_id.'">';

$questionId     =   $row['id'] ;  

$sqO =$db->query("SELECT id, options from tbl_options_levelexam_question WHERE question_id = ".$questionId."");
while($rowO=mysql_fetch_array($sqO))
{
?>  
    <li class="list-group-item wrp_radio">
      <div class="radio noMargin">
        <label>
          <input type="radio" class="checkedInfo" alt="<?php echo $no ; ?>" name="optionAns<?php echo $i ; ?>" value="<?php echo $rowO['id'] ; ?>">
          <?php echo $rowO['options'] ; ?></label>
      </div>
    </li>
<?php } ?>   

   <li class="list-group-item">
      <div class="radio noMargin wrp_radio">
        <label>
          <input type="radio" class="checkedInfo" alt="<?php echo $no ; ?>" accesskey="notAttempt" checked name="optionAns<?php echo $i ; ?>" id="optionAns<?php echo $i ; ?>" value="NA">
          <p style="color:red;">I am not Attempting this question</p></label>
      </div>
    </li> 

   <li class="list-group-item" >
    <div class="wrp_review">
      <div class="radio noMargin bg-danger">
        <label>
          <input type="checkbox" class="checkedInfo" alt="<?php echo $no ; ?>" accesskey="revision" name="optionAnss<?php echo $i ; ?>"  id="optionAns<?php echo $no  ; ?>" value="NA">
          Review</label>
      </div>
    </div>   
    </li> 
     </ul> 



<?php $i++;$no++; ?>
<?php $sectionTwo++;} ?>


<?php if($sectionThree <= $secC_ques && $row['section'] == 3) {?> 
 <ul class="list-group">        
          <li class="list-group-item active">
            <div class="radio noMargin"  id="<?php echo $no ; ?>info"><?php echo $no ; ?> <span class="glyphicon glyphicon-arrow-right"></span></div><div class="question"><?php echo $row['question'].$row['id'] ; ?></div>
          <div style="float:right; margin-top:-20px;">Marks:<?php echo $row['marks'] ; ?></div></li>
<?php          
echo '<input type="hidden" name="question[]" id="questionId" value="'.$row['id'].'">';
echo '<input type="hidden" name="attempt" value="'.$attemptCount.'">';
echo '<input type="hidden" name="unit_id" value="'.$unit_id.'">';

$questionId     =   $row['id'] ;  

$sqO =$db->query("SELECT id, options from tbl_options_levelexam_question WHERE question_id = ".$questionId."");
while($rowO=mysql_fetch_array($sqO))
{
?>  
    <li class="list-group-item wrp_radio">
      <div class="radio noMargin">
        <label>
          <input type="radio" class="checkedInfo" alt="<?php echo $no ; ?>" name="optionAns<?php echo $i ; ?>" value="<?php echo $rowO['id'] ; ?>">
          <?php echo $rowO['options'] ; ?></label>
      </div>
    </li>
<?php } ?>   

   <li class="list-group-item">
      <div class="radio noMargin wrp_radio">
        <label>
          <input type="radio" class="checkedInfo" alt="<?php echo $no ; ?>" accesskey="notAttempt" checked name="optionAns<?php echo $i ; ?>" id="optionAns<?php echo $i ; ?>" value="NA">
          <p style="color:red;">I am not Attempting this question</p></label>
      </div>
    </li>   

<li class="list-group-item">
<div class="wrp_review"> 
      <div class="radio noMargin bg-danger">
        <label>
          <input type="checkbox" class="checkedInfo" alt="<?php echo $no ; ?>" accesskey="revision" name="optionAnss<?php echo $i ; ?>"  id="optionAns<?php echo $no  ; ?>" value="NA">
          Review</label>
      </div>
    </div>  
    </li>  
          </ul>        
<?php $i++;$no++; ?>
<?php $sectionThree++;} }} } ?>

$Sectionone$sectionTwo$sectionthree是整个试卷分为三个部分。每个单元在所有三个部分中都有不同的值。像第一个单元一样,第1部分有3个问题,第2部分有2个问题,第3部分有2个问题。我在哪里做错了?

0 个答案:

没有答案