Jump to content

php iteration until a condition is met


textaiwo

Recommended Posts

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.         }
Link to comment
https://forums.phpfreaks.com/topic/296391-php-iteration-until-a-condition-is-met/
Share on other sites

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.

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.