Show date of last modification

Show date of last modification

Postby Dr.Watson » Mon Mar 30, 2009 6:47 pm

Hi Chris,

first of all thanks for sharing this wonderful script. It's just what I have been looking for!

One thing I miss and I would like to implement is the date of the last changes to the availability calendar.

So if the owner changes some days in the calendar today -> on the bottom of the calendar it shows up "Last updated 30.03.2009"

I was thinking of adding a new column to the table bookings_calendar like "last_update" as date and when modifications are done this column would be feed with todays date.

When displaying the calendar it would read the max_date of the "last_update" column...

Well the ideas are here - but the knowledge of Ajax not :roll:

Could you provide me some help on that?

Regards
Dr.Watson
Dr.Watson
 
Posts: 2
Joined: Sat Mar 28, 2009 11:57 am

Re: Show date of last modification

Postby chris » Mon Mar 30, 2009 7:41 pm

HI there and thanks for the comments.

Having a last updated date would of course be very useful and not at all hard to implement.

I suggest that you create a new table as this info has nothing really do with the actual bookings.

To be able to cope with the multiple "item" feature of this calendar script I would create a table called (eg) calendar_updates with 3 fields:
id (INT,11) autoincrement
id_item (INT,11)
date_update (datetime)

Then, in the ajax file you update the datestamp with a simple mysql alter code. something like this:
Code: Select all
// check if this is the first update
$sql="SELECT id FROM date_update WHERE id_item='".$_REQUEST["id_item"]."'";
$res=mysql_query($sql) or die("error checking update time");

//if exists update it
if(mysql_num_rows($res)==1) $update="UPDATE date_update SET date_update=now WHERE id=".$_REQUEST["id_item"]."";
// else add new entry
else $update="INSERT INTO date_update SET id_item=".$_REQUEST["id_item"].", date_update=now()";

//execute update query
mysql_query($update) or die("Error modifying update time");


Finally you would want to show this on the calendar page like this:
Code: Select all
$sql="SELECT date_update FROM date_update WHERE id_item='".$_REQUEST["id_item"]."'";
$res=mysql_query($sql) or die("error checking update time");
$row=mysql_fetch_assoc($res);
echo $row["update_time"];


Please note that I haven't tested any of that code so I can't promise that there aren't any minor errors in it but hopefully you get the idea.
You don't actually need to make any changes to any ajax or javascript files as it is done in the same file that updates the date state.

I hope this helps :)

Chris
User avatar
chris
Site Admin
 
Posts: 1089
Joined: Mon Dec 17, 2007 7:42 pm
Location: Málaga, Spain

Re: Show date of last modification

Postby Dr.Watson » Thu Apr 02, 2009 1:07 pm

Hi Chris,

thanks a lot for the fast and good support. :)

If others need the same addon here is how I implemented your code: ;) (some typing errors are fixed)

in the index.php at about line 116:
Code: Select all
$sql="SELECT date_update FROM calendar_updates WHERE id_item='1'";
$res=mysql_query($sql) or die("error checking update time");
$row=mysql_fetch_assoc($res);

$date_array = explode("-", substr($row["date_update"],0,-9)); // split the array

$var_year = $date_array[0];
$var_month = $date_array[1];
$var_day = $date_array[2];
$last_updated= $var_day.".".$var_month.".".$var_year;


in the file update_cal.ajax.php at about line 64 right after where the } IF of the get current state ends

Code: Select all
    // check if this is the first update
  $sql="SELECT id FROM calendar_updates WHERE id_item='1'";
  $res=mysql_query($sql) or die("error checking update time");

  //if exists update it
  if(mysql_num_rows($res)==1) $update="UPDATE calendar_updates SET date_update=now() WHERE id='1'";
  // else add new entry
  else $update="INSERT INTO calendar_updates SET id_item='1', date_update=now()";

  //execute update query
  mysql_query($update) or die("Error modifying update time");


In my example I use the ID=1 just to track the over all "last updated date" and the result is converted to the european format like "18.05.2009"

Regards
Dr.Watson
Dr.Watson
 
Posts: 2
Joined: Sat Mar 28, 2009 11:57 am

Re: Show date of last modification

Postby chris » Thu Apr 02, 2009 1:29 pm

Thanks, as I said, that code was untested and typed directly into this forum.
Thanks for the "correct" version anyway :) I am glad that you got it working.

Just a tip - I normally format the date directly in the mysql query like this:
Code: Select all
SELECT DATE_FORMAT(date_update, '%d-%m-%Y %H:%i:%s') as date_update FROM db_table etc.

Not actually sure if that is any more efficient than doing it via php but does keep the code down ;)
(that demo actually returns the time as well)

Whilst I see why you have chosen to update the date using a common id, I would still recommend using the passed id_item as this allows each item to have it's own update time - especially useful for (eg.) properties that are managed by different users

Chris
User avatar
chris
Site Admin
 
Posts: 1089
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