textaiwo Posted May 19, 2015 Share Posted May 19, 2015 I am working on a schdeuling system and i urgetly need your help. I have code running well, $t_slot_time is the current timeslot, if condition is yes on line19, $t_slot_time increment by 2, and $t_slot_times becomes new time. how can i repeat same process from 1 to 21 for the new timeslot $t_slot_times where $t_slot_time on line1 is replaced with the new $t_slot_times on line20 and subsequent values after increment until this $num_rowe >= 1 is not satisfied. Thanks 1. $queuen = mysql_query("SELECT * FROM put_exam WHERE sess_id ='".$t_slot_time."'") or die(mysql_error()); 2. $arrDatasa= array(); 3. while($rowsa = mysql_fetch_array($queuen)) 4. { 5. $arrDatasa[]=$rowsa['course_code']. '|'; 6. $docam = array_filter($arrDatasa); 7. } 8. foreach($arrDatasa as $a=> $rowsa){ 9. $docama.=$docam[$a]; 10. } //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 11. $chkcomms = mysql_query("SELECT student.matric, student.std_name FROM student 12. JOIN course_reg e1 ON e1.matric=student.matric 13. JOIN course c1 ON c1.course_code=e1.course_code 14. JOIN course_reg e2 ON e2.matric=student.matric 15. JOIN course c2 on c2.course_code = e2.course_code 16. WHERE c1.course_code = '".$currentass."' 17. AND c2.course_code RLIKE '%$docama%' 17. GROUP BY student.matric") or die(mysql_error()); // Count number of rows 18. $num_rowe = mysql_num_rows($chkcomms); 19. if($num_rowe >= 1) { 20. $t_slot_times = $t_slot_time + 2; 21. } Quote Link to comment https://forums.phpfreaks.com/topic/296391-php-iteration-until-a-condition-is-met/ Share on other sites More sharing options...
QuickOldCar Posted May 19, 2015 Share Posted May 19, 2015 If you use mysql_fetch_array() or mysql_fetch_assoc() would get the array of multiple results. Then you could loop these results in a while statement. The way your code is now is only finding out how many rows exist. $num_rowe = mysql_num_rows($chkcomms); mysql_* functions have been deprecated and should consider using PDO or mysqli_* functions. Quote Link to comment https://forums.phpfreaks.com/topic/296391-php-iteration-until-a-condition-is-met/#findComment-1512210 Share on other sites More sharing options...
textaiwo Posted May 19, 2015 Author Share Posted May 19, 2015 Thanks, how can i make every single loop use a different value for $t_slot_time in line1 until my condition is satisfied and terminate the loop after my condition is satisfied Quote Link to comment https://forums.phpfreaks.com/topic/296391-php-iteration-until-a-condition-is-met/#findComment-1512226 Share on other sites More sharing options...
AP81 Posted May 20, 2015 Share Posted May 20, 2015 There's multiple ways to do it, but a good start is wrapping that code in a function. function hasSlotTime($slotTime) { $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 as $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); return ($num_rowe >= 1) { } Then you can do something like this: while (hasSlotTime($t_slot_time)) { $t_slot_times = $t_slot_time + 2; } Ideally, you shouldn't be doing queries in a loop, and if possible you should write some better code rather than using the array filter in the loop. I don't know the nature of your application and how to use it, but that is essentially what you'd do. So: Create function which does the query The function should return true if the result is >=1, otherwise false Loop until false Hopefully that is a push in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/296391-php-iteration-until-a-condition-is-met/#findComment-1512293 Share on other sites More sharing options...
textaiwo Posted May 20, 2015 Author Share Posted May 20, 2015 (edited) Thanks @AP81, I really appreciate, one of the best answers i ever got on this, i knew function would be the way forward but am not sharp in OOP.. I usually get my way around. kidnly confirm the line below has no opening brace as it gives error even when i try to close it and also requires a semi colon ';' when i try to remove it return ($num_rowe >= 1) { Many Thanks Edited May 20, 2015 by textaiwo Quote Link to comment https://forums.phpfreaks.com/topic/296391-php-iteration-until-a-condition-is-met/#findComment-1512349 Share on other sites More sharing options...
AP81 Posted May 20, 2015 Share Posted May 20, 2015 My bad, sorry I did this off the top of my head. Should be: return ($num_rowe >= 1); Quote Link to comment https://forums.phpfreaks.com/topic/296391-php-iteration-until-a-condition-is-met/#findComment-1512359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.