0

我目前正在尝试生成一个表,其中每个工作日都填充了数据库中的数据。

一切正常,但我也想显示空日期。所以:

for (/*each weekday of the month*/) {
    if ($row['date'] == /*that day of the month*/)
        echo /*the data for that day*/;
    else
        echo /*empty row*/;
}

这就是我想它的样子,但我不知道如何以体面的方式做到这一点。

我希望有人可以帮我解决这个问题。

最终代码:

$data = array();
$numofdays = date(t, $monthstamp);

while ($row = $res->fetch_assoc()) {
    $monthstamp = mktime(0, 0, 0, $month, 1, $year);

    for ($i=1; $i<$numofdays; $i++) {
    $monthstamp = mktime(0, 0, 0, $month, $i, $year);
        if (strtotime($row['datum']) == $monthstamp) {
            echo 'Entry added for day '.$i.'<br>';
            $data[] = $i;
        }
    }
}

for ($i=1; $i<$numofdays; $i++) {
    if (in_array($i, $data))
        echo 'date ' . $i . ' is full<br>';
    else
        echo 'date ' . $i . ' is empty<br>';
}
4

2 回答 2

0

正是你建议的工作。像这样的东西(伪代码)

// create a date
$year = 2013;
for$month=1; $month<13;$month++;)
{
// create date for each day of each month, or just use an array with number of days in each month, which is easier imho
$MonthStamp = mktime(1, 1, 1, $month, 1,   $year);

// get number of days 
$NumOfDays = date(t, $MonthStamp);

for($i=1; $i=<$NumOfDays; $i++) 
  {
  if ($row['date'] == // Not sure how your date is formatted. create same date format
      echo /*the data for that day*/;
  else
    echo /*empty row*/;
  }
}
于 2013-10-16T08:54:06.800 回答
0

您将需要格式化为 table / divs 并更改 $row 的格式以匹配您的数据库行,但这应该可以工作。

// You would get your dates from DB here
$row = array
    (
    "01-01-2013" => "New Years Day",
    "31-01-2013" => "Holiday",
    );

$year = 2013;

for ($month = 1; $month <= 12; $month++)
    {
    $monthStamp = mktime(1, 1, 1, $month, 1, $year);

    $daysInMonth = date('t', $monthStamp);

    for($i = 1; $i <= $daysInMonth; $i++) 
    {
        $date = date("d-m-Y", mktime(1,1,1, $month, $i, $year) );

        echo $date;

        // Check to see if any events in $row and echo result is true
        if ( isset($row[$date]) )
            {
            echo " : " . $row[$date];
            }

        echo "<br>";
        }
    }
于 2013-10-16T09:20:07.550 回答