Recomended update to version 2

Recomended update to version 2

Postby chris » Wed Jan 07, 2009 1:15 pm

Hello all,
I know that I have been promising for a long time that I would be creating a new version of this calender, implementing many of the requests that have been made in this forum and adding new features.
However, as some of you know, I have been very busy working on clients projects over the last few months and have really only had time to answer the forum questions (if I have been able to)
Unfortunately, (depending on how you look at it) I am still very busy with work so I can't see when I will be able to do this in the foreseeable future.

However, there is one modification that I recommend that everyone makes to the code.
Whilst not a functional problem, there is a flaw in the way the code gets the booked dates from the database.
As is, the first thing that the code does is get the booked dates from the database regardless of the months and/or years being shown. This this is not "economic" in database terms as it means that we are getting info for dates that are not being shown. Clearly as time passes we will have more and more "past" dates that are generally of no interest (unless we are specifically requesting past months).
What we "should" be doing is just getting the dates for the months being shown.
Fortunately the solution is quite simple.
We need to move the main db query made within index.php (the page that shows the calendar) to within the draw_cal() function in functions.inc.php and add a condition so that it only checks the month being requested. I must add that this method now means that you would be making a call for each of the months being shown but I think that this method is more efficient than getting all the dates in the database.... maybe some mysql expert could correct me.... ;)

So, here is what you need to do.
1. remove the query and array from the index.php (or whatever your main calendar file is called:
Code: Select all
//   get bookings for this month for this advert
$booked_days   =   array();
$sql="SELECT * FROM ".T_BOOKINGS." WHERE id_item=".ID_ITEM."";
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"];
}

For the sake of reducing php warnings, seeing as we are still passing the variable in the function, we should leave a variable called $booked_days like this:
Code: Select all
$booked_days=true;

Ideally we should actually remove this parameter from the funcion draw_cal() as it is now not being used. If you do so, remember to remove it from the function itself in functions.inc.php.

Now we have removed this function, if you load the calendar in your browser you will see that it still works but shows no booked dates - this is correct :)
Now, in functions.inc.php, we need to get the booked dates for the month being shown with the aforementioned conditions for month and year.
In the function draw_cal(), pretty much at the start (eg. after global $lang;), add the following lines:
Code: Select all
//   NEW - get booked days for month selected
$sql="SELECT * FROM ".T_BOOKINGS." WHERE id_item=".$id." 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"];
}

Hopefully the code is pretty simple to follow. It is basically the same as the code that we have just removed from the main file, with the month and year being checked according to the vars passed in the function.
There is no need to change anything else as we have used the same name for the array of booked days - ie $booked_days.

Well, this is NOT a necesarry change to the code but I do recomend that you do it to make life easier on your server and database :)

Chris
PS I will update the zip file to use this system now so anybody who downloads this script after this post can ignore this post :)
User avatar
chris
Site Admin
 
Posts: 1088
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron