迭代直到满足条件

时间:2015-05-19 06:20:43

标签: php mysql loops iteration do-while

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

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

如何为新的时间段$t_slot_times重复1到21的相同过程,其中第1行上的$t_slot_time被第20行上的新$t_slot_times替换,后续增加后的值为$num_rowe >= 1 { {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;
}

1 个答案:

答案 0 :(得分:0)

$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!
    }
}