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.