Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用href浏览日历月份_Php_Calendar - Fatal编程技术网

Php 使用href浏览日历月份

Php 使用href浏览日历月份,php,calendar,Php,Calendar,好的,我正在尝试用纯php制作一个日历。我想添加两个箭头,允许用户在几个月内前进和后退。它还需要在第一个月或第十二个月时自动更改年份 我得到了下面的代码,认为它可以与get paramaters一起工作,但我开始认为有一种更好的方法不用使用$\u get[] 我制作日历和获取菜单项的完整代码如下:这在menu-controller.php中 function draw_calendar($month,$year){ require '../system/config/config.php';

好的,我正在尝试用纯php制作一个日历。我想添加两个箭头,允许用户在几个月内前进和后退。它还需要在第一个月或第十二个月时自动更改年份

我得到了下面的代码,认为它可以与get paramaters一起工作,但我开始认为有一种更好的方法不用使用$\u get[]

我制作日历和获取菜单项的完整代码如下:这在menu-controller.php中

function draw_calendar($month,$year){

require '../system/config/config.php';

/* draw table */
$calendar = '<table class="table table-bordered">';

/* get full month text */ 
$result = mysqli_query($con,"SELECT * FROM months WHERE month_number=$month");

if (mysqli_num_rows($result) > "0") {

    while($row = mysqli_fetch_array($result)) {

        extract($row);

        $calendar .= "<tr class='calendar-row'>
            <td class='calendar-day-head'><a href='Menu.php?month=".--$month."&year=2013' class='btn btn-primary'><span class='glyphicon glyphicon-chevron-left'></span></a></td>
            <td class='calendar-day-head' colspan='5' style='vertical-align:middle;'>" .$month_full_text. " " .$year. "</td>
            <td class='calendar-day-head'><a href='Menu.php?month=".++$month."&year=2013' class='btn btn-primary'><span class='glyphicon glyphicon-chevron-right'></span></a></td></tr>";
    }
}

/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= '<tr class="calendar-row">';

/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
    $calendar.= '<td class="calendar-day-np"> </td>';
    $days_in_this_week++;
endfor;

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    $calendar.= '<td class="calendar-day" height="100">';
        /* add in the day number */
        $calendar.= '<div class="day-number">'.$list_day.'</div>';

        /** Query the database for an entry for this day !! If matches found, print them !! **/
        $result = mysqli_query($con,"SELECT * FROM menu_items WHERE month=$month AND year=$year AND day = $list_day");

        if (mysqli_num_rows($result) > "0") {

            while($row = mysqli_fetch_array($result)) {

                extract($row); 

                $calendar .= "<b>" .$entree. "</b><br/>";
                $calendar .= "" .$side_one. "<br/>";
                $calendar .= "" .$side_two. "<br/>";
                $calendar .= "" .$dessert. ""; 
            }
        }

        else {

            $calendar .= "";
        }

    $calendar.= '</td>';
    if($running_day == 6):
        $calendar.= '</tr>';
        if(($day_counter+1) != $days_in_month):
            $calendar.= '<tr class="calendar-row">';
        endif;
        $running_day = -1;
        $days_in_this_week = 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < 8):
    for($x = 1; $x <= (8 - $days_in_this_week); $x++):
        $calendar.= '<td class="calendar-day-np"> </td>';
    endfor;
endif;

/* final row */
$calendar.= '</tr>';

/* end the table */
$calendar.= '</table>';

/* all done, return result */
return $calendar;
    }
更新 因此,我的问题是,当用户单击菜单顶部的左箭头和右箭头时,如何使用我已经必须动态增加和减少月份和年份的脚本?现在我正在使用$\u GET[],但由于许多原因,它不起作用:

当月份变为1时,它可以降为0,年份不变。上一年应从1变为12

它使用易受攻击的$\u GET[]

++$month减少而不是增加月份


基本上,我想增加和减少用户在不使用URL的情况下单击“前进”和“上一步”按钮的月份和年份。就像谷歌日历一样。不,我不能只使用谷歌日历。

