PHP:使用foreach访问多维数组并将结果输出为表



我已经被这种编码困住了相当长一段时间了。我是PHP新手,所以我可能在某个地方犯了一个愚蠢的错误。我的目标是在用户选择日期和年份后显示一个月的考勤表。类似于

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

这就是我说的代码:

<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)看起来像这样

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...

尽管在这里读了很多关于访问多维数组的指南,我仍然不明白如何使用foreach,使用键等。我知道这段代码看起来很乱,但是现在我被难住了。

我希望有人能帮助我。

编辑:找到解决重复问题的方法

我遇到的另一个问题是,我不确定我访问数组的方式是否正确。在代码末尾,$staff应该是这样的:

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'
      //...
  //...

我需要遍历$staff和$dome,以便从后者提取时间到前者。但是我不确定我做的foreach是否正确

抱歉,我读错了你的代码。

看起来你是在给变量$staff赋一个值,而不是把这个值填充到现有的$staff数组中。

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

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

相关内容

  • 没有找到相关文章

最新更新