View Full Version : Really stuck with CGI
Ricey
04-15-2005, 11:42 AM
Hi
I am trying to get my script (booking form) to search for the next available date in a database. The script should take in a start date and finish date and search threw my database to find the next free date
The database (events.dat) has the following layout:
1111:Leinster:john:12/12/05 (id:town:name:date)
1111:Leinster:john:19/12/05
1111:Leinster:john:15/12/05
so if I was to put in 12/12/05, and 15/12/05 it should come back with 13/12/05, does anyone have any sample code or could anyone help me plz
thank you :thumbsup:
John
Ricey
04-15-2005, 11:54 AM
Also I am using the below code to open the file and process the date
&ReadParse(*input);
$room = $input{'ROOM'};
$start = $input{'START'};
$finish = $input{'FINISH'};
$count=0;
$fin;
# open the file
open(EVENTSIN, "events.dat") or die "Couldn't open events.dat";
while(<EVENTSIN>)
{
# read in each line
$Record = $_;
# clean it up
chomp($Record);
# split it apart
($Key, $Value) = split(/:/, $Record,4);
if($Value eq $room)
{
($Key1, $info) = split(/:/,$Record,2);
$info =~ /.(\d{2})\/(\d{2})\/(\d{2})$/;
$rep = $3.$2.$1;
$start =~ /^(\d{2})\/(\d{2})\/(\d{2})/;
$sta = $3.$2.$1;
$sta += $count;
$finish =~ /^(\d{2})\/(\d{2})\/(\d{2})/;
$fin = $3.$2.$1;
if($sta == $rep)
{
$count +=1;
}
elsif(($sta > $rep)&&($count==0))
{
$rep += 1;
$sta = $rep;
}
elsif(($sta < $rep)&&($count==0))
{
$sta =~ /^(\d{1})(\d{2})(\d{2})/;
$free = $3."/".$2."/"."0".$1;
print $free <br />;
# return 0;
}
}
}
close(EVENTSIN);
if($count>0)
{
$sta += 1;
if($sta > $fin)
{
CgiError("All Booked Up!!!");
# return 0;
}
}
$sta =~ /^(\d{1})(\d{2})(\d{2})/;
$free = $3."/".$2."/"."0".$1;
print $free <br />;
mlseim
04-15-2005, 01:03 PM
Ricey.
One other thing first.
Usually, with flat-file databases, it's best to separate fields
using pipes "|" instead of colons ":". You might end up wanting
to save a time value that has colons in it. Pipes "||||" are
usually never used for anything else.
For date stuff, I usually find it easiest to convert my date to
Julian Dates. This makes calculations and comparisons very easy.
Here's an example that also has a link to display the script:
http://www.catpin.com/cgi-bin/julian.pl
As far as formatting, you'll only need to swap the day and month
variables around because you are using DD/MM/YY instead of the
American MM/DD/YY. And, you might have to append "20" in front
of your 2 digit year.
You'll put the subroutines at the bottom of your script and call them
when needed to convert the dates from your database to Julian.
You might want to even save both the normal date DD/MM/YY and
the Julian Date in your database.
The Julian Dates end up being integers that you can add-to,
subtract-from, compare, etc.
If you really get stuck, let me know. But I think you'll find this
amazingly easy to compare and manipulate dates.
Jeff Mott
04-18-2005, 01:51 PM
Usually, with flat-file databases, it's best to separate fields
using pipes "|" instead of colons ":". You might end up wanting
to save a time value that has colons in it. Pipes "||||" are
usually never used for anything else.Any decent database should be able to store any arbitrary data. That means that any characters special to the DB implementation should be escaped in some way.
Though if you want to minimize the number of escapes then you would be much better off choose a non-printable character as a separator, 0x1F for example.
(Pipes are in fact very frequently used. Consider a database that would store messages posted to some kind of coding board. The pipe is the common operator for OR.)
Ricey
04-19-2005, 10:59 AM
Thank you for your help, Im working on that now and hopefully it will all come together :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.