I have these two blocks of identical code in one of my PHP modules. The only difference between these two blocks is that in first block it is ... active = ' .$db->q('1'));
and in the second block its ... active = ' .$db->q('0'));
Can I make a common PHP function say subjectlist() for both these blocks and then pass arguments 0
and 1
respectively to shorten the code.
As in, subjectlist(0);
subjectlist(1);
First Block
<?php
// getting branch, semester and subject code of present classes of the teacher
$query1 = $db->getQuery(true);
$query1->select('sem,code')
->from('iust_teachers_classes')
->where('teacher = '. $db->q($user->username). 'and active = ' .$db->q('1'));
$db->setQuery($query1);
$sem_code = $db->loadObjectlist();
echo "<h3>List of present classes : </h3> <br>";
// Loop to populate the list of all present classes of the teacher
foreach ($sem_code as $rows) {
$query2 = $db->getQuery(true);
$query2->select('subject')
->from('iust_subjects')
->where('sem = '. $db->q($rows->sem). 'and code = ' .$db->q($rows->code));
$db->setQuery($query2);
$sub_list = $db->loadObjectlist();
// echoing the active subject list
foreach ($sub_list as $rows) {
echo $rows->subject;
echo "<br>";
}
}
?>
Second Block
<?php
// getting branch, semester and subject code of previous classes of the teacher
$query3 = $db->getQuery(true);
$query3->select('sem,code')
->from('iust_teachers_classes')
->where('teacher = '. $db->q($user->username). 'and active = ' .$db->q('0'));
$db->setQuery($query3);
$sem_code = $db->loadObjectlist();
echo "<h3>List of previous classes : </h3> <br>";
// Loop to populate the list of all previous classes of the teacher
foreach ($sem_code as $rows) {
$query4 = $db->getQuery(true);
$query4->select('subject')
->from('iust_subjects')
->where('sem = '. $db->q($rows->sem). 'and code = ' .$db->q($rows->code));
$db->setQuery($query4);
$sub_list = $db->loadObjectlist();
// echoing the previous subject list
foreach ($sub_list as $rows) {
echo $rows->subject;
echo "<br>";
}
}
?>
Help would be appreciated.