Attendance System IF Statement Help

Hello
This is about the attendance system in which I have to make some changes in a company in which i just joined. The previous code which was written by previous employee is following in 1). When attendance category 3 ( 3 for Leave) is selected & Leave category form 1 to 12 is selected this system gives ‘L’. Now i was asked to make change that if attendance category 3 ( 3 for Leave) & leave category 13 is selected it should give result ‘C’, But the code is not working. I wrote the following code which is in 2) and now it is giving me output as ‘L’ ‘C’.
1.

 if ($emp_attendance->attendance_status == '3' || $emp_attendance->leave_category_id != NULL) {
    if ($emp_attendance->leave_category_id == '2' || $emp_attendance->leave_category_id == '9') {
        echo '<span style="padding:2px 4px" class="label label-primary std_p">L<sub>s</sub></span>';
    }
    else {
        echo '<span style="padding:2px 4px" class="label label-primary std_p">L</span>';
    }
}
if ($emp_attendance->attendance_status == '3' || $emp_attendance->leave_category_id == '13') {
   echo '<span  style="padding:2px 4px" class="label label-success std_p">C</span>';
}

The original logic isn’t doing everything it should and contains a logic mistake, an || (OR) vs an && (AND.)

All this logic is for the case where attendance_status is a 3 (leave) and if you have code for several different values for this, you should be using a switch/case statement. To simplify and eliminate redundant tests, you should have one main conditional statement testing the attendance_status value. Next, you need to deal with the different possible leave_category_id values. The original logic is just testing if the leave_category_id is not a null value, meaning all real values. But what if the value is a null? Doesn’t this mean that a leave category either hasn’t been selected or there’s a programming mistake somewhere? Code should do ‘something’ for all possible outcomes.

This is what the original logic should (probably, untested) be doing -

// attendance_status 3 (leave) handling
if($emp_attendance->attendance_status == 3)
{
	// note: because the following statements are inside the above main conditional statement, they are being AND'ed with it.
	// the above conditional statement is true AND one of the following branches is true -
	
	// test the leave category
	if($emp_attendance->leave_category_id == NULL)
	{
		// it is a null
		echo 'There is no leave category entered.';
	}
	else
	{
		// not a null. this would include all possible real values
		// 2, 9 special handling
		$sub = in_array($emp_attendance->leave_category_id, [2,9]) ? '<sub>s</sub>' : '';
		echo "<span style='padding:2px 4px' class='label label-primary std_p'>L{$sub}</span>";
	}
}

It should now be clearer where and how you can change this to add the logic for the leave_category_id 13 value.

Sponsor our Newsletter | Privacy Policy | Terms of Service