【问题标题】:Iterate until a condition is met迭代直到满足条件
【发布时间】:2015-05-19 06:20:42
【问题描述】:

我正在开发一个调度系统,我需要你的帮助。我的代码运行良好,

$t_slot_time是当前时间段,如果第19行条件为yes,$t_slot_time加2,$t_slot_times成为新时间。

我怎样才能为新的时间槽 $t_slot_times 重复从 1 到 21 的相同过程,其中 line1 上的 $t_slot_time 被 line20 上的新 $t_slot_times 和递增后的后续值替换,直到这个 $num_rowe >= 1 不满足。

谢谢

$queuen = mysql_query("SELECT * FROM put_exam WHERE sess_id ='".$t_slot_time."'") or die(mysql_error());
$arrDatasa = array();
while($rowsa = mysql_fetch_array($queuen))
{
    $arrDatasa[]=$rowsa['course_code']. '|';
    $docam = array_filter($arrDatasa);
}

foreach($arrDatasa as $a=> $rowsa)
{
    $docama .= $docam[$a];
}

//Store current coursecode to assisgn into currentass
$currentass = $e_course_code;

//Check for common student between the last assigned course and current course to be assigned, if yes increment timeslot by 2
$chkcomms = mysql_query("SELECT student.matric, student.std_name FROM student
    JOIN course_reg e1 ON e1.matric=student.matric
    JOIN course c1 ON c1.course_code=e1.course_code
    JOIN course_reg e2 ON e2.matric=student.matric
    JOIN course c2 on c2.course_code = e2.course_code
    WHERE c1.course_code = '".$currentass."'
    AND c2.course_code RLIKE '%$docama%'
    GROUP BY student.matric") or die(mysql_error());

// Count number of rows
$num_rowe = mysql_num_rows($chkcomms);
if($num_rowe >= 1) {
    $t_slot_times = $t_slot_time + 2;
}

【问题讨论】:

    标签: php mysql loops iteration do-while


    【解决方案1】:
    $num_rowe = mysql_num_rows($chkcomms);
    for( $i=0; $i<=$num_rowe; $i++){
        ...
    }
    

    我通常是怎么处理这种事情的!

    编辑:希望我已经理解你想要做什么,还要注意我没有测试过这个错误!

    function get_codes( $t_slot_time ){
        $queuen = mysql_query("SELECT * FROM put_exam WHERE sess_id ='".$t_slot_time."'") or die(mysql_error());
        $arrDatasa = array();
        while($rowsa = mysql_fetch_array($queuen))
        {
            $arrDatasa[]=$rowsa['course_code']. '|';
            $docam = array_filter($arrDatasa);
        }
    
        foreach($arrDatasa as $a=> $rowsa)
        {
            $docama .= $docam[$a];
        }
    
        return $docama;
    }
    
    //if you need it done at least once then 
    $docama = get_codes( $t_slot_time );
    
    //Store current coursecode to assisgn into currentass
    $currentass = $e_course_code;
    
    //Check for common student between the last assigned course and current course to be assigned, if yes increment timeslot by 2
    $chkcomms = mysql_query("SELECT student.matric, student.std_name FROM student
        JOIN course_reg e1 ON e1.matric=student.matric
        JOIN course c1 ON c1.course_code=e1.course_code
        JOIN course_reg e2 ON e2.matric=student.matric
        JOIN course c2 on c2.course_code = e2.course_code
        WHERE c1.course_code = '".$currentass."'
        AND c2.course_code RLIKE '%$docama%'
        GROUP BY student.matric") or die(mysql_error());
    
    // Count number of rows
    $num_rowe = mysql_num_rows($chkcomms);
    if($num_rowe >= 1) {
        for( $i=0; $i<=$num_rowe; $i++){
            $t_slot_time = $t_slot_time + 2;
            $docama = get_codes( $t_slot_time );
            //add code to do what ever you want with $docama!
        }
    }
    

    【讨论】:

    • 谢谢。但是循环之间会发生什么?以及如何让我的新时间脱离循环使用
    • 在循环中放置你想要重复的任何代码。如果您只希望它在匹配条件时重复,则使用 if 语句检查它是否满足。如果需要多次执行源代码顶部的代码,请将其放入 for 循环中。您需要决定哪些部分!
    • 感谢用户,我理解你,但我上面的代码示例可能会有所帮助。 @user3633383
    • 感谢用户,但在增加 2 直到满足条件 @user3633383 后,我没有获得 $t_slot_time 的新值
    猜你喜欢
    • 2020-10-06
    • 2021-11-26
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 2022-01-13
    • 2016-11-16
    • 1970-01-01
    • 2021-11-14
    相关资源
    最近更新 更多