2

I've been stuck on this coding for quite some time now. I'm new to PHP, so I might've made a stupid mistake somewhere. My aim is to show a table of attendance for a month after a user selects the date and year. Similar to this

   January 2015
   Name              |1   |2   |3|...|31|
   STAFF A-Full Name |9:02|8:30| |...|  |
   STAFF B-Full Name |8:43|    | |...|  |

This is the code I'm talking about:

<html>
<?php
error_reporting(0);
//database connection goes here

$m = $_POST['month'];
$y = $_POST['year'];

$inidate= print_r($y.'/'.$m.'/%',true);

$sql = "SELECT NAME, DATEIN, TIMEIN FROM VXDTIME WHERE NAME != '' AND NAME LIKE '%-%' AND NAME NOT LIKE '%Old% %Data%' AND TIMEIN != '' ORDER BY NAME, DATEIN"; 

$result = ibase_query($connect,$sql);

$staff = $dome = array();
$getmonth = date("m",strtotime($inidate));
$getyear = date("Y",strtotime($inidate));

while($row = ibase_fetch_assoc($result))
{   $dome[] = $row; }

foreach($dome as &$value)
{
    if (ctype_upper(substr($value['NAME'],0,2))== TRUE)
        $staff = $value['NAME'];
}
$staff = array_values(array_unique($staff,SORT_REGULAR));

//***************************
foreach ($staff as $key1 => $value1)
{
    foreach ($dome as $key => $value)
    {
        for ($i=0;$i<cal_days_in_month(CAL_GREGORIAN,$getmonth,$getyear);$i++)
        {
            if ($value1 == $key['NAME']) //compares name
                if ($key['DATEIN'] != NULL) //compares date
                    if (idate("d",strtotime($key['DATEIN'])) == (i+1))
                         $key1[$i+1] = $key['TIMEIN'];
                    else
                         $key1[$i+1] = 'No Record';
                else
                    $key1[$i+1] = 'Blank';
        }
    }
}

//Make the array start at 0
//$staff = array_values($staff);
//array_walk($staff, create_function('&$v,$k', 'if (is_array($v)) $v=array_values($v);'));

$getdate = print_r($y.'/'.$m.'/01',true);
echo "<table border=1><tr><td>No.</td> <td style='width:350'>Name</td>";

for($i=0;$i<cal_days_in_month(CAL_GREGORIAN,$getmonth,$getyear);$i++)
    echo "<td style='width:40'>".($i+1)."</td>";
echo "</tr>";

$count=0;
foreach ($staff as $key => $value)
{       
    echo "<tr><td>".($count+1)."</td><td>".$key[0]."</td>";

    for($j=1;$j<(cal_days_in_month(CAL_GREGORIAN,$getmonth,$getyear)+1);$j++)
    {           
        if (date("D",strtotime($getdate)) == 'Sat' || date("D",strtotime($getdate)) == 'Sun')
            echo "<td BGCOLOR='#525266'> </td>";
        else
            if (strtotime($key[$j]) > strtotime('09:10'))
                echo "<td BGCOLOR='#ffff00'>".$key[$j]."</td>";
            else
                echo "<td>".$key[$j]."</td>";
    }
    echo "</tr>";
    $getdate=strftime("%Y/%m/%d", strtotime("$getdate +1 day"));
    count++;
}
echo "</table>";
?>
</html>

var_dump($dome) looks like this

array
  0 => 
    array
      'NAME' => string 'STAFF A-Full Name'
      'DATEIN' => string '2015/01/01' //string date/time isn't not my choice
      'TIMEIN' => string '09:02'
  1 => 
    array
      'NAME' => string 'STAFF A-Full Name'
      'DATEIN' => string '2015/01/02'
      'TIMEIN' => string '08:30'
  2 => 
    array
      'NAME' => string 'STAFF B-Full Name'
      'DATEIN' => string '2015/01/01'
      'TIMEIN' => string '08:43'
  3 => 
    array
      'NAME' => string 'Staff B-Full Name'
      'DATEIN' => string '2012/01/01'
      'TIMEIN' => string '09:11'
 //and so on...

Despite reading many guides here about accessing multidimensional array, I still can't understand how to use foreach, using keys and such. I'm sure this code looks really messy but right now I'm stumped.

I hope someone can help me.

EDIT: Found the solution to the the duplicate problem

Another problem I'm having is that I'm not sure my way of accessing the array is correct. By the end of the code, $staff should look like this:

array
  0 => 
    array
      'NAME' => string 'STAFF A-Full Name'
      0 => string '09:02'
      1 => string '08:30'
      2 => string '09:00'
      //... all the way to the end of month

  1 => 
    array
      'NAME' => string 'STAFF B-Full Name'
      0 => string '08:43'
      1 => string '09:01'
      2 => string '08:50'
      //...
  //...

I'll need to loop through $staff and $dome in order to extract the time from the latter into the former. But I'm not sure I'm doing the foreach correctly to achieve that.

2
  • 1
    From your var_dump output, it looks like the top level is a typical numerical-indexed array. At the second level is an associative array, which has three keys 'NAME', 'DATEIN' and 'TIMEIN'. For example, to print the 'NAME' of the first employee in the array, you have to use $staff[0]['NAME']. Hope this helps Commented Jul 23, 2015 at 18:02
  • @kkaosninja Thank you, that clarifies one thing. When it comes to multi arrays, I usually use for loops to access the index. However, it's real confusing when it comes to associative arrays because I don't know how the $key => $value works. I've looked around but the examples are more advanced. Commented Jul 24, 2015 at 1:12

1 Answer 1

0

Apologies, editing as I read your code wrong.

It looks like you are assigning a value to the variable $staff, instead of stuffing that value into the existing array at $staff.

foreach($dome as &$value)
{
  $staff = $value['NAME'];
}
$staff = array_unique($staff);

should be:

foreach($dome as &$value)
{
  $staff[] = $value['NAME'];
}
$staff = array_unique($staff);
Sign up to request clarification or add additional context in comments.

1 Comment

You're most welcome, glad it helped. Could you accept this as the answer? As for your comment above about the loop with $key => $value, it means you can use $key inside the loop as a var to access the key itself, and $value as the actual value in that place in the array. So if you had an associative array where 'name' points to 'namehere', you could use $key inside the loop to get the value 'name' and $value to get the value 'namehere'. It just makes it easy to access the value of the key itself so you can grab its name.

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.