Jump to content

Calendars


lional

Recommended Posts

Hi All
I am trying to write a site for a guesthouse. What I would like to do is have a calender that will show when a particular room is booked. Ideally I would like to use PHP and mysql.
When somebody books for a room it must update the database table. The calendar must then get this info and update itself.
The guesthouse has four rooms so I think the best would be to have a calander for each room, which will show the day in red when it is booked and in green in availalble.
Is this the best way of doing this or does anyone else have any suggestions.
I have never tried this before so it is all very new to me.

Any help would be appreciated

Thanks

Lional
Link to comment
Share on other sites

when a user books the date for a room then echo the room is booked on that date via room id .


have a tabe name booked rooms

four rooms will have 4 $rooms_id in table booked rooms.

when a user books  a room they have to specefy the room.

when a room is booked also update the rooms1_id into booked rooms ect ect ...........

then show the four rooms with a pic on a page and the booked dates beside the picture
and rooom information.


i don like calenders but good luck....................


this is php fraks calender and if you want the numbers to go a colour indercating a booked date to cheeck avalabilty the your have to ask a more exsperence programer then me but good luck.
[code]

<html>
<head>
<title>PHP Calendar</title>
<style type="text/css">
<!--
.table.calendar {border: 1px solid #000000; border-collapse: collapse; color: #000000;

background: #FFFFFF; }
.td.today { border: 1px solid white; color: #000000; background: #EFEFEF; font-weight:

bold;}
.td.monthdays {border: 1px solid #434470; color: #000000; background: #FFFFFFF; }
.td.nonmonthdays { border: 1px solid white; color: #000000; background: #EFEFEF;}
-->
</style>
<body>

<?php
error_reporting('0');
ini_set('display_errors', '0');
// Gather variables from
// user input and break them
// down for usage in our script

if(!isset($_REQUEST['date'])){
  $date = mktime(0,0,0,date('m'), date('d'), date('Y'));
} else {
  $date = $_REQUEST['date'];
}

$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

// Get the first day of the month
$month_start = mktime(0,0,0,$month, 1, $year);

// Get friendly month name
$month_name = date('M', $month_start);

// Figure out which day of the week
// the month starts on.
$month_start_day = date('D', $month_start);

switch($month_start_day){
    case "Sun": $offset = 0; break;
    case "Mon": $offset = 1; break;
    case "Tue": $offset = 2; break;
    case "Wed": $offset = 3; break;
    case "Thu": $offset = 4; break;
    case "Fri": $offset = 5; break;
    case "Sat": $offset = 6; break;
}

// determine how many days are in the last month.
if($month == 1){
  $num_days_last = cal_days_in_month(0, 12, ($year -1));
} else {
  $num_days_last = cal_days_in_month(0, ($month -1), $year);
}
// determine how many days are in the current month.
$num_days_current = cal_days_in_month(0, $month, $year);

// Build an array for the current days
// in the month
for($i = 1; $i <= $num_days_current; $i++){
    $num_days_array[] = $i;
}

// Build an array for the number of days
// in last month
for($i = 1; $i <= $num_days_last; $i++){
    $num_days_last_array[] = $i;
}

// If the $offset from the starting day of the
// week happens to be Sunday, $offset would be 0,
// so don't need an offset correction.

if($offset > 0){
    $offset_correction = array_slice($num_days_last_array, -$offset, $offset);
    $new_count = array_merge($offset_correction, $num_days_array);
    $offset_count = count($offset_correction);
}

// The else statement is to prevent building the $offset array.
else {
    $offset_count = 0;
    $new_count = $num_days_array;
}

// count how many days we have with the two
// previous arrays merged together
$current_num = count($new_count);

// Since we will have 5 HTML table rows (TR)
// with 7 table data entries (TD)
// we need to fill in 35 TDs
// so, we will have to figure out
// how many days to appened to the end
// of the final array to make it 35 days.


if($current_num > 35){
  $num_weeks = 6;
  $outset = (42 - $current_num);
} elseif($current_num < 35){
  $num_weeks = 5;
  $outset = (35 - $current_num);
}
if($current_num == 35){
  $num_weeks = 5;
  $outset = 0;
}
// Outset Correction
for($i = 1; $i <= $outset; $i++){
  $new_count[] = $i;
}

// Now let's "chunk" the $all_days array
// into weeks. Each week has 7 days
// so we will array_chunk it into 7 days.
$weeks = array_chunk($new_count, 7);


// Build Previous and Next Links
$previous_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=";
if($month == 1){
  $previous_link .= mktime(0,0,0,12,$day,($year -1));
} else {
  $previous_link .= mktime(0,0,0,($month -1),$day,$year);
}
$previous_link .= "\"><< Prev</a>";

$next_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=";
if($month == 12){
  $next_link .= mktime(0,0,0,1,$day,($year + 1));
} else {
  $next_link .= mktime(0,0,0,($month +1),$day,$year);
}
$next_link .= "\">Next >></a>";

// Build the heading portion of the calendar table
echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"300\"

class=\"calendar\">\n".
    "<tr>\n".
    "<td colspan=\"7\">\n".
    "<table align=\"center\">\n".
    "<tr>\n".
    "<td colspan=\"2\" width=\"75\" align=\"left\">$previous_link</td>\n".
    "<td colspan=\"3\" width=\"150\" align=\"center\">$month_name $year</td>\n".
    "<td colspan=\"2\" width=\"75\" align=\"right\">$next_link</td>\n".
    "</tr>\n".
    "</table>\n".
    "</td>\n".
    "<tr>\n".
    "<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>\n".
    "</tr>\n";

// Now we break each key of the array 
// into a week and create a new table row for each
// week with the days of that week in the table data

$i = 0;
foreach($weeks AS $week){
      echo "<tr>\n";
      foreach($week as $d){
        if($i < $offset_count){
            $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month

-1,$d,$year)."\">$d</a>";
            echo "<td class=\"nonmonthdays\">$day_link</td>\n";
        }
        if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){
            $day_link = "<a

href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month,$d,$year)."\">$d</a>";
          if($date == mktime(0,0,0,$month,$d,$year)){
              echo "<td class=\"today\">$d</td>\n";
          } else {
              echo "<td class=\"days\">$day_link</td>\n";
          }
        } elseif(($outset > 0)) {
            if(($i >= ($num_weeks * 7) - $outset)){
              $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month

+1,$d,$year)."\">$d</a>";
              echo "<td class=\"nonmonthdays\">$day_link</td>\n";
          }
        }
        $i++;
      }
      echo "</tr>\n";   
}

