Jump to content


Photo

Calendars


  • Please log in to reply
22 replies to this topic

#1 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 02 July 2006 - 07:50 AM

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

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 July 2006 - 08:09 AM

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.

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

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 02 July 2006 - 09:59 AM

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]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 July 2006 - 10:43 AM

Can you kindly give a code example please so i can learn cheers mate.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 02 July 2006 - 11:12 AM

Sure, give me 5 minutes and i'll design and create a reservations database and write a bookings application.

On second thoughts, make that 10 mins  ::)
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 July 2006 - 11:31 AM

Thats nice of you cheers, i keep it in a folder to study so please use as meny comments as possable cheers mate your a star.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 02 July 2006 - 11:38 AM

ROFL
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 07 July 2006 - 02:08 PM

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

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 07 July 2006 - 02:24 PM

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

then
$cls = $booked ? 'class="booked"' : '';
echo "<TD $cls> $day </TD>"

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 07 July 2006 - 05:06 PM

How will I pull the info from the database. I only know the basics of php so I am struggling a bit with the calendar.
Where do I insert the code you mentioned

Appreciate any help

Thanks
Lional

#11 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 07 July 2006 - 05:23 PM

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

#12 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 July 2006 - 07:41 PM

there you go al the information make your grate callender

the link
http://www.zend.com/...endar-ver7f.php
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#13 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 11 July 2006 - 12:56 PM

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

#14 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 11 July 2006 - 05:08 PM

I am sure you mean font color here you go mate.

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

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#15 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 11 July 2006 - 05:25 PM

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

#16 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 July 2006 - 09:20 PM

Can you kindly give a code example please so i can learn cheers mate.


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

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');

Calendar script
<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#17 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 22 January 2007 - 01:39 PM

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

#18 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 22 January 2007 - 01:44 PM

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


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).
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#19 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 22 January 2007 - 04:21 PM

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

Thanks a stack

Lional

#20 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 22 January 2007 - 04:34 PM

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


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?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users