好的,所以我正在尝试使用纯PHP制作日历。我想添加两个箭头,允许用户前进和后退几个月。当它下降到第一个月或直到第12个月时,它还需要自动更改年份。
我已经得到以下代码,认为它可以与get paramaters一起工作,但我开始认为没有使用$_GET['']
有更好的方法。
<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>";
现在我有一个if语句所以日历仍然加载没有参数:(这是在Menu.php中)
if (!$_GET['month'] || !$_GET['year']) {
$month = date('m');
$year = date('y');
}
else {
$month = $_GET['month'];
$year = $_GET['year'];
}
echo draw_calendar($month,$year);
我制作日历和获取菜单项的完整代码如下:(这是在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;
}
更新
所以我的问题是,当用户点击菜单顶部的左右箭头时,如何使用我已经拥有的脚本动态增加和减少月份和年份?现在我正在使用$_GET['']
因为很多原因而无法正常工作:
当月份变为1时,它可以降至0并且年份不会改变。上一年应该从1到12。
它使用易受攻击的$_GET['']
++$month
减少月份而不是增加月份。
基本上,我希望增加和减少用户在不使用URL的情况下单击前进和上一个按钮的月份和年份。就像你可以使用谷歌日历。 (不,我不能只使用谷歌日历。)
答案 0 :(得分:0)
谷歌日历使用AJAX我猜你不会在浏览器URL中看到params。在解决方案中使用GET参数应该没问题。
//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中使用年份和月份应该不是问题。
您将获得的唯一“漏洞”是用户可以手动输入值并以这种方式跳转到月份而不是单击箭头(只要您确保输入值如前所述有效)。