2017-08-23 84 views
-1

我正在编写一个php代码,用于检查mgt模块,我需要给出缓刑,撤回,缺陷或良好站立的学生数量,这是我计算了以前的cgpa ,累计cgpa以及学生是否已经完成课程。所有这些都在for循环中,我想从for循环中获得结果。我怎么做。我的代码是冗长低于从for循环中获取值

我已编辑的代码是什么,我想实现可以理解

foreach($aitemn as $item) 

    { 
     if (substr($item,1,2) < 13 || (substr($item,1,2) == 13 && $mode == '2')) 
     { 

      if ($pcgpa < 1.0 && $gpa < 1.0 && $xlevel == 1) 
       { 
       $remark = 'Probation'; 
       $p = $p + 1; 
       } 
      elseif ($pcgpa < 1.0 && $gpa < 1.0 && $xlevel != 1) 
       { 
        $remark = 'Withdraw'; 
        $w = $w + 1; 
       } 

      elseif($gpa > 1.0 && $fc == 'NIL') 
      { 
       $remark = 'In Good Standing'; 
       $gs = $gs + 1; 
      } 
      elseif ($gpa < 1.0 && $pcgpa > 1.0) 
      { 
       $remark = 'Probation'; 
       $p = $p + 1; 
      }  
      elseif ($fc <> 'NIL') 
      { 

       $remark = 'Deficient'; 
       $d = $d + 1; 
      } 

     } 
    elseif (substr($item,1,2) > 12) 
     { 

      if ($pcgpa < 1.5 && $gpa < 1.5 && $xlevel == 1) 
      { 
       $remark = 'Probation'; 
       $p = $p + 1; 
      } 
      elseif ($pcgpa < 1.5 && $gpa < 1.5 && $xlevel != 1) 
       { 
        $remark = 'Withdraw'; 
        $w = $w + 1; 
       } 

     elseif($gpa > 1.5 && $fc == 'NIL') 
      { 
       $remark = 'In Good Standing'; 
       $gs = $gs + 1; 
      } 
      elseif ($gpa < 1.5 && $pcgpa > 1.5) 
      { 
       $remark = 'Probation'; 
       $p = $p + 1; 
      }  
      elseif ($fc <> 'NIL') 
      { 

       $remark = 'Deficient'; 
       $d = $d + 1; 
      } 

     } 

    $num = $num + 1; 

    } 


     <td>Number in Good Standing: <?php echo $gs;?></br> 
     <td>Number on Probation:  <?php echo $p;?></br> 
     <td>Number in Deficient  <?php echo $d;?></br> 
     <td>Number in Withdrawn  <?php echo $w;?></br> 
+0

你的问题不清楚。你能改述一下吗? –

+0

我有一份学生的名单,我正在计算他们的GPA,CGPA,PCGPA,如果他们有遗漏或没有。所有这些都在for循环中。我现在需要打印具有优秀课程的学生数量,良好的学生数量等等。我注意到,即使在使用数组之后,变量也无法在for循环之外访问。 –

+0

我建议你重新编写你的代码,这样有条件符合学生的条件,1.有优秀的课程,2.有良好的信誉等。这样,它使你的代码更加模糊和容易理解。 –

回答

0

好吧,我想我明白你的意思。

为了让您能够访问的变量在foreach循环,这样做会为您您的标记在循环中,像这样的一种方法:

<?php 
foreach($aitemn as $item) { 
    // your other codes 
?> 
<td>Number in Good Standing: <?php echo $gs;?></br> 
<td>Number on Probation:  <?php echo $p;?></br> 
<td>Number in Deficient  <?php echo $d;?></br> 
<td>Number in Withdrawn  <?php echo $w;?></br> 

<?php 
} // close the foreach after the markup 
?> 

希望有所帮助。

+0

感谢塞缪尔,但它真的是我想看到的每个变量的最后一个值,而不是每个变量的所有迭代 –

+0

最后一个值,你是指在变量中加了'+ 1'的位置? –