使用Ajax日历在PHP中显示下个月或上个月

时间:2018-05-28 08:04:32

标签: php jquery ajax

我正在创建一个日历,您可以在其中查看用户的所有预留日期。这个日历将可供管理员使用,但现在我正在制作它时遇到了问题,我似乎无法显示下一个或前几个月。我需要使用Ajax执行此操作,因此当管理员单击下一个或上个月的按钮时,页面不会刷新。我有两个响应ajax-jQuery $.post函数的按钮,它在某种程度上起作用。我只能转换到一个月前进或后退,当我从下一个上一个或从上一个到下一个时,代码会忽略当前月份并仅显示那两个月。

这是index.php部分代码,我在其中调用负责日历的类:

<div class="container-fluid">
      <div class="roomss">
                <div class="calendar">
                    <div class="container-fluid header">
                        <a id="hello" href="#" onclick="return false" onmousedown="javascript:swapContent('prev')"><span class="glyphicon glyphicon-chevron-left"></span></a>
                        <a id="hello2" href="#" onclick="return false" onmousedown="javascript:swapContent('next')"><span class="glyphicon glyphicon-chevron-right"></span></a>
                        <h4 id="current_month" style="color: #fff;"><?php echo date("F, Y ",mktime(0,0,0,$month,$day,$year));?></h4>
                    </div>
                    <div id="availibility-calendar" class="availibility-calendar">
                        <?php
              $book = new viewBook();
              $book->showCalendar();
            ?>   
                    </div>


                </div>
      </div>

这是Ajax-jQuery部分:

function swapContent(a){
  var url = "includes/js/booking_calendar.php";
  $.post(url,{contentVar: a}, function(r){
    $("#current_month").html(r).show();
  });
  $.post(url,{contentVars: a}, function(r){
    $("#availibility-calendar").html(r).show();
  });
}

交换内容的booking_calendar.php文件(第一个isset用于更改月份标题,它非常原始,我只需要将2678400秒添加到当前时间):

<?php
include '../../../inc/config.php'; // here I include the class connection to database (private)
include '../../../inc/booking.inc.php'; // here I include class with a SELECT statement (protected)
include '../../../inc/booking-view.inc.php'; // and here I include the class which shows all the content (public)
$contentVar = $_POST['contentVar'];
if(isset($_POST['contentVar'])){
  if ($contentVar == "next") {
    $onemonth = 2678400;
    $date = time() + $onemonth;
    $curr = $date;
    $month= date("m", $curr);
    $year=date("Y", $curr);
    $day=date("d", $curr);
    echo date("F, Y ",mktime(0,0,0,$month,$day,$year));
  /*  die(var_dump($contentVar));*/
  } elseif($contentVar == "prev"){
    $onemonth = 2678400;
    $date = time() - $onemonth;
    $curr2 = $date;
    $month= date("m", $curr2);
    $year=date("Y", $curr2);
    $day=date("d", $curr2);
    echo date("F, Y ",mktime(0,0,0,$month,$day,$year));
  }
}

$contentVars = $_POST['contentVars'];
if(isset($_POST['contentVars'])){
  if ($contentVars == "next") {
    $next = new viewBook();
    $next->showNextCalendar();
  }elseif($contentVars == "prev"){
    $prev = new viewBook();
    $prev->showPrevCalendar();
  }
}

这是创建日历的类函数,如果日期匹配,它也会从数据库中检查并突出显示为红色。

public function showCalendar(){
    date_default_timezone_set('Europe/Riga');
    $date = time();
    $month= date ("m", $date);
    $year=date("Y", $date);
    $day=date("d", $date);

    $first_day = mktime(0,0,0,$month, 1, $year);
    $day_of_week = date('D', $first_day);
    switch ($day_of_week) {
      case 'Mon': $blank = 0; break;
      case 'Tue': $blank = 1; break;
      case 'Wed': $blank = 2; break;
      case 'Thu': $blank = 3; break;
      case 'Fri': $blank = 4; break;
      case 'Sat': $blank = 5; break;
      case 'Sun': $blank = 6; break;

    }

    $days_in_month = cal_days_in_month(CAL_GREGORIAN , $month, $year);

    echo '<table class="calendar-table" border="1" >';
    echo '<tr><td style="width:60px">Mon</td><td style="width:60px">Tue</td><td style="width:60px">Wed</td><td style="width:60px">Thu</td><td style="width:60px">Fri</td><td style="width:60px">Sat</td><td style="width:60px">Sun</td></tr>';

    $day_count = 1;

    echo '<tr>';
    while ($blank > 0) {
      echo '<td></td>';
      $blank = $blank - 1;
      $day_count++;
    }

    $day_num = 1;

    while ($day_num <= $days_in_month) {
      $sql = "SELECT date_from, date_to FROM booking_2 ORDER BY book2_id";
      $result = $this->connect()->query($sql);
      $numRows = $result->num_rows;
      if($numRows > 0){
        while($row = $result->fetch_assoc()){
          $true = true;
          $date_from = $row['date_from'];
          $date_to = $row['date_to'];
          $time_from = strtotime($date_from);
          $time_fromm = idate('d', $time_from);
          $time_to = strtotime($date_to);
          $time_too = idate('d', $time_to);
        }
      }

      if ($time_fromm == $day_num) {
          for ($g = $time_fromm ; $g <= $time_too ; $g++){
            echo '<td style="border: 1px solid black; background-color:#FF3232 !important;">'.$g.'</td>';
            $day_count++;
            $day_num++;
            if ($day_count > 7) {
              echo '</tr><tr>'; 
              $day_count = 1;
            }
          }
          $day_num--;
          $day_count--;
      }else{
        echo '<td style="border: 1px solid black; background-color:#D3D3D3 !important;">'.$day_num.'</td>';
      }

        $day_num++;
        $day_count++;
        if ($day_count > 7) {
          echo '</tr><tr>';
          $day_count = 1;
        }

    }



    while($day_count > 1 && $day_count <= 7){
      echo '<td> </td>';
      $day_count++;
    }

    echo '</tr></table>';

并且对于下一个和前几个月有两个不同的类基本相同但彼此不同,在一个我添加2678400秒showNextCalendar();time();,在其他我减去2678400来自showPrevCalendar(); current monthprevious monthnext month not showing current month的秒time();

是的,我知道代码在所有月份显示相同的日期红色。这是我稍后会处理的问题。我现在需要的是一种创建日历的不同方式,也许是一个自己处理它的功能,我只能想到一个。如果你这样做,感谢你阅读本文!如果有任何专业人士可以帮助我解决这个问题,请做:D。再次感谢。

0 个答案:

没有答案