Gossamer Forum
Home : Products : DBMan : Customization :

Re: [LoisC] comparing dates to get a number

Quote Reply
Re: [LoisC] comparing dates to get a number In reply to
Hi

I am setting the database up as a kind of virtual pet game for a school website. Ive got everything working but would like to have the pet appear to have developed while the user is away. So far the user can add food etc using modify_record forms and see how old their pet is and when they last visited.

I dont think having the dates update during login is strictly necessary but could be updated after the login with a modify_record form including hidden fields for the dates. At the moment I have modified the cgi file as follows
Code:

sub modify_record {


my ($status, $line, @lines, @data, $output, $found, $restricted);

$in{'food'} = $in{'food'} + $in{'addfood'};


$status = &validate_record; # Check to make sure the modifications are ok!

The fields are set up as follows
Code:
%db_def = (
ID => [0, 'numer', 5, 8, 1, '', ''],
body => [1, 'alpha', 0, 3, 0, '', ''],
arm => [2, 'alpha', 0, 3, 0, '', ''],
leg => [3, 'alpha', 0, 3, 0, '', ''],
head => [4, 'alpha', 0, 3, 0, '', ''],
Userid => [5, 'alpha', -2, 15, 0, '', ''],
food => [6, 'numer', 5, 8, 1, '20', ''],
addfood => [7, 'numer', 5, 8, 0, '20', ''],
happiness => [8, 'numer', 5, 8, 0, '20', ''],
weight => [9, 'numer', 5, 8, 0, '20', ''],
date => [10, 'date', 15, 18, 0, &get_date, ''],
currentdate => [11, 'date', 15, 18, 0, &get_date, '']

);

So at the moment the "food" field is updated when food is added during a session which works fine. I have also put the following into html.pl

Code:
sub html_record_form {


my (%rec) = @_;
my (%rec) = @_;
if ($in{'modify_form_record'}) {
$rec{'date'} = $rec{'currentdate'};
$rec{'currentdate'} = &get_date;

which also works fine and swaps the dates so that the date modified is kept in the date field and the current date is added to the currentdate field

so what I thought was, if I could calculate the difference in the dates I could make it appear to the user that the food had decreased or increased since they last logged in.
if I added hidden fields to a seperate modify_record form then i could get the user to click on a button which calculates the dates and adds food to the food field as below. That way they would log in first, reach a page with a button, which updates the fields then takes them to their record. That way I think the code can be left in the modify_record section?

Code:

sub modify_record {


my ($status, $line, @lines, @data, $output, $found, $restricted);

$compare1 = &date_to_unix($rec{'date'});
$compare2 = &date_to_unix($rec{'currentdate'});
$total = $compare2-$compare1;
$in{'food'} = $in{'food'} + $in{'addfood'} + $total;


$status = &validate_record; # Check to make sure the modifications are ok!

I hope this makes sense, I have no experience with perl (just a designer) so am guessing the code based on other posts -which is one way to learn.

thanks
carl

Last edited by:

carlstevenson: Nov 4, 2002, 1:25 PM
Subject Author Views Date
Thread comparing dates to get a number carlstevenson 3240 Nov 2, 2002, 1:12 PM
Thread Re: [carlstevenson] comparing dates to get a number
LoisC 3172 Nov 4, 2002, 9:17 AM
Thread Re: [LoisC] comparing dates to get a number
carlstevenson 3163 Nov 4, 2002, 1:17 PM
Thread Re: [carlstevenson] comparing dates to get a number
carlstevenson 3155 Nov 4, 2002, 3:01 PM
Post Re: [carlstevenson] comparing dates to get a number
LoisC 3164 Nov 5, 2002, 9:19 AM