如何用不同的参数值调用foreach循环内部控制器内的函数

时间:2014-07-07 07:12:31

标签: php oop laravel-4

我试图用相同或不同的参数值重复调用m自定义函数,在foreach循环参数内部取决于我提供的关键值。

foreach ($result as $r) {

            if($r->marks1==null || $r->marks2==null)
            {
                echo $r->p_code;
                $toUpdate=$this->getResult($username,$r->p_code);
                print_r($toUpdate);
            }
        }

但是当我打印最新的查询结果时,我得到$ toUpdate附加最新的参数查询。

Array
(
    [query] => select * from `result` where (`studentid` = ?) and `studentid` = ? and `ccode` = ? and `a_no` = ? order by `date` desc limit 1
    [bindings] => Array
        (
            [0] => XYZ
            [1] => XYZ
            [2] => course123code
            [3] => 12321
        )

    [time] => 0.18
)

我的用户名变得相同,而课程代码在找到第二个结果时会被覆盖。

我希望在foreach循环中得到结果getResult(),以便它可以为不同的参数值提供相关结果。

public function getLatestResult($username,$course_code)
{

    $user=new User;
    $currentDetailOfUser=$this->userCurrentDetail($username);

        $userdetail=json_decode($currentDetailOfUser,true);
            $username        =$userdetail['username'];
            $studentid       =$userdetail['userid'];
            $studentBatch    =$userdetail['batch'];
            $programCode     =$userdetail['programCode'];
            $activeSemester  =$userdetail['activesemester'];

        $condition_key=array(
            'studentid' =>$studentid
            );


       $getCurrentResult1 =$user->getDetail('student_result',$condition_key);
       $getCurrentResult2 =$user->getDetail('student_result',$condition_key);
       $resultAssessment1=$getCurrentResult1->where('studentid',$studentid)->where('course_code',$course_code)->where('assignment_no',1)->orderBy('date','Desc')->limit(1)->get();
       $resultAssessment2=$getCurrentResult2->where('studentid',$studentid)->where('course_code',$course_code)->where('assignment_no',2)->orderBy('date','Desc')->limit(1)->get();
       $recentResult=array_merge($resultAssessment1,$resultAssessment2);
        return $recentResult;


}

1 个答案:

答案 0 :(得分:0)

如果您从db内的foreach loop获取数据,这是一种不好的做法。

顺便说一句,你可以通过将所有新结果保存在索引相同的数组中来实现这一点,它会看起来像这样 -

$toUpdate=array();

foreach ($result as $r) {

                if($r->marks1==null || $r->marks2==null)
                {
                    echo $r->p_code;
                    $toUpdate[$r->p_code]=$this->getResult($username,$r->p_code); // added $r->p_code as index to Array - $toUpdate

                }
            }
    print_r($toUpdate); // this will give all the related result according your parameters

<强> [UPDATE]

尝试使用unset($yourarray)作为下一个请求,这些数组将是新的,并且每次都会分配新值 -

public function getLatestResult($username,$course_code)
{

    $user=new User;
    $currentDetailOfUser=$this->userCurrentDetail($username);

        $userdetail=json_decode($currentDetailOfUser,true);
            $username        =$userdetail['username'];
            $studentid       =$userdetail['userid'];
            $studentBatch    =$userdetail['batch'];
            $programCode     =$userdetail['programCode'];
            $activeSemester  =$userdetail['activesemester'];

        $condition_key=array(
            'studentid' =>$studentid
            );


       $getCurrentResult1 =$user->getDetail('student_result',$condition_key);
       $getCurrentResult2 =$user->getDetail('student_result',$condition_key);
       $resultAssessment1=$getCurrentResult1->where('studentid',$studentid)->where('course_code',$course_code)->where('assignment_no',1)->orderBy('date','Desc')->limit(1)->get();
       $resultAssessment2=$getCurrentResult2->where('studentid',$studentid)->where('course_code',$course_code)->where('assignment_no',2)->orderBy('date','Desc')->limit(1)->get();


        unset($currentDetailOfUser);
        unset($userdetail);
        unset($condition_key);
        unset($recentResult);

        $recentResult=array_merge($resultAssessment1,$resultAssessment2);
        return $recentResult;

}

希望这可以帮到你。