2011-10-27 59 views

回答

1

由于绘制压延机的功能是

function draw_calendar($month,$year){ 

你必须提供$month并在下一/上一环节$year,例如

/calendar.php?month=12&year=2011 

单击此链接时,此数据可在$_GET中找到。你不想unsanitized数据,所以你把它拿来像这样在您的日历脚本的顶部:

$input = filter_input_array(
    INPUT_GET, 
    array(
     'month' => array(
      'filter' => FILTER_VALIDATE_INT, 
      'options' => array('min_range' => 1, 'max_range' => 12) 
     ), 
     'year' => array(
      'filter' => FILTER_VALIDATE_INT, 
      'options' => array('min_range' => 2010, 'max_range' => 2015) 
     ) 
    ) 
); 

过滤功能将确保我们得到1和12,以及2010年和2015年之间的年间的一个月(相应调整或者按照您认为合适的方式删除选项)。如果通过的号码不在该范围内(或者没有链接被点击),我们将为他们获得false,这意味着我们将不得不设置理智默认值,例如,

$input['year'] = $input['year'] ?: date('Y'); 
$input['month'] = $input['month'] ?: date('n'); 

这将既可以使用传递给脚本的有效值或无效值的情况下,一年和/或月设置为当前年份和/或月份。

现在画的日历:

echo draw_calendar($input['month'], $input['year']); 

对于下一/上一链接,您可以手动检查月份是否是12或1,然后增加/相应减少当年或使用DateTime对象

$dateTime = new DateTime; 
$dateTime->setDate($input['year'], $input['month'], 1)); 
printf(
    '<a href="/calendar.php?month=%d&amp;year=%d">Next</a>' . 
    '<a href="/calendar.php?month=%d&amp;year=%d">Previous</a>', 
    $dateTime->modify('-1 month')->format('n'), 
    $dateTime->format('Y'), 
    $dateTime->modify('+2 month')->format('n'), 
    $dateTime->format('Y') 
); 

demo (slightly abridged)

另一种选择是,以目前的月和年在会话中存储,然后就甲肝e下一个/上一个链接,无需年份和月份,而只是像+1和-1这样的来回。但是,你没有直接的方式跳到某个月。

这就是它的全部。

+0

“INPUT_GET”后是否缺少逗号? –

+0

@Jared是的,谢谢。固定。随时编辑任何其他小错别字 – Gordon

相关问题