数组到日历-Table

Array to Calendar-Table

我在 PHP 中创建了以下数组 ($stuArr)。 我想在 HTML-Table 中打印数组,这样我就可以创建一个 Table 像这样(日历系统):

table 中的数字是当月的日期。 每个用户都有自己的行。我用这个生成 table:

$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$days = $days + 2;

echo "<table border='1'>";
echo "<tr>";
for ($j = 0; $j < $days; $j++) {
    echo "<th>$j</th>";
}
echo "</tr>";

//Person
$file = file_get_contents('MOCK_DATA-100.json');
$data = json_decode($file);

//Events
$file2 = file_get_contents('calendar.json');
$data2 = json_decode($file2);

$stuArr = [];

foreach($data as $student) { 
    $id = $student->id;
    $name = $student->name;

    $mark = false;
    foreach($data2 as $cal) {
        if($cal->resource == $id) {
            $start  = new DateTime(substr($cal->start, 0, 10));
            $end    = new DateTime(substr($cal->end, 0, 10));
            $interval = DateInterval::createFromDateString('1 day');
            $period   = new DatePeriod($start, $interval, $end);                    

            $dd         = [];
            if($month == $start->format('n')) {
                foreach ($period as $dt) {
                    if($month != $dt->format('n'))
                        break;
                    $dd[]   = $dt->format("j");
                }
                $stuArr[] = [$id, $dd];
            }
        }
    }
    
}

var_export($stuArr);

foreach($stuArr as $student) {
    $id     = $student[0];
    $dates  = $student[1];

}

echo "</table>";

var_export($stuArr); 的输出:

array ( 0 => array ( 0 => '2', 1 => array ( 0 => '22', 1 => '23', 2 => '24', 3 => '25', ), ), 1 => array ( 0 => '3', 1 => array ( 0 => '23', 1 => '24', 2 => '25', ), ), )

感谢您的帮助!问候, 芬恩 如果您对我有任何疑问或需要对代码的某些部分进行解释,请直接说!!!

错误:

试试下面的代码。我可以看到您将 o-29 打印为天数,请尝试在循环中从 1 而不是 0 开始。

// update inside the Student loop, previous code remains as it is

        foreach($stuArr as $student) {
            
            $dates  = $student[1];
            
            echo "<tr>";
            
            for ($j = 1; $j <= $days; $j++) {

                if(in_array($j, $dates))
                    echo "<td>X</t>";
                else 
                    echo "<td>&nbsp;</td>";

            }   
            
            echo "</tr>";           
        
        }