php - Browse through calendar months with a href

657

Okay so I'm trying to make a calendar with pure php. I am wanting to add two arrows that allow the user to go forward and back through the months. It also needs to automatically change the year when it gets down to the first month or up to the twelfth month.

I have gotten the following code thinking it would work with get paramaters, but I am starting to think there is a better way without using the$_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>";

Right now I have an if statement so the calendar still loads with no paramaters: (this is in 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); 

My full code for making the calendar and getting the menu items is as follows: (this is in menu-controller.php)

function draw_calendar($month,$year){

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

/* draw table */
$calendar = '<table >';

/* 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 ><td >'.implode('</td><td >',$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 >';

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

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

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

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

/* all done, return result */
return $calendar;
  }

My calendar so you can understand what I'm trying to do better.

Update So my question is, how can I use the script I already have to increase and decrease the month and year dynamically when the user clicks the left and right arrows on the top of the menu? Right now I'm using the$_GET[''] and it isn't working because of many reasons:

  • When the month gets to 1 it can go down to 0 and the year doesn't change. Should go from 1 to 12 for the previous year.

  • It uses the$_GET[''] which is vulnerable

  • The++$month decreases the month rather than increases it.

Basically, I want to increase and decrease the months and years when the user clicks the forward and previous buttons without using the URL. Just like you can with google calendar. (and no I can't just use google calendar.)

896

Answer

Solution:

Google calendar uses AJAX I guess so you don't see the params in your browser URL. Using GET params should be fine in your solution.

//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

I believe this piece of code should help you out.

You should do all the checking of "existing" or "allowed" dates before you open the month as you need. But having the year and month in the URL shouldn't be a problem.

The only "vulnerability" you'll get is that the user might enter the values manually and jump to the month this way instead of clicking the arrows (as long as you make sure the input values are valid as mentioned earlier).

People are also looking for solutions to the problem: php - Algorithm for USSD ( State machine ) Application

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.