1

I am trying to create a monthly attendance system which displays how many hours an employee has worked each day of the month.The table looks like this-monthly attendance sample pic

The php code -

    $s = $_GET['s'];
$y = date('Y');

$fdt = date('Y-m-d',strtotime($y.'-'.$s.'-'.'01'));
$ldt = date('Y-m-t',strtotime($y.'-'.$s.'-'.'01'));
$nd = date('t',strtotime($y.'-'.$s.'-'.'01'));
$stmt = $pdo->prepare("SELECT emp_id_fk, Resource_Name , ROUND(SUM(TIME_TO_SEC(TIMEDIFF(COALESCE(`end_time`,NOW()),`start_time`)))/3600,1) as Hrs, CAST(`start_time` AS DATE) as cd FROM `attendance` LEFT JOIN resource ON attendance.`emp_id_fk` = resource.`Resource_ID` WHERE CAST(`start_time` AS DATE) BETWEEN :s AND :e GROUP BY emp_id_fk, Resource_Name, CAST(`start_time` AS DATE)");

$stmt->bindParam(':s', $fdt);
$stmt->bindParam(':e', $ldt);

$stmt->execute();
$data = $stmt->fetchAll();
print_r($data);
$ret = "";
$id = array_column($data,'emp_id_fk');
$name = array_column($data,'Resource_Name');
$id = array_unique($id);
$name = array_unique($name);

for($i=0;$i<sizeof($id); $i++){
    $ret.='<tr>
            <td>'.$name[$i].'</td>';

        for($j=0; $j<sizeof($nd); $j++){
            $cd = date('Y-m-d',strtotime($y.'-'.$s.'-'.($j+1)));
            $d = date('l',strtotime($y.'-'.$s.'-'.($j+1)));

            if($d != 'Sunday' && $d != 'Saturday'){

                for($k=0; $k<sizeof($data); $k++){

                    if( $id[$i] == $data[$k]['emp_id_fk']){

                        if($cd == $data[$k]['cd']){
                            $ret.='<td>'.($data[$k]['Hrs']).'</td>';
                        }else{
                            $ret.='<td>0</td>';
                        }
                    }
                }
            }

        }
    $ret.='</tr>';
}

echo $ret;

As seen in the image, the second forloop($j loop) is iterating only once where as it should iterate 28,30 or 31 times based on the month. What my expected output is that for each employee there should be a value for everyday of the week and if there is no record for a particular date it should display 0.

Thanks in advance.

2
  • 1
    From what i can see, $j<sizeof($nd) is the likely culprit, in that sizeof expects an array, but $nd is just a date that you set on line 5. I'm surprised it's even running twice. Commented May 16, 2017 at 19:16
  • I hope you're running a 64-bit version of PHP. If not, you should really look into the DateTime object: php.net/manual/en/class.datetime.php. Year 2038 problem: en.wikipedia.org/wiki/Year_2038_problem Commented May 16, 2017 at 19:52

1 Answer 1

2

The PHP function sizeof() is an alias for count(). The function count() is to count all elements in an array, or something in an object.

Change:

for($j=0; $j<sizeof($nd); $j++){

To:

for($j=1; $j<=$nd; $j++) {
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! I get stumped by things like this too if I've been in the code too much that day. Sometimes it is just good to have another set of eyes. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.