Forum Moderators: coopster

Message Too Old, No Replies

Color table cells for calendar

         

steveinspain

11:13 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



Hi I am working on a calendar for a rentals site where I want to change the color of table cells where a property is booked. It works if there is only 1 booking per month but if there is more it doesn't show them. Here is the full code.

<?php

mysql_select_db($database_myconn, $myconn);
$query_rst_booking = "SELECT * FROM start WHERE startmonth = 4 AND startyear = 2008 AND startpropid = 1";
$rst_booking = mysql_query($query_rst_booking, $myconn) or die(mysql_error());
$row_rst_booking = mysql_fetch_assoc($rst_booking);
$totalRows_rst_booking = mysql_num_rows($rst_booking);
//This gets today's date

$date =time () ;

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

//Here we start building the table heads
echo "<table border=0 width=196>";
echo "<tr bgcolor='#AB0000' style='color:#FFFFFF;'><th colspan=7> $title $year </th></tr>";
echo "<tr bgcolor='#E2E2E2'><td bordercolor='#E2E2E2' width=28>S</td><td width=28>M</td><td width=28>T</td><td width=28>W</td><td width=28>T</td><td width=28>F</td><td width=28>S</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></td>";
$blank = $blank-1;
$day_count++;
}
/sets the first day of the month to 1
$day_num = 1;

$total = $row_rst_booking['startday'];
//count up the days, untill we've done all of them in the month
$myarray = array();
while ( $day_num <= $days_in_month )
{

while($total <= $row_rst_booking['endday']){

$myarray[$total] = $total;
$total++;
}
if (in_array($day_num, $myarray)){

if ($row_rst_booking['startpending'] == 'Y'){
echo "<td bgcolor='#FFD393'>$day_num</td>";
}

if ($row_rst_booking['startreserved'] == 'Y'){
echo "<td bgcolor='#A4DBFF'>$day_num</td>";
}
if ($row_rst_booking['startconfirmed'] == 'Y'){
echo "<td bgcolor='#FFA6A6'>$day_num</td>";
}

}
else{
echo "<td bgcolor='#FFFFFF'> $day_num </td>";
}

$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> </td>";
$day_count++;
}
echo "</tr></table>";
?>

And here is the part that I am working on.
I have the start day and the end day in the database and I loop the start day till it = the end day and put this in an array. I can only get 1 record to show this way.

$total = $row_rst_booking['startday'];
//count up the days, untill we've done all of them in the month
$myarray = array();
while ( $day_num <= $days_in_month )
{

while($total <= $row_rst_booking['endday']){

$myarray[$total] = $total;
$total++;
}
if (in_array($day_num, $myarray)){

if ($row_rst_booking['startpending'] == 'Y'){
echo "<td bgcolor='#FFD393'>$day_num</td>";
}

if ($row_rst_booking['startreserved'] == 'Y'){
echo "<td bgcolor='#A4DBFF'>$day_num</td>";
}
if ($row_rst_booking['startconfirmed'] == 'Y'){
echo "<td bgcolor='#FFA6A6'>$day_num</td>";
}

}
else{
echo "<td bgcolor='#FFFFFF'> $day_num </td>";
}

I need some help here please.

fabricator

3:55 pm on Apr 22, 2008 (gmt 0)

10+ Year Member



Its a simple problem, but with a slightly complex solution.

$row_rst_booking = mysql_fetch_assoc($rst_booking);
Only retrieves one row from the database, and you need to add a loop to get all of them.

Change your query to this:

$query_rst_booking = "SELECT * FROM start WHERE startmonth = 4 AND startyear = 2008 AND startpropid = 1 ORDER BY endday ASC";

That will now sort by 'endday' in ascending order. Should give you a basis for the rest of your code.