生成部分填充的表格

时间:2013-10-16 08:37:08

标签: php html mysql

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

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

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>';
}

2 个答案:

答案 0 :(得分: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*/;
  }
}

答案 1 :(得分:0)

您需要格式化为table / div并更改$ 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>";
        }
    }