Gossamer Forum
Home : Products : DBMan : Customization :

Listing the next event in an IFRAME (Code included)

Quote Reply
Listing the next event in an IFRAME (Code included)
I wanted to be able to load the next event from my event database in an IFRAME on my main page. Here is the code to do it if anyone's interested.

Hope this is of help to someone. See www.bandsman.co.uk to see it in action.

Change view_records in db.cgi:

Code:
sub view_records {
# --------------------------------------------------------
# This is called when a user is searching the database for
# viewing. All the work is done in query() and the routines just
# checks to see if the search was successful or not and returns
# the user to the appropriate page.

# NJH (njh@bandsman.co.uk)
if($in{'listnext'}) {
# Suitable for use in an IFRAME

if (!$html_headers_printed) {
print "Content-type: text/html\n\n";
$html_headers_printed = 1;
}

my @match;

my $today = &date_to_unix(&get_date(time()));
my $matchdate = 0;

open (DB, "<$db_file_name") or &cgierr("error in search. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE; # Skip comment Lines.
(/^\s*$/) and next LINE; # Skip blank lines.
my $line = $_; chomp ($line); # Remove trailing new line.
my @values = &split_decode($line);

# Ignore any before today, 12 is my date field
my $thisdate = &date_to_unix($values[12]);

($thisdate > $today) or next LINE;

# TODO: time as well as date
if(($matchdate == 0) || ($thisdate < $matchdate)) {
@match = @values;
$matchdate = $thisdate;
}

($thisdate == $today) and last LINE;
}

close DB;

if($matchdate > 0) {
print "<HTML>";
print "<BODY BGCOLOR=\"#FFFFFF\">";
print "<FONT SIZE=\"2\"><I>Next event:<BR>";
# Print date, desc and URL
print "$match[12]: $match[2]<BR>";

print "<A HREF=\"";

if ($match[9] ne "") {
my $url = $match[9];

$url =~ s/^http:\/\///;

print "../bbl2.cgi?redir=$url&address=";
print "$match[11]\" target=\"_top\">$match[1]";
} else {
print "mailto:$match[11]\" target=\"_top\">$match[1]";
}
print "</A></FONT></I></BODY></HTML>\n";
}
} else {
my ($status, @hits) = &query("view");
if ($status eq "ok") {
&html_view_success(@hits);
}
else {
&html_view_failure($status);
}
}
}

You then need to mark up the page it's to be included in (in my case my index.htm)

Code:
<P ALIGN="center"><FONT SIZE="2">
<IFRAME src="cgi-bin/events/db.cgi?uid=default&db=events&listnext=1&view_records=1" ALIGN="center" scrolling="no" frameborder="no"></IFRAME>
</P>
--
Nigel Horne. Arranger, Composer, Conductor, Typesetter.
Quote Reply
Re: [njh] Listing the next event in an IFRAME (Code included for db.cgi) In reply to
I've fixed a few bugs:

1) Events on today did not appear
2) The character set and language were not set
3) The last-modified HTTP attribute was not set.

To fix you will need to add this to the top of db.cgi:
Code:
use Time::Local; # NJH

Then modify view_records thus:

Code:
sub view_records {
# --------------------------------------------------------
# This is called when a user is searching the database for
# viewing. All the work is done in query() and the routines just
# checks to see if the search was successful or not and returns
# the user to the appropriate page.

# NJH
if($in{'listnext'}) {
# Suitable for use in an IFRAME

my @match;

my $mostrecent = 0;
my @mostrecentdate;
my $today = &date_to_unix(&get_date(time()));
my $matchdate = 0;

open (DB, "<$db_file_name") or &cgierr("error in search. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE; # Skip comment Lines.
(/^\s*$/) and next LINE; # Skip blank lines.
my $line = $_; chomp ($line); # Remove trailing new line.
my @values = &split_decode($line);

# Ignore any before today, 12 is my date field
my $thisdate = &date_to_unix($values[12]);

if($thisdate >= $today) {
# TODO: time as well as date
if(($matchdate == 0) || ($thisdate < $matchdate)) {
@match = @values;
$matchdate = $thisdate;

# TODO: if there's more than one on
# the same day pick one in random
last LINE if($thisdate == $today);
}
} else {
if(($mostrecent == 0) || ($thisdate > $mostrecent)) {
$mostrecent = $thisdate;
@mostrecentdate = @values;
}
}
}

close DB;

if($matchdate > 0) {
if (!$html_headers_printed) {
# print Last-Modified based on the most recent
# entry
print "Content-type: text/html; charset=ISO-8859-1\n";

if($mostrecent > 0) {
my ($dd, $mmm, $yy) = ($mostrecentdate[12] =~ /(\d+)-([A-Z][a-z][a-z])-(\d+)/);
my %months = (
"Jan" => 0,
"Feb" => 1,
"Mar" => 2,
"Apr" => 3,
"May" => 4,
"Jun" => 5,
"Jul" => 6,
"Aug" => 7,
"Sep" => 8,
"Oct" => 9,
"Nov" => 10,
"Dec" => 11);

my $mm = $mostrecentdate[6]; # mins
my $hh = $mostrecentdate[7]; # hours

$mostrecent = timelocal(0, $mm, $hh, $dd, $months{$mmm}, $yy - 1900);
my $mtime = localtime $mostrecent;
print "Last-Modified: $mtime\n";
}
print "\n";
$html_headers_printed = 1;
if ($ENV{'REQUEST_METHOD'} eq 'HEAD') {
return;
}
}

# Print date, desc and URL
print "<HTML LANG=\"en-GB\"><BODY BGCOLOR=\"#FFFFFF\">";
print "<FONT SIZE=\"2\"><I>";
print "<A HREF=\"$db_script_link_url&view_records=1&ID=$match[0]\" target=\"_top\">";
print "$match[12]</A>: $match[2]<BR>";

print "<A HREF=";

if ($match[9] ne "") {
my $url = $match[9];

$url =~ s/^http:\/\///;

print "\"../bbl2.cgi?redir=$url&address=";
print "$match[11]\" target=\"_top\"";
} else {
print "\"mailto:$match[11]\"";
}
print ">$match[1]</A></FONT></I></BODY></HTML>\n";
}
} else {
my ($status, @hits) = &query("view");
if ($status eq "ok") {
&html_view_success(@hits);
}
else {
&html_view_failure($status);
}
}
}
--
Nigel Horne. Arranger, Composer, Conductor, Typesetter.