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.
$staff[0]['NAME']. Hope this helps