2014-02-16 26 views
0

我創建一個日曆的招待所馬平ID和日期。它只是告訴預訂日期或可用日期。 綠色爲Available和Red爲已預訂。日曆腳本 - 在日期列

所有工作不錯,但我想使它變爲可編輯手段入住時間應該有一個鏈接「的書,它」和預訂日期應該有一個鏈接「未登記」。

我的劇本實際上是取出由表和店鋪ID和日期在數組(只有在該表中這兩列BTW)。然後使用in_arrary函數,它只是使所有在表中找到的日期變爲紅色,並將所有其他日期保留爲綠色。

我面臨的困難映射或填充ID和日期值有人能幫助我。

概括地說,我想ID和日期值的日期欄打印,所以我可以做鏈接。對於預訂日期,ID應傳遞到下一頁,以便我可以將其刪除並重新提供。而對於可用的日期,日期應該在下一頁傳遞,所以我在表中添加該日期並使其成爲預訂。

從大師們的幫助。

這裏是我完整的腳本:

  <?php 

      error_reporting(0); 
      include("config.php"); 

      /// get current month and year and store them in $cMonth and $cYear variables 
      (intval($_REQUEST["month"])>0) ? $cMonth = $_REQUEST["month"] : $cMonth = date("n"); 
      (intval($_REQUEST["year"])>0) ? $cYear = $_REQUEST["year"] : $cYear = date("Y"); 

      if ($cMonth<10) $cMonth = '0'.$cMonth; 

      // generate an array with all unavailable dates 
      $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE `date` LIKE '".$cYear."-".$cMonth."-%'"; 
      $sql_result = mysql_query ($sql, $connection) or die ('request "Could not execute SQL query" '.$sql); 
      while ($row = mysql_fetch_assoc($sql_result)) { 
      $unavailable[] = $row["date"]; 
      } 


      // calculate next and prev month and year used for next/prev month navigation links and store them in respective variables 
      $prev_year = $cYear; 
      $next_year = $cYear; 
      $prev_month = intval($cMonth)-1; 
      $next_month = intval($cMonth)+1; 

      // if current month is Decembe or January month navigation links have to be updated to point to next/prev years 
      if ($cMonth == 12) { 
      $next_month = 1; 
      $next_year = $cYear + 1; 
      } elseif ($cMonth == 1) { 
      $prev_month = 12; 
      $prev_year = $cYear - 1; 
      } 
      ?> 
      <table style="width:100%; text-align: center;"> 
      <tr> 
       <td class="mNav"><a href="javascript:LoadMonth('<?php echo $prev_month; ?>', '<?php echo $prev_year; ?>')">&lt;&lt;&nbsp;Prev</a></td> 
       <td colspan="5" class="cMonth"><?php echo date("F, Y",strtotime($cYear."-".$cMonth."-01")); ?></td> 
       <td class="mNav"><a href="javascript:LoadMonth('<?php echo $next_month; ?>', '<?php echo $next_year; ?>')">Next&nbsp;&gt;&gt;</a></td> 
      </tr> 
      <tr> 
       <td style="width:16%;" class="wDays">M</td> 
       <td style="width:14%;" class="wDays">T</td> 
       <td style="width:14%;" class="wDays">W</td> 
       <td style="width:14%;" class="wDays">T</td> 
       <td style="width:14%;" class="wDays">F</td> 
       <td style="width:14%;" class="wDays">S</td> 
       <td style="width:14%;" class="wDays">S</td> 
      </tr> 
      <?php 
      $first_day_timestamp = mktime(0,0,0,$cMonth,1,$cYear); // time stamp for first day of the month used to calculate 
      $maxday = date("t",$first_day_timestamp); // number of days in current month 
      $thismonth = getdate($first_day_timestamp); // find out which day of the week the first date of the month is 
      $startday = $thismonth['wday'] ; // 0 is for Sunday and as we want week to start on Mon we subtract 1 
      if (!$thismonth['wday']) $startday = 7; 


      for ($i=1; $i<($maxday+$startday); $i++) { 


      if (($i % 7) == 1) echo "<tr>"; 

      if ($i < $startday) { echo "<td>&nbsp;</td>"; continue; }; 

      $current_day = $i - $startday + 1; 

      (in_array($cYear."-".$cMonth."-".$current_day,$unavailable)) ? $css='booked' : $css='available'; // set css class name based on date availability 


      if ($css == 'booked') { 
      echo "<td class='".$css."'>". $current_day . "<br/>" . "Booked" . "</td>"; 
      } 
      elseif ($css == 'available') { 
      echo "<td class='".$css."'>". $current_day . "<br/>" . "Available" . "</td>"; 
      } 

      if (($i % 7) == 0) echo "</tr>"; 

      } 
      ?> 
      </table> 

回答

0

您需要的id添加到$unavailable陣列,這樣以後可以把它添加到鏈接。最簡單的是使用它作爲數組鍵 -

while ($row = mysql_fetch_assoc($sql_result)) { 
    $unavailable[$row["id"]] = $row["date"]; 
} 

然後,當你正在構建的每一天細胞,用array_search()拿到預訂的日期的數組鍵,使用日期的可用日期 -

if ($css == 'booked') { 
    echo "<td class='".$css."'>". $current_day . "<br/>" . "Booked" . "<br/>" . "<a href='scheduler_page.php?id=".array_search($cYear."-".$cMonth."-".$current_day,$unavailable)."'>Unbook</a>" . "</td>"; 
} 
elseif ($css == 'available') { 
    echo "<td class='".$css."'>". $current_day . "<br/>" . "Available" . "<br/>" . "<a href='scheduler_page.php?date=".$cYear."-".$cMonth."-".$current_day."'>Book it</a>" . "</td>"; 
} 

然後你的下一個頁面,我的例子使用scheduler_page.php,您可以$_GET確定weither取消預定或書 -

if(isset($_GET['id'])){ 
    // unbook the date 
} 
elseif(isset($_GET['date'])){ 
    // schedule the date 
} 
+0

嗨肖恩,感謝您的解決方案RESO大部分問題都存在,現在可用的日期顯示正確的Book Book鏈接,並在日後參數中使用正確的日期。但問題出現在已經在表格中的日期意味着預訂日期。現在應用您的解決方案後,只有一個日期列以紅色顯示,鏈接Unbook,但沒有參數中的ID。我認爲應該做一些小小的調整,讓它變得完美,請你幫忙。 – WebDeem

+0

順便說一句,我現在有3個ID表,id = 1 date = 2014-02-21,id = 2 date = 2014-02-15,id = 3 date = 2014-02-16。它看起來像它沒有得到id值 – WebDeem

+0

當我打印數組$美元[$行[「ID」]] = $行[「日期」];它只顯示最後一個日期2014-02-16其中有ID 3. – WebDeem