// Saturday start
$d=6; # Saturday is number 6
for($k=1; $k<=7; $k++){
$the_cal.='<td class="cal_title" width="'.$td_width.'"> '.substr($lang["days"][$d],0,1).'</td>';
++$d;
$d %= 7; # reset to 0
}
then:
$first_week_day = $first_week_day+1;
and finally:
if($first_week_day<6){
for($week_day = 0; $week_day <= $first_week_day; $week_day++){
$the_cal.='<td class="cal_empty">'.$cal_no_day.'</td>';
$j++;
}
}
The first was originally set to '5' for a Friday, hence '6' seems like it should be right for a Saturday. The second one....no idea. The latter one I've tried all variations from 1 to 7 to replace the '6', all to no avail.
Chris - can you see where I'm going wrong based on the above?
Thanks so much.
Richard
FYI: here's the complete code from the functions.inc.php file I'm using. I've marked out the 'old' code using // and the new code as above is directly below those sections, hence relatively easy to find. My site is just about done now, hence I'll shortly post a link to where people can (if interested) see the working calendar (v2).
- Code: Select all
application/x-httpd-php sat_start_functions.inc.php
PHP script text
<?php
/*
script: Ajax availability calendar
version: 2.1
author: Chris Bolson
web: http://www.cbolson.com/
date: 2008-03-26
file: functions.inc.php
use: common functions for all pages
inst:
*/
// create calendar for given month
function draw_cal($id,$month, $year, $booked_days,$show_link){
global $lang;
// get bookings for this month for this advert
$booked_days = array();
$sql="SELECT * FROM ".T_BOOKINGS." WHERE id_item=".ID_ITEM." AND MONTH(the_date)=".$month." AND YEAR(the_date)=".$year."";
if(!$res=mysql_query($sql)) die("ERROR checking id item availability dates");
while($row=mysql_fetch_array($res)){
$booked_days[$row["the_date"]]=$row["state"];
}
$month_name = $lang["months"][($month-1)];
$td_width = "20";
$cur_month = date('m');
$cur_day = date('d');
$this_month = getDate(mktime(0, 0, 0, $month, 1, $year));
$next_month = getDate(mktime(0, 0, 0, $month + 1, 1, $year)); # needed to draw cal
//$last_month = getDate(mktime(0, 0, 0, $month - 1, 1, $year));
//Find out when this month starts and ends.
$first_week_day = $this_month["wday"];
$days_in_this_month = round(($next_month[0] - $this_month[0]) / (60 * 60 * 24));
$the_cal='
<table class="cal_month" border=0 cellspacing=1 cellpadding=0>
<thead>
<tr>
<td colspan="7">'.$month_name.' '.$year.'</td>
</tr>
</thead>
<tbody>
<tr>
';
// if(CAL_START_DAY=="sun"){
// //$cal_row_counter=0;
// for($k=0; $k<7; $k++){
// $weekday = substr($lang["days"][$k],0,1);
// $the_cal.='<td class="cal_title" width="'.$td_width.'"> '.$weekday.'</td>';
// }
// }else{
// //$cal_row_counter=1;
// if ($first_week_day == 0) $first_week_day =7;
// for($k=1; $k<=7; $k++){
// if($k==7) $weekday = substr($lang["days"][0],0,1);
// else $weekday = substr($lang["days"][$k],0,1);
// $the_cal.='<td class="cal_title" width="'.$td_width.'"> '.$weekday.'</td>';
// }
// }
// Saturday start
$d=6; # Saturday is number 6
for($k=1; $k<=7; $k++){
$the_cal.='<td class="cal_title" width="'.$td_width.'"> '.substr($lang["days"][$d],0,1).'</td>';
++$d;
$d %= 7; # reset to 0
}
$the_cal.='
</tr>
<tr>
';
//Fill the first week of the month with the appropriate number of blanks.
$j=1;
// if(CAL_START_DAY=="sun") $first_week_day_start = $first_week_day; # start sunday
// else $first_week_day = $first_week_day-1; # current version starts // monday
$first_week_day = $first_week_day+2;
// if($first_week_day!=7){
// for($week_day = 0; $week_day < $first_week_day; $week_day++){
// $the_cal.='<td class="cal_empty">'.$cal_no_day.'</td>';
// $j++;
// }
// }
if($first_week_day<6){
for($week_day = 0; $week_day <= $first_week_day; $week_day++){
$the_cal.='<td class="cal_empty">'.$cal_no_day.'</td>';
$j++;
}
}
$week_day=$j;
for($day_counter = 1; $day_counter <= $days_in_this_month; $day_counter++){
$this_date = $year."-".sprintf("%02s",$month)."-".sprintf("%02s",$day_counter);
$date_format = $day_counter."/".$month."/".$year;
// check if day is available
if(array_key_exists($this_date,$booked_days)){
//echo "<br>".$booked_days[$this_date];
if($booked_days[$this_date]=="am") $day_image='cal_am_'.$day_counter.'.png';
elseif($booked_days[$this_date]=="pm") $day_image='cal_pm_'.$day_counter.'.png';
elseif($booked_days[$this_date]=="pr") $day_image='cal_pr_'.$day_counter.'.png';
else $day_image='cal_on_'.$day_counter.'.png';
}
else{
$day_image='cal_off_'.$day_counter.'.png';
}
// show ajax link or not
if($show_link==1){
$day_link_start ='<a href="#" onclick="ajax_mod_state(\''.$id.'\',\''.$this_date.'\',\''.$day_counter.'\',event); return false" title="'.$lang["modify_availability_for"].' '.$lang["day_".$week_day.""].' '. $date_format.'" class="cal_day">';
$day_link_end ='</a>';
}else{
$day_link_start ="";
$day_link_end ="";
}
// highlight current day
if( ($day_counter==$cur_day) && ($month==$cur_month) ) $border='style="border: 2px solid #99cc00"';
else $border="";
$the_cal .= '
<td class="cal_data" '.$border.'>
'.$day_link_start.'
<span id="state_'.$this_date.'">
<img src="'.DIR_IMAGES.''.$day_image.'" alt="'.$lang["day_".$week_day.""].' '. $date_format.'" border="0" width="20px" height="20px">
</span>
'.$day_link_end.'
</td>
';
// end row if Sunday
$week_day %= 7;
if($week_day == 0){
$the_cal .= "</tr><tr>";
//++$cal_row_counter;
}
$week_day++;
}
if($week_day > 1){
for($till_day = $week_day; $till_day <=7; $till_day++){
$the_cal .= '<td class="cal_empty">'.$cal_no_day.'</td>';
}
}
// add extra rows to make 6 so calendars are the same size - NOT working on height - needs trans
/*
echo "<br>".$cal_row_counter;
for($i=$cal_row_counter; $i<6;$i++){
$the_cal .= '<tr><td class="cal_empty" colspan="7"><img src="'.DIR_IMAGES.'/trans.png" height="20" width="10">as</td></tr>';
}
*/
$the_cal.="
</tbody>
</table>
";
return $the_cal;
}
// check table is created
function mysql_is_table($tbl){
$tables = array();
$sql="SHOW TABLES";
$res=mysql_query($sql);
while ($r = mysql_fetch_array($res)) {
$tables[] = $r[0];
}
if (in_array($tbl, $tables)) return TRUE;
else return FALSE;
}
?>