PHP SQL query - trying to change class on calendar if a date is available

I’m trying to create a laptop system where you can see if a laptop is booked out already, I have a MySQL table with the columns approved_start_date (as datetime) and approved_end_date (as datetime) and laptop_id (1,2,3,4). I have the booking system all working but would like to be able to show the availability of the laptops on the days. The calendar is literally just for display purposes to show availbility not to actually do anything else.

I have this calendar -

/* draws a calendar */
function draw_calendar($month,$year,$events = array()){

/* draw table */
$calendar = β€˜

’;

/* table headings */
$headings = array(β€˜Su’,β€˜Mo’,β€˜Tu’,β€˜We’,β€˜Th’,β€˜Fr’,β€˜Sa’);
$calendar.= β€˜

’;

/* days and weeks vars now … */
$running_day = date(β€˜w’,mktime(0,0,0,$month,1,$year));
$days_in_month = date(β€˜t’,mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= β€˜

’;

/* print β€œblank” days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
$calendar.= ’

';
$days_in_this_week++;
endfor;

/* keep going with days… /
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
$calendar.= β€˜

’;
if($running_day == 6):
$calendar.= β€˜

’;
if(($day_counter+1) != $days_in_month):
$calendar.= β€˜β€™;
endif;
$running_day = -1;
$days_in_this_week = 0;
endif;
$days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < 8):
for($x = 1; $x <= (8 - $days_in_this_week); $x++):
$calendar.= β€˜

’;
endfor;
endif;

/* final row */
$calendar.= β€˜

’;

/* end the table */
$calendar.= β€˜

’.implode(’ ’,$headings).’
’;
/ add in the day number */
$calendar.= β€˜
’.$list_day.’
’;

$calendar.= β€˜

’;

/** DEBUG **/
$calendar = str_replace(’’,’’."\n",$calendar);
$calendar = str_replace(’’,’’."\n",$calendar);

/* all done, return result */
return $calendar;
}

function random_number() {
srand(time());
return (rand() % 7);
}

// Create date object to store the DateTime format
$dateObj = DateTime::createFromFormat(’!m’, $month);

// Store the month name to variable
$monthName = $dateObj->format(β€˜F’);

/* sample usages */
echo β€˜

’. $monthName . ’ ’ . $year . β€˜

’;

// echo β€˜

’.date(β€˜F’,mktime(0,0,0,$month,1,$year)).’ β€˜.$year.’

’;
echo draw_calendar($month,$year,$events);

I basically want to show if all 4 laptops are booked out for the days to be a different colour, if all 3 a different colour and so on.

Any help would be really appreciated!! :slight_smile:

It would help a lot if you place your code between the </> code tags!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service