// Close out your table and that's it!
echo '<tr><td colspan="7" class="days"> </td></tr>';
echo '</table>';
?>
</body>
</html>
[/code]
Link to comment
Share on other sites

I'd go for a combined horizontal calendar. That way your users can see at a glance the room availability for a given period of booking dates. EG
[pre]
Room | JULY
    | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15|
    | Sa| Su|  |  |  |  |  | Sa| Su|  |  |  |  |  | Sa|
-----|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
  1  | X | X |  |  |  |  | X | X |  |  |  |  |  | X | X |
  2  | X | X | X | X |  |  | X | X | X | X | X | X | X | X | X |
  3  | X | X | X | X | X |  |  | X |  |  |  |  |  | X | X |
  4  | X | X |  |  |  |  |  | X |  |  |  |  |  |  |  |[/pre]
Link to comment
Share on other sites

Thanks for your help- barend, I am getting there.
I am just stuch at my last hurdle.
The dates are writing perfectly to my dataase but what I would like to do, and I just can't seem toget right is to make the background red if the date is in the bookings database, and green if it is not ie the day is available.
I am trying to use the calendar that redarrow posted
Could you just point me in the right direction please

Thanks

Lional
Link to comment
Share on other sites

Make the default bg green and if the room is booked on a particular day, override the default
[code]
<style>
TD {background-color: #99FF99}
TD.booked {background-color: #FF9999}
</style>[/code]

then
[code]
$cls = $booked ? 'class="booked"' : '';
echo "<TD $cls> $day </TD>"[/code]
Link to comment
Share on other sites

I am actually looking for a very simple version of a calendar. Very much like Barend sugested with his crossed. each view must only show the current month with previous and next links to navigate between the months. The main criteria is that it must pull the dates from a tabe, the same structure that barend suggested and make the background red for the days that are booked, and green for the days that are not booked. I think the calandar that redarrow showed is a bit advanced for my knowledge of php at the moment, and also overkill for what I need.

Can anybody please help

Thanks

Lional
Link to comment
Share on other sites

Thanks redarrow,
I used that calender but the only probem I am still having is the changing of the backgroud colors.
when I query the database, only the last date is shown because the pevious is overwritten by the new one. I am fairly new to PHP and I m sure that it is something small that I am doing wrong.
please find a portion of my code
include 'includes/conn_db.php';
$query = "SELECT * from availability";
$result = mysql_query($query, $conn);
while ($row = mysql_fetch_array($result)){
$date_out = $row["booking_date"];


print $date_out;
}
if ($full_date_out == $date_out[date]) {
echo "<td bgcolor=red><b> $day </b></td>";
} else {
echo "<td bgcolor=#74f874><b> $day </b></td>";
}

Thanks

Lional
Link to comment
Share on other sites

I want the background to be colored and not the font. Thanks for your reply but the problem I am having with the method you showed is that it only picks up the last date of the query as the one overwrites the previous one. Maybe I am doing something wrong

Thanks

lional
Link to comment
Share on other sites

[quote author=redarrow link=topic=99163.msg390441#msg390441 date=1151837002]
Can you kindly give a code example please so i can learn cheers mate.
[/quote]

Didn't have time before but here's sample code. First the test data then the calendar script

[code]
CREATE TABLE room_booking (
`bookID` int(11)  NOT NULL auto_increment,
`clientID` int(11)  NOT NULL ,
`roomID` int(11)  NOT NULL ,
`arrive` date  NOT NULL DEFAULT '0000-00-00',
`depart` date  NOT NULL DEFAULT '0000-00-00',
`nights` int(11)  NOT NULL ,
`discount` varchar(100)  NOT NULL ,
`status` tinyint(1)  NOT NULL ,
PRIMARY KEY(bookID)
);

INSERT INTO room_booking VALUES ('1', '1', '1', '2006-06-30', '2006-07-02', '2', '0', '1');
INSERT INTO room_booking VALUES ('2', '2', '1', '2006-07-07', '2006-07-11', '3', '0', '2');
INSERT INTO room_booking VALUES ('3', '3', '1', '2006-07-14', '2006-07-17', '2', '0', '1');
INSERT INTO room_booking VALUES ('4', '4', '2', '2006-07-07', '2006-07-11', '3', '0', '2');
INSERT INTO room_booking VALUES ('5', '1', '2', '2006-07-14', '2006-07-19', '4', '0', '2');
INSERT INTO room_booking VALUES ('6', '3', '4', '2006-07-14', '2006-07-29', '14', '0', '3');
[/code]

Calendar script
[code]
<?php
include('db.php');

$cal_start = isset($_GET['cal_start']) ? $_GET['cal_start'] : mktime(0,0,0);
$cal_days = $bookings = array();
  // dates for calendar display
for ($i=0; $i<28; $i++) {
$cal_days[] = strtotime("+$i days", $cal_start);
}
// create room booking array
for ($r=1; $r<=4; $r++) {
$bookings[$r]=array();
foreach($cal_days as $cd) {
$bookings[$r][$cd] = 0;
}
}

$cal_end = end($cal_days);
  // get bookings for calendar period and put in array
$d1 = date('Y-m-d', $cal_start);
$d2 = date('Y-m-d', $cal_end);
$sql = "SELECT b.bookID, b.clientID, b.roomID, b.arrive, b.depart, b.status
FROM room_booking b
WHERE ((b.arrive BETWEEN '$d1' AND '$d2') OR
  (b.depart BETWEEN '$d1' AND '$d2'))
ORDER BY b.roomID, b.arrive";
$res = mysql_query($sql) or die(mysql_error());
while (list($bid, $cid, $rid, $arr, $dep, $stat) = mysql_fetch_row($res)) {
for ($d=strtotime($arr); $d<=strtotime("$dep -1 days"); $d+=86400) {
if (isset($bookings[$rid][$d])) $bookings[$rid][$d] = $stat;
}
}
  // calc prev and next weeks
$prevweek = strtotime('-7 days', $cal_start);
$nextweek = strtotime('+7 days', $cal_start);

function display_calendar (&$dates, &$bookings, $prev, $next) {
echo "<table cellspacing='0' cellpadding='0' >\n";
echo "<TR><TH><a href='?cal_start=$prev'>&lt;</a></TH>
<TH colspan='27'>Fawlty Towers Guest House<br>Room Bookings</TH>
<TH><a href='?cal_start=$next'>&gt;</a></TH></TR>";
// dates
echo "<TR>\n";
echo "<TH>Room</TH>" ;
foreach ($dates as $day) {
switch (date('w', $day)) {
case 0:
case 6: $class = 'class=wkend'; break;
default: $class = '';
}
echo "<TH $class>".date('M', $day).'<br>'.date('j', $day)."</TH>\n";
}
echo "</TR>\n";

// rooms
foreach ($bookings as $room => $rmdata) {
display_room_bookings ($room, $rmdata);
}
echo "</table><br>\n";
display_key();
}

function display_room_bookings ($room, $rmdata) {
echo "<TR><TH>$room</TH>\n";
foreach ($rmdata as $dt=>$status) {
switch ($status) {
case 1: $class = 'class=booked'; break;
case 2: $class = 'class=conf'; break; 
case 3: $class = 'class=paid'; break; 
default: $class = ''; break; 
}
echo "<TD $class>&nbsp;</TD>\n";
}
}

function display_key() {
echo '<table cellspacing="0">
<tr><td>Free</td>
<td class="booked">Booked</td>
<td class="conf">Confirmed</td>
<td class="paid">Paid</td>
</tr></table>';
}

?>
<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>Sample bookings calendar</title>
<meta name="author" content="Barand">
<meta name="creation-date" content="07/15/2006">
<style type='text/css'>
        table {
        border-left: 1px solid gray;
        border-top:  1px solid gray;
        width: 90%;
        }
        th {
        border-right: 1px solid gray;
        border-bottom:  1px solid gray;
        font-family: sans-serif;
        font-size: 0.75em;
        font-weight: 700;
        background-color: #E0E0E0;
        height : 20px;
        width: 3.5%
        }
        th.wkend {
        background-color: #C0C0C0;
        }
        td {
        border-right: 1px solid gray;
        border-bottom:  1px solid gray;
        font-family: sans-serif;
        font-size: 0.75em;
        font-weight: 300;
        text-align: center;
        width: 25px;
        height : 20px;
        background-color: #C0FFC0;
        }
        td.booked {
        background-color: #FFFFC0;
        }
        td.conf {
        background-color: #DFC868;
        }
        td.paid {
        background-color: #FF8080;
        }
</style>
</head>
<body>
<?php display_calendar ($cal_days, $bookings, $prevweek, $nextweek)?>
</body>
</html>
[/code]
Link to comment
Share on other sites

  • 6 months later...
Hi All
I have got the calendar to work but I have a small problem. The site I wrote is www.paternosterdunes.co.za. On the booking button there is a button to check for availability. This brings up this calendar program. My problem is that the live site does not go to the next month when the next button is pressed and the same for the previous button, but it works perfectly on my localhost.
Any ideas what the problem can be, thanks
Lional
Link to comment
Share on other sites

[quote author=lional link=topic=99163.msg510475#msg510475 date=1169473189]
My problem is that the live site does not go to the next month when the next button is pressed and the same for the previous button, but it works perfectly on my localhost.
Any ideas what the problem can be, thanks
Lional
[/quote]

What version of PHP/MySQL is your server running? Also, you may need to share your code (the portion where you create your next link and handle it).
Link to comment
Share on other sites

[quote author=lional link=topic=99163.msg510621#msg510621 date=1169482867]
They are runninh PHP5 with mysql version 4.1
What do you mean by sharing the code, I am fairly new to php so please explain
[/quote]

Sorry... I meant, you may need to share your code with us so we can help you troubleshoot it. As for versions... are you testing on the same versions that your host is running?
Link to comment
Share on other sites

No I am running php4 with mysql 4.0.
I will attach the code

[code]
<?php
// Check for a Month Change submission

if ($submit) {

// Subtract one from the month for previous, add one for next

if ($submit == "Prev") {
$month_now--;
} else {
$month_now++; }

$date = getdate(mktime(0,0,0,$month_now,1,$year_now));

} else {
$date = getdate();
}

$month_num = $date["mon"];
$month_name = $date["month"];
$year = $date["year"];
$date_today = getdate(mktime(0,0,0,$month_num,1,$year));
$first_week_day = $date_today["wday"];
$cont = true;
$today = 27;

while (($today <= 32) && ($cont)) {
$date_today = getdate(mktime(0,0,0,$month_num,$today,$year));

if ($date_today["mon"] != $month_num) {
$lastday = $today - 1;
$cont = false;
}

$today++;
}

// allow for form submission to the script for forward and backwards

echo"
<form action=\"cal.php\" method=\"POST\" name=\"calendar\">

<input type=\"hidden\" name=\"month_now\" value=\"$month_num\">
<input type=\"hidden\" name=\"year_now\" value=\"$year\">

<tr><td  align=left><input type=\"submit\" name=\"submit\" value=\"Prev\"></td>
    <td align=right><input
type=\"submit\" name=\"submit\" value=\"Next\"></td>
</tr>
</table>
<table width=530 height=5>
<tr>
<td align=center width=170>
<b>Rose room</b>
</td>
<td align=center width=170>
<b>Cream room</b>
</td>
<td align=center width=170>
<b>Turquoise room</b>
</td>
</tr>
</table>
</form>
<table width=\"170\" border=\"1\" cellspacing=0 cellpadding=2 align=left>
<tr><td colspan=\"7\">$month_name $year</td></tr>
<tr><td>Su</td><td>M</td><td>T</td><td>W</td><td>Th</td><td>F</td><td>Sat</td></
tr>";
[/code]

I hope this is enough of the code

Thanks

Lional
Link to comment
Share on other sites

My calenders seem to work on php4 but not on 5, I am not sure why.
Here is my code, can someone please give my some assistance

[code]
<?php
// Check for a Month Change submission

if ($submit) {

// Subtract one from the month for previous, add one for next

if ($submit == "Prev") {
$month_now--;
} else {
$month_now++; }

$date = getdate(mktime(0,0,0,$month_now,1,$year_now));

} else {
$date = getdate();
}

$month_num = $date["mon"];
$month_name = $date["month"];
$year = $date["year"];
$date_today = getdate(mktime(0,0,0,$month_num,1,$year));
$first_week_day = $date_today["wday"];
$cont = true;
$today = 27;

while (($today <= 32) && ($cont)) {
$date_today = getdate(mktime(0,0,0,$month_num,$today,$year));

if ($date_today["mon"] != $month_num) {
$lastday = $today - 1;
$cont = false;
}

$today++;
}

// allow for form submission to the script for forward and backwards

echo"
<form action=\"cal.php\" method=\"POST\" name=\"calendar\">

<input type=\"hidden\" name=\"month_now\" value=\"$month_num\">
<input type=\"hidden\" name=\"year_now\" value=\"$year\">

<tr><td  align=left><input type=\"submit\" name=\"submit\" value=\"Prev\"></td>
    <td align=right><input
type=\"submit\" name=\"submit\" value=\"Next\"></td>
</tr>
</table>
[/code]

Thanks

Lional
Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.