谷歌日历使用AJAX,我想这样你就看不到浏览器URL中的参数了。在您的解决方案中使用GET params应该很好

//get the 1st of currently opened month
$currentMonth = strtotime($year . '-' . $month . '-01');

//get the 1st of previous and next months
$prevMonth = strtotime('-1 month', $currentMonth);
$nextMonth = strtotime('+1 month', $currentMonth);

//get the url (GET) params for prev and next months
$prevMonthParams = 'year=' . date('Y', $prevMonth) . '&month=' . date('m', $prevMonth);
$nextMonthParams = 'year=' . date('Y', $nextMonth) . '&month=' . date('m', $nextMonth);

//use them in the url you need
我相信这段代码应该能帮到你

在根据需要打开月份之前,您应该对现有日期或允许的日期进行所有检查。但是在URL中包含年份和月份应该不是问题


您将遇到的唯一漏洞是,用户可能会手动输入值,并以这种方式跳转到月份,而不是单击箭头,只要您确保输入值如前所述有效。

您的问题是…?检查我的更新。。。很抱歉在这里使用GET参数没有安全问题。我不会说它很脆弱。
function draw_calendar($month,$year){

require '../system/config/config.php';

/* draw table */
$calendar = '<table class="table table-bordered">';

/* get full month text */ 
$result = mysqli_query($con,"SELECT * FROM months WHERE month_number=$month");

if (mysqli_num_rows($result) > "0") {

    while($row = mysqli_fetch_array($result)) {

        extract($row);

        $calendar .= "<tr class='calendar-row'>
            <td class='calendar-day-head'><a href='Menu.php?month=".--$month."&year=2013' class='btn btn-primary'><span class='glyphicon glyphicon-chevron-left'></span></a></td>
            <td class='calendar-day-head' colspan='5' style='vertical-align:middle;'>" .$month_full_text. " " .$year. "</td>
            <td class='calendar-day-head'><a href='Menu.php?month=".++$month."&year=2013' class='btn btn-primary'><span class='glyphicon glyphicon-chevron-right'></span></a></td></tr>";
    }
}

/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= '<tr class="calendar-row">';

/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
    $calendar.= '<td class="calendar-day-np"> </td>';
    $days_in_this_week++;
endfor;

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    $calendar.= '<td class="calendar-day" height="100">';
        /* add in the day number */
        $calendar.= '<div class="day-number">'.$list_day.'</div>';

        /** Query the database for an entry for this day !! If matches found, print them !! **/
        $result = mysqli_query($con,"SELECT * FROM menu_items WHERE month=$month AND year=$year AND day = $list_day");

        if (mysqli_num_rows($result) > "0") {

            while($row = mysqli_fetch_array($result)) {

                extract($row); 

                $calendar .= "<b>" .$entree. "</b><br/>";
                $calendar .= "" .$side_one. "<br/>";
                $calendar .= "" .$side_two. "<br/>";
                $calendar .= "" .$dessert. ""; 
            }
        }

        else {

            $calendar .= "";
        }

    $calendar.= '</td>';
    if($running_day == 6):
        $calendar.= '</tr>';
        if(($day_counter+1) != $days_in_month):
            $calendar.= '<tr class="calendar-row">';
        endif;
        $running_day = -1;
        $days_in_this_week = 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < 8):
    for($x = 1; $x <= (8 - $days_in_this_week); $x++):
        $calendar.= '<td class="calendar-day-np"> </td>';
    endfor;
endif;

/* final row */
$calendar.= '</tr>';

/* end the table */
$calendar.= '</table>';

/* all done, return result */
return $calendar;
    }
//get the 1st of currently opened month
$currentMonth = strtotime($year . '-' . $month . '-01');

//get the 1st of previous and next months
$prevMonth = strtotime('-1 month', $currentMonth);
$nextMonth = strtotime('+1 month', $currentMonth);

//get the url (GET) params for prev and next months
$prevMonthParams = 'year=' . date('Y', $prevMonth) . '&month=' . date('m', $prevMonth);
$nextMonthParams = 'year=' . date('Y', $nextMonth) . '&month=' . date('m', $nextMonth);

//use them in the url you need