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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by textaiwo
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.