Jump to content

[SOLVED] Calendar booking


Alexhoward

Recommended Posts

Hi Guys!

 

I'm trying to create a simple calendar booking form, just for a bit of an education at the moment...

 

I'm trying to display one result for if we're booked, and one for when we're available..

 

I've got the calendar working but am having trouble with the "we're booked" part..

 

the problem being that it will only pull back one result, and if i move the while loop further it will duplicate cells, or the entire table...

 

any suggestions would be much appreciated!

 

thanks in advance

 


<?php


include ('config.php');

//connect to the mysql server
$link = mysql_connect($host, $db, $pass) or die('Could not connect to mysql because ' . mysql_error());

//select the database	
mysql_select_db($db) or die('Could not select database because ' . mysql_error());

$query = ' SELECT * FROM book';

$result = mysql_query($query);

while ($array= mysql_fetch_array($result)) {

$booked = $array['date_req'];


}


//This gets today's date 

if(!isset($_GET['date'])) { $_GET['date'] = time() ; }

$date = $_GET['date'] ; 

//This puts the day, month, and year in seperate variables 
$day = date('d', $date) ; 
$month = date('m', $date) ; 
$year = date('Y', $date) ;

//Here we generate the first day of the month 
$first_day = mktime(0,0,0,$month, 1, $year) ; 

//This gets us the month name 
$title = date('F', $first_day) ; 

//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day) ; 

//Once we know what day of the week it falls on, we know how many blank days occure before it.
//If the first day of the week is a Sunday then it would be zero
switch($day_of_week){ 
case "Sun": $blank = 0; break; 
case "Mon": $blank = 1; break; 
case "Tue": $blank = 2; break; 
case "Wed": $blank = 3; break; 
case "Thu": $blank = 4; break; 
case "Fri": $blank = 5; break; 
case "Sat": $blank = 6; break; 
}

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 

$days_left = ($days_in_month - $day) ;
$days_gone = $day ;

$next = $date + (($days_left + 1) * 24 * 60 * 60) ;
$prev = $date - (($days_gone + 1) * 24 * 60 * 60) ;


//Here we start building the table heads 
echo "<table border=1 width=700>";
echo "<tr><th colspan='7'><a href=availability.php?date=$prev style='text-decoration:none; color:#000000;'><<</a> $title $year <a href=availability.php?date=$next style='text-decoration:none; color:#000000';>>></a></th></tr>";
echo "<tr><td width=100>Sun</td><td width=100>Mon</td><td width=100>Tue</td><td width=100>Wed</td><td width=100>Thur</td><td width=100>Fri</td><td width=100>Sat</td></tr>";

//This counts the days in the week, up to 7
$day_count = 1;

echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 ) 
{ 
echo "<td bgcolor='#CCCCCC'> </td>"; 
$blank = $blank-1; 
$day_count++;
} 

//sets the first day of the month to 1 
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month ) 
{ 



$booked_date = explode('-', $booked) ;

if($booked_date[0] == $year && $booked_date[1] == $month && $booked_date[2] == $day_num ) {

$book = 'Apologies we are booked <br> <a href=who.php>more info</a>' ;
$colour = 'bgcolor=#A4A4FF' ;

} else {

$book = 'Available<p><input type=submit name=book value=book us>' ;
$colour = 'bgcolor=#A6FFA6' ;

}


echo "
<form>
<td height='100' width='100' align='left' valign='top' $colour> <strong>$day_num</strong> 
<input type='hidden' name='when' value='$year-$month-$day_num'>
<input type='hidden' name='date' value='$date'>
<p>

<div align='center'>


$book


</div>
</td>
</form>"; 	


$day_num++; 
$day_count++;


//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 



//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 ) 
{ 
echo "<td bgcolor='#CCCCCC'> </td>"; 
$day_count++; 
} 

echo "</tr></table>"; 

mysql_close($link) ;



?>

Link to comment
https://forums.phpfreaks.com/topic/134083-solved-calendar-booking/
Share on other sites

OK,

 

let me narrow things down a bit...  ;)

 

the calendars fine, and it displays ok (not great design but it works)

 

i have an MySQL table that stores the date required

 

i then want to be able to pull the dates, and display a "booked" results in that day on the calendar.

 

the issue lies here :

 

<?php

//connect to the mysql server
$link = mysql_connect($host, $db, $pass) or die('Could not connect to mysql because ' . mysql_error());

//select the database	
mysql_select_db($db) or die('Could not select database because ' . mysql_error());

$query = ' SELECT * FROM book';

$result = mysql_query($query);

while ($array= mysql_fetch_array($result)) {

$booked = $array['date_req'];


}

?>

 

as i am only pulling the loop once to stop the table being duplicated, or a cell i only get the last result in the table, i only get on booking displayed...

 

can anyone help me to do this properly?

 

thank again!

Forgive me if I'm wrong, but if there are more than one bookings, the only thing this piece of code below will achieve is overwrite the variable $booked each time.

 

while ($array= mysql_fetch_array($result)) {

   $booked = $array['date_req'];
   
}

 

Try using an array:

 

$booked = array();
while ($array= mysql_fetch_array($result)) {
   array_push($booked,$array['date_req']); // adding each date_req to array
}

Hey!

 

thanks fot the reply

 

i'm getting the error:

 

array_push() [function.array-push]: First argument should be an array

 

with that...

 

maybe i should try foreach as i suggested to the other guy...

 

didn't think of it before?!?

 

would that work?

 

$booked = array();
while(condition)
{
  $booked[] = 'item';
}

 

 

alright so i've got this working like :

 

$booked = array();

while($array= mysql_fetch_array($result))
{
   $booked[] = $array['date_req'];
}

 

but i can't get them all at once, therefore i can only do :

 

$booked_date = explode('-', $booked[0]) ;

 

to pull one result at a time...

 

somehow i need to pull them all out at once in one loop and display the booked dates in the correct dates on the calendar...

 

thanks for getting me this far,

 

any other suggestions...?

I've sorted it!!!

 

So what i done was use the loop that gets the days, then selected by that day to see if there was a booking.

 

if the num rows come back with 1 (as you can only book one on a given day)

 

then = "Booked"

 

if zero

 

Then = "Available!"

 

Woo!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.