PHP:未定义的索引错误,即使程序运行良好

时间:2013-08-22 00:25:28

标签: php date indexing undefined

我在日期格式方面遇到了麻烦。我不知道为什么,让我们检查代码。

<?php

                    /* date settings */
                    $month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
                    $year = (int)  ($_GET['year'] ? $_GET['year'] : date('Y'));

                    /* select month control */
                    $select_month_control = '<select name="month" id="month">';
                    for($x = 1; $x <= 12; $x++) {
                        $select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
                    }
                    $select_month_control.= '</select>';

                    /* select year control */
                    $year_range = 7;
                    $select_year_control = '<select name="year" id="year">';
                    for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
                        $select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>';
                    }
                    $select_year_control.= '</select>';

                    /* "next month" control */
                    $next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month &gt;&gt;</a>';

                    /* "previous month" control */
                    $previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control">&lt;&lt;    Previous Month</a>';


                    /* bringing the controls together */
                    $controls = '<form method="get">'.$select_month_control.$select_year_control.'&nbsp;<input type="submit" name="submit" value="Go" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$previous_month_link.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$next_month_link.' </form>';

                    /* get all events for the given month */
                    $events = array();
                    $query = mysql_query("SELECT nama_task, DATE_FORMAT(tanggal_deadline,'%Y-%m-%d') AS tanggal_deadline FROM `task`");
                    if($query){
                    while($row = mysql_fetch_assoc($query)) {
                        $events[$row['tanggal_deadline']][] = $row;
                    }
                    }else{
                        echo 'querynya kosong';
                    }

                    echo '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
                    echo '<div style="float:left;">'.$controls.'</div>';
                    echo '<div style="clear:both;"></div>';
                    echo draw_calendar($month,$year,$events);
                    echo '<br /><br />';
                ?>

日历工作得很好,但是出现了像这样说的错误信息

  

注意:未定义的索引:第199行的E:\ xampp \ htdocs \ reminder \ calendar.php中的月份

     

注意:未定义的索引:第200行的E:\ xampp \ htdocs \ reminder \ calendar.php中的年份

错误消息表示此行错误

$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int)  ($_GET['year'] ? $_GET['year'] : date('Y'));

感谢您抽出宝贵的时间阅读我的问题和糟糕的英语。

2 个答案:

答案 0 :(得分:5)

要避免此消息,您可以对可能不存在的索引使用isset

$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));
$year = (int)  (isset($_GET['year']) ? $_GET['year'] : date('Y'));

如果有字段但没有任何数据,您也可以使用empty

$month = (int) (!empty($_GET['month']) ? $_GET['month'] : date('m'));
$year = (int)  (!empty($_GET['year']) ? $_GET['year'] : date('Y'));

答案 1 :(得分:0)

查看此代码我已经完成了您的选择。 